Skip to Main Content
  • Questions
  • How to use Clob parameter inside dynamic sql statement to execute immediate

Breadcrumb

Question and Answer

Chris Saxon

Thanks for the question, Xander.

Asked: December 06, 2016 - 2:33 pm UTC

Last updated: December 08, 2016 - 1:27 pm UTC

Version: Oracle 12c

Viewed 1000+ times

You Asked

Hello, Ask Tom team!
There is my problem.
i have a package procedure with some parameters:
procedure updt(p_col varchar2,p_value clob)
is
v_update_clob clob;
begin
v_update_clob:= 'update table set :p_col=:p_value where cond = ''some_condition'' ';
execute IMMEDIATE v_update_clob using p_col,p_value;
commit;
end updt;

in another procedure I want to send to updt procedure column name and clob variable:

updt('description',v_clob);

But get a errors such as string is too long or incomplete expression, if try rewrite procedure with hardcoding ( means||p_value||) like:

procedure updt(p_col varchar2,p_value clob)
is
begin
execute immediate
'update table set set '||p_key||'='''||p_value_clob||''' where cond = ''some_condition'' ';
commit;
end;

Can I improve this issue without changing of procedure logic?

and Chris said...

You can't bind column names! If you want to dynamically pass the column name, you have to append it into the statement. Do this and your clob should work fine:

create table t (
  x clob
);

insert into t values ('blah');
commit;
select * from t;

X     
blah 

create or replace procedure p(col varchar2, val clob) is
  stmt clob;
begin
  stmt := 'update t set ' || col || ' = :val';
  execute immediate stmt using val;
end p;
/
      
declare
  l clob;
begin
  dbms_lob.createtemporary(l, true);
  for i in 1 .. 10 loop
    dbms_lob.writeappend(l, 1000, rpad('x', 1000, 'x'));
  end loop;
  
  p('x', l);
end;
/

select length(x) from t;

LENGTH(X)  
10,000

Rating

  (2 ratings)

Is this answer out of date? If it is, please let us know via a Comment

Comments

Xander Matchang, December 08, 2016 - 11:51 am UTC


Xander Matchang, December 08, 2016 - 12:03 pm UTC

thanks, Chris. I understood it yesterday. Also I found another variant, that works too
https://asktom.oracle.com/pls/apex/f?p=100:11:0::::P11_QUESTION_ID:846056192691

For my porcedure it looks like:
procedure updt(p_col varchar2,p_value clob)
is
l_clob clob;
begin
execute immediate
'begin '||
'update sync setup '||
'set '||p_col||' = empty_clob()'||
' where cond = ''some_condition'' '||
'returning ' ||p_col|| ' into :y; '||
'end;'
USING OUT l_clob;
dbms_lob.writeappend( l_clob, length(p_value), p_value );
end updt;
Chris Saxon
December 08, 2016 - 1:27 pm UTC

Glad you figured it out.

More to Explore

CLOB/BLOB/etc

Complete documentation on Securefiles and Large Objects here