Oracle:使用动态列名称更新 table
Oracle: update table using dynamic column names
我正在使用 Oracle 11g。我的 tables 包括名称和 l_name(名称列的小写)等列。我正在尝试遍历 table space 中的所有列,以将 l_ 列设置为其各自大写列的小写。这是我尝试过的:
for i in (select table_name from user_tables) loop
SELECT SUBSTR(column_name,3) bulk collect into my_temp_storage FROM user_tab_columns WHERE table_name = i.table_name and column_name like 'L\_%' escape '\';
for j in (select column_name from user_tab_columns where table_name = i.table_name) loop
for k in 1..my_temp_storage.count
loop
if(j.column_name like 'L\_%' escape '\' and SUBSTR(j.column_name,3) = my_temp_storage(k)) then
DBMS_OUTPUT.PUT_LINE( 'update ' || i.table_name || ' set ' || j.column_name || ' = LOWER(' ||my_temp_storage(k)|| ') where ' || j.column_name || ' is not null');
execute immediate 'update ' || i.table_name || ' set ' || j.column_name || ' = LOWER(' ||my_temp_storage(k)|| ') where ' || j.column_name || ' is not null';
end if;
end loop;
end loop;
end loop;
我将所有列的名称以大写形式存储在 my_temp_storage 中,并使用 my_temp_storage 中列的较低值更新 table。这给了我一条错误消息:
Error report -
ORA-00900: invalid SQL statement
ORA-06512: at line 8
00900. 00000 - "invalid SQL statement"
*Cause:
*Action:
但 DBMS 输出似乎没问题:
`update EMPLOYEE set L_NAME = LOWER(NAME) where L_NAME is not null`
你能帮我用我做的方式或任何其他方式吗?
程序当然可以简化:
begin
for i in (select table_name, column_name from user_tab_columns
where column_name like 'L\_%' escape '\')
loop
l_sql := 'update ' || i.table_name || ' set ' || i.column_name
|| ' = LOWER(' ||substr(i.columm_name,3)
|| ') where ' || i.column_name || ' is not null';
execute immediate l_sql;
end loop;
end;
不过这似乎是一个奇怪的数据库设计。您是否考虑过虚拟列、and/or 基于函数的索引,而不是手动维护的列?
我正在使用 Oracle 11g。我的 tables 包括名称和 l_name(名称列的小写)等列。我正在尝试遍历 table space 中的所有列,以将 l_ 列设置为其各自大写列的小写。这是我尝试过的:
for i in (select table_name from user_tables) loop
SELECT SUBSTR(column_name,3) bulk collect into my_temp_storage FROM user_tab_columns WHERE table_name = i.table_name and column_name like 'L\_%' escape '\';
for j in (select column_name from user_tab_columns where table_name = i.table_name) loop
for k in 1..my_temp_storage.count
loop
if(j.column_name like 'L\_%' escape '\' and SUBSTR(j.column_name,3) = my_temp_storage(k)) then
DBMS_OUTPUT.PUT_LINE( 'update ' || i.table_name || ' set ' || j.column_name || ' = LOWER(' ||my_temp_storage(k)|| ') where ' || j.column_name || ' is not null');
execute immediate 'update ' || i.table_name || ' set ' || j.column_name || ' = LOWER(' ||my_temp_storage(k)|| ') where ' || j.column_name || ' is not null';
end if;
end loop;
end loop;
end loop;
我将所有列的名称以大写形式存储在 my_temp_storage 中,并使用 my_temp_storage 中列的较低值更新 table。这给了我一条错误消息:
Error report -
ORA-00900: invalid SQL statement
ORA-06512: at line 8
00900. 00000 - "invalid SQL statement"
*Cause:
*Action:
但 DBMS 输出似乎没问题:
`update EMPLOYEE set L_NAME = LOWER(NAME) where L_NAME is not null`
你能帮我用我做的方式或任何其他方式吗?
程序当然可以简化:
begin
for i in (select table_name, column_name from user_tab_columns
where column_name like 'L\_%' escape '\')
loop
l_sql := 'update ' || i.table_name || ' set ' || i.column_name
|| ' = LOWER(' ||substr(i.columm_name,3)
|| ') where ' || i.column_name || ' is not null';
execute immediate l_sql;
end loop;
end;
不过这似乎是一个奇怪的数据库设计。您是否考虑过虚拟列、and/or 基于函数的索引,而不是手动维护的列?