Search code examples
oracleplsqldynamic-sql

Creating dynamic oracle sql query


I need to dynamically create a query in Oracle's PLSQL.

What i mean is something like this:

declare 
secondPart varchar2(100);


begin
select COLUMN into secondPart from TABLE where columnName='someName';
update firstPart_secondPart set SOME_COLUMN=1;
end

So basically what i want to do is to combine some constant string(firstPart_) with the dynamic value


Solution

  • You can use the execute immediate as follows:

    declare 
    secondPart varchar2(100);
    begin
    select COLUMN into secondPart from TABLE where columnName='someName';
    execute immediate 'update firstPart_' ||secondPart || ' set SOME_COLUMN=1';
    --commit/rollback;
    end;
    /