Skip to Main Content

Breadcrumb

Question and Answer

Connor McDonald

Thanks for the question, Joel.

Asked: September 03, 2024 - 5:52 pm UTC

Last updated: September 04, 2024 - 2:28 am UTC

Version: 19c.22

Viewed 100+ times

You Asked

Hi,

thank you for taking my question,

I wanted to know the logic behind not being able to reference :new and :old variables in dynamic sql

execute immediate 'insert into tablea (columna, columnb) values (:old.columna,:new.columnb)';

this will fail

just trying to understand the logic behind it.


thank you for you response

PS constant browser on on your site loven it!

and Connor said...

Its one of those things, it "just is" :-)

new and old values are akin to bind variables, so just like you cannot do:

execute immediate 'delete from emp where empno = p_my_plsql_variable';


you can't do the same with new/old.

You can treat them like binds hence

execute immediate 'delete from emp where empno = :b1' using :new.empno;


eg

SQL> create table t ( x int );

Table created.

SQL> create or replace
  2  trigger trg
  3  before insert on t
  4  for each row
  5  declare
  6    v int;
  7  begin
  8    execute immediate 'select count(*) from emp where empno = :b1' using :new.x;
  9  end;
 10  /

Trigger created.


More to Explore

PL/SQL demos

Check out more PL/SQL tutorials on our LiveSQL tool.

PL/SQL docs

PL/SQL reference manual from the Oracle documentation library