Replacing varchar2 with bigger data type in oracle SP

后端 未结 1 1070
终归单人心
终归单人心 2021-01-24 22:30

I am using oracle verion 10. There is stored procedure in PL/SQL using varchar2 variable. The code is constantly appending the varchar2 variable. When the varchar2 variable size

相关标签:
1条回答
  • 2021-01-24 22:43

    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.

    0 讨论(0)
提交回复
热议问题