Oracle:从SELECTed String执行查询

时间:2016-03-10 08:45:52

标签: oracle select execute

SELECT 'ALTER TABLE ' || uc.table_name || ' DROP CONSTRAINT ' || uc.constraint_name FROM user_constraints uc WHERE constraint_type = 'R';

返回一些我可以手动执行的语句,如:

ALTER TABLE APPLICATIONUSERROLE DROP CONSTRAINT APP_APPUSERROLE_FK1                       
ALTER TABLE APPLICATIONUSERROLE DROP CONSTRAINT USER_APPUSERROLE_FK1                      
ALTER TABLE APPLICATIONUSERROLE DROP CONSTRAINT ROLE_APPUSERROLE_FK1

我需要做什么才能自动执行它们?

例如我试过:

EXECUTE IMMEDIATE (SELECT 'ALTER TABLE ' || uc.table_name || ' DROP CONSTRAINT ' || uc.constraint_name FROM user_constraints uc WHERE constraint_type = 'R');

但那根本不起作用会造成一些错误。

提前致谢。

1 个答案:

答案 0 :(得分:0)

begin
   for i in (SELECT 'ALTER TABLE ' || uc.table_name || ' DROP CONSTRAINT ' || uc.constraint_name as sql_text FROM user_constraints uc WHERE constraint_type = 'R')
     loop
       execute immediate i.sql_text;
     end loop;
end;