简体   繁体   中英

Concat CLOB in PL/SQL for execute immediate

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM