Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Execute immediate of Anonymous pl/sql block stored in a VARCHAR2 DB Column

541243Aug 10 2007 — edited Aug 10 2007
Hi Guys,
I really hope someone can help me with this.

I have the following pl/sql anonymous block stored in a varchar2 database column.
declare 
    l_my_val varchar2(32767);
            cursor c_activity is
            SELECT 
                  l.DESCRIPTION || decode(l2.DESCRIPTION,null,'',l2.description,  '-' || l2.description) || decode(a.DT,'Y',' - Distributed Training','N',null,null) as value1
            FROM   ACTIVITY a
                  ,MOUNTAINEERING m
                  ,LOV l
                  ,LOV l2
            WHERE  a.JSATFA_ID = 82
            AND    a.SPECIFIC_ACTIVITY_LOV_ID = l.LOV_ID
            AND    m.ACTIVITY_ID(+) = a.ACTIVITY_ID
            AND    m.CLASSIFICATION_LOV_ID = l2.LOV_ID(+);
begin 

for each_row in c_activity loop
  l_my_val := l_my_val ||  ', ' || each_row.value1;
end loop;
  
 :l_value := ltrim (l_my_val, ', ');
 
end;  
The code is select out of the database and assigned to a local variable within my pl/sql package. The following code should demonstrate what I'm trying to achieve:
declare
  l_sql varchar2(32767);
  l_value varchar2(32767);
begin

  select query_sql into l_sql from lov where lov_id = 100;

  execute immediate l_sql using out :l_value;
 
  dbms_output.put_line(l_value);

end;
However Oracle (10.2.0.2) seems to be doing something funny with the single quotes. and gives the following error:
ORA-06550: line 1, column 1:
PLS-00103: Encountered the symbol "" when expecting one of the following:

   begin case declare exit for function goto if loop mod null
   package pragma procedure raise return select separate type
   update while with <an identifier>
   <a double-quoted delimited-identifier> <a bind variable> <<
   form table call close current define delete fetch lock insert
   open rollback savepoint set sql execute commit forall merge
   library OPERATOR_ pipe
error
If I embed the code I'm trying to execute within the package i.e
declare
  l_sql varchar2(32767);
  l_value varchar2(32767);
begin

  l_sql := q'~declare 
    l_my_val varchar2(32767);
            cursor c_activity is
            SELECT 
                  l.DESCRIPTION || decode(l2.DESCRIPTION,null,'',l2.description,  '-' || l2.description) || decode(a.DT,'Y',' - Distributed Training','N',null,null) as value1
            FROM   ACTIVITY a
                  ,MOUNTAINEERING m
                  ,LOV l
                  ,LOV l2
            WHERE  a.JSATFA_ID = 82
            AND    a.SPECIFIC_ACTIVITY_LOV_ID = l.LOV_ID
            AND    m.ACTIVITY_ID(+) = a.ACTIVITY_ID
            AND    m.CLASSIFICATION_LOV_ID = l2.LOV_ID(+);
begin 

for each_row in c_activity loop
  l_my_val := l_my_val ||  ', ' || each_row.value1;
end loop;
  
 :l_value := ltrim (l_my_val, ', ');
 
end;  
  ~';

  execute immediate l_sql using out :l_value;
 
  dbms_output.put_line(l_value);

end;
It works perfectly. Notice I have used the q syntax when embedding the sql directly into the package.

I have tried
- appending the q syntax directly to query_sql stored in the database
- escaping the quotes i.e. ' become ''

Neither method seem to work. We are running 10.2.0.2 on Windows Server 2003. If anyone has any suggestions I would love to hear from you as this has me stumped.

Regards
Kris
- http://kristianjones.blogspot.com
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Sep 7 2007
Added on Aug 10 2007
5 comments
4,175 views