开发者

Concat CLOB in PL/SQL for execute immediate

开发者 https://www.devze.com 2023-01-29 13:53 出处:网络
I need to change a functions signature to have a parameter (p_param) of type CLOB, the previous type was nvarchar2.This caused me trouble when existing code was using || to concat.

I need to change a functions signature to have a parameter (p_param) of type CLOB, the previous type was nvarchar2. This caused me trouble when existing code was using || to concat.

Here is the existing snippet, that worked just fine for the NVarchar2, but apparently when you use || to concat a CLOB it treats it like a nvarchar2, which is a problem when the character limit of nvarchar2 is exceeded.

 execute immediate    
    'select p.person_id 
     from person p where p.id in (' || p_param || ')'
 bulk collect into persons;

I tried to change the above snippet to be

  execute immediate   
   concat(concat('select p.id from person p where p.person_id in(',p_param),')')
  bulk collect into members;

This doesn't compile, I get an error,

PLS-00382开发者_开发问答: expression is of wrong type

I am new to PL/SQL, thanks for the help.


As long as you have to modify this particular snippet of code to use DBMS_SQL, I would strongly consider changing it to eliminate the dynamic SQL approach entirely. Instead, I would take Tom's str2tbl function, modify it to take a CLOB (a subsequent reply has already done this) and then change the code to

select p.person_id 
  bulk collect into persons
  from person p 
 where p.id in (select column_value
                  from str2tbl( p_param ))

At a minimum, that will let you avoid creating large numbers of non-sharable statements, reduce the number of hard parses and the amount of time spent parsing, and reduce the pressure on the shared pool. It may also reduce the risk of things like SQL injection attacks.


You can't use a CLOB for EXECUTE IMMEDIATE until 11g. Until then, you'll need to switch to DBMS_SQL.

http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:7032414607769#2445120800346017454

0

精彩评论

暂无评论...
验证码 换一张
取 消