gpt4 book ai didi

sql - 创建一个新表,第一行作为旧表的列名

转载 作者:行者123 更新时间:2023-12-04 20:50:01 27 4
gpt4 key购买 nike

这个非常有趣的问题是创建存储过程以获取表名 oldTable 和新表名 newTable 的参数。newTable 的列名应该是oldTable 第一行的值,其余行是内容。到目前为止,如果我知道在以下查询中设计的列数,我就成功了。这是从工具导入数据所必需的,但没有选择标题行的选项,列在此工具中被命名为 field_1、field_2 等。

--Following is a solution but does not work when number of columns vary :     
SELECT 'CREATE TABLE NEWTABLE AS SELECT ' || REGEXP_REPLACE(FIELD_0, '[^a-zA-Z'']')
|| ',' || REGEXP_REPLACE(FIELD_1, '[^a-zA-Z'']')
|| ',' ||REGEXP_REPLACE(FIELD_2, '[^a-zA-Z'']')

|| ' FROM (SELECT ROWNUM R,'
|| 'FIELD_0 ' || REGEXP_REPLACE(FIELD_0, '[^a-zA-Z'']', '')
|| ', FIELD_1 ' || REGEXP_REPLACE(FIELD_1, '[^a-zA-Z'']', '')
|| ', FIELD_2 ' || REGEXP_REPLACE(FIELD_2, '[^a-zA-Z'']', '')

|| ' FROM test) WHERE R <> 1'
FROM oldTable
WHERE ROWNUM = 1

这里旧表的结构如下

 CREATE TABLE oldTable 
( "FIELD_0" VARCHAR2(30),
"FIELD_1" VARCHAR2(30),
"FIELD_2" VARCHAR2(30)
) ;
insert into oldTable (FIELD_0, FIELD_1, FIELD_2)
values ('a', 'b', 'c');

insert into oldTable (FIELD_0, FIELD_1, FIELD_2)
values ('apple', 'ball', 'cat');

insert into oldTable (FIELD_0, FIELD_1, FIELD_2)
values ('1', '23', '4');

Old Table
FIELD_0 FIELD_1 FIELD_2
1 a b c
2 apple ball cat
3 1 23 4

NewTable (First row of old table is Column of new table)
a b c
1 apple ball cat
2 1 23 4

我正在尝试创建一个适用于具有任意数量列的任何表的解决方案,如果有的话,该解决方案应该对很多人都有用。

如果我们可以像下面给出的那样循环遍历值和列,可能会有解决方案。唯一的问题是获取第一行的值来构造动态查询。

BEGIN
FOR col IN (select column_name
from cols
where upper(table_name) = upper('oldTable')) LOOP

--Some code here
END LOOP;
END;

最佳答案

我过去遇到过这个问题,我能够解决它的唯一方法是使用 DBMS_SQL .以下存储过程可以根据您的需要进行调整。有很多评论,请仔细阅读它们,因为它们解释了此过程的工作原理。我假设您能够识别第一行,这将是新表中的列名。 :

PROCEDURE create_stg_tab(old_tab_name IN VARCHAR2, new_tab_name IN VARCHAR2)
IS
v_ct number default 0;
v_col varchar2(1000) default '';
l_theCursor integer default dbms_sql.open_cursor;
l_colCnt number;
l_descTbl dbms_sql.desc_tab;
l_columnValue varchar2(4000);
l_status integer;
v_dest_cols varchar2(1000) default '';
v_sql varchar2(1000) default '';
v_col_insert varchar2(32000) default '';
v_sql_insert varchar2(32000) default '';

BEGIN

--get the number of columns of the source table
select count(*) into v_ct from all_tab_cols where upper(table_name) = old_tab_name AND OWNER = <SCHEMA NAME>;

--build your dynamic source query
if v_ct > 0 then
for i in (select column_name from all_tab_cols where table_name = old_tab_name AND OWNER = <SCHEMA NAME>)
loop
v_col := ltrim((v_col||','||i.column_name),',');
end loop;

--Get dynamic select all columns from old table
v_sql := 'select '||v_col||' from '||old_tab_name;
dbms_sql.parse(l_theCursor,v_sql,dbms_sql.native );
dbms_sql.describe_columns( l_theCursor, l_colCnt, l_descTbl);

for i in 1 .. l_colCnt loop
dbms_sql.define_column(l_theCursor, i, l_columnValue, 4000);
end loop;

--execute cursor
l_status := dbms_sql.execute(l_theCursor);

--loop through the rows
while ( dbms_sql.fetch_rows(l_theCursor) > 0 ) loop
--loop through the columns
for i in 1 .. l_colCnt loop

dbms_sql.column_value( l_theCursor, i, l_columnValue );
IF l_columnValue IS NOT NULL THEN
--REGEXP_REPLACE start from inner most (You can customize/ignore this):
--1)if value is just a number, replace it with the source column name
--2)if value starts with anything except letters (e.g. start with number but has letters), remove the leading non-letters
--3)replace all non leading characters (except numbers and letters) with '_'
--Ex.1289789bbB#4B$5 => bbB_4B5, 222 => source_column name
v_dest_cols := v_dest_cols || (l_descTbl(i).col_name||' as '||substr(REGEXP_REPLACE(REGEXP_replace(REGEXP_REPLACE(l_columnValue,'^\d+$', l_descTbl(i).col_name, 1),'^[^a-z,A-Z]+', null, 1),'[^a-z,A-Z,0-9]', '_'),0,30))||',';
v_col_insert := ltrim((v_col_insert||','||l_descTbl(i).col_name),',');
END IF;
end loop;

--remove last ','
v_dest_cols := rtrim(v_dest_cols,',');
--build create new empty table statement
v_sql := 'create table '||new_tab_name||' as select '||v_dest_cols||' from '||old_tab_name||' where 1=2';
--build insert into the new table statement
v_sql_insert := 'insert into '||new_tab_name||' select '||v_col_insert||' from '||old_tab_name||' WHERE <condition to fetch rows from 2nd onwards>';
end loop;

--execute
execute immediate v_sql;
execute immediate v_sql_insert;
end if;

exception
--your exception
END create_stg_tab;

关于sql - 创建一个新表,第一行作为旧表的列名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43246762/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com