Replacing varchar2 with bigger data type in oracle SP

匆匆过客 提交于 2019-12-02 05:24:13

The long datatype is deprecated; if you can you should seriously consider migrating your long column to a clob.

If you were working with a clob you could append past the 32k varchar2 limit like this:

declare
  l_clob clob;
begin
  dbms_lob.createtemporary(l_clob, true);
  dbms_lob.open(l_clob, dbms_lob.lob_readwrite);
  dbms_lob.writeappend(l_clob, 4, '1234');
  for i in 1..10000 loop
    dbms_lob.writeappend(l_clob, 5, '.5678');
  end loop;
  dbms_output.put_line('CLOB length: ' || length(l_clob));
  dbms_lob.close(l_clob);
  dbms_lob.freetemporary(l_clob);    end;
/

CLOB length: 50004

You can append to a long with the concatenate operator ||, but as you've seen already, only up to 32k. There is no easy way to handle long values above that within PL/SQL. You might be able to do soemthing with dbms_sql but it's really not going to be worth the effort if there is any possiblility of switching the table column to a clob.


If you want to pass the clob back to the caller, and it's a temporary clob, it will have to be defined by the caller and be passed it after it's created:

create or replace procedure proc1 as
  l_clob clob;
begin
  dbms_lob.createtemporary(l_clob, true);

  proc2(l_clob);
  dbms_output.put_line('proc1 CLOB length: ' || length(l_clob));

  dbms_lob.freetemporary(l_clob);
end;
/

create or replace procedure proc2(p_clob in out clob) as
begin
  dbms_lob.open(p_clob, dbms_lob.lob_readwrite);
  dbms_lob.writeappend(p_clob, 5, '12345');
  for i in 1..9999 loop
    dbms_lob.writeappend(p_clob, 5, '.56789');
  end loop;
  dbms_output.put_line('proc2 CLOB length: ' || length(p_clob));
  dbms_lob.close(p_clob);
end;
/

exec procs;

proc2 CLOB length: 50000
proc1 CLOB length: 50000

Otherwise the object won't exist as far as the caller is concerned.

If the clob exists - selected from a table, say, so you don't need the createtemporary call - then you can just assign it to an out parameter, but I don't think that's the case for what you've described.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!