如何编译保存在表中的PL / SQL源?

时间:2016-12-09 18:32:20

标签: oracle plsql

如何编译我目前保存在Oracle表中的PL / SQL源代码? (我正在从USER_SOURCE视图中复制源代码,然后我删除这些对象,并希望从我保存的副本中恢复。)

我确信有一种简单的方法,但我没有输入正确的搜索字词。

1 个答案:

答案 0 :(得分:6)

试试这个:

declare
 text varchar2(4000);
begin
 select code into text from bkp_table;
 execute immediate 'create or replace ' || text;
end;
/

好的,如果所有代码行都存储在单行中,则此方法有效。如果你想执行存储在多行中的代码,你应该选择:

declare 
  text varchar2(32767);
begin
  select listagg(text, ' ') within group (order by line) into text from all_source where name = 'MYPROC';
  execute immediate 'create or replace ' || text;
end;
/

当32767个字符太少时,问题就开始了。在这种情况下,这可能是一个解决方案:

declare 
  text clob;
begin
for x in (select text from all_source where name = 'LONGTEST') loop
  text := text || x.text;
end loop;
  execute immediate 'create or replace ' || text;
end;
/

请同时查看that为何有点奇怪。

修改

建议更改为dbms_lob,在这种情况下clob需要初始化:

declare 
  text clob := ' ';
begin
for x in (select text from all_source where name = 'LONGTEST') loop
  dbms_lob.append(text, x.text);
end loop;
  execute immediate 'create or replace ' || text;
end;
/