Dynamic USING clause in execute immediate

Hi All,
Is it possible to build a dynamic using clause in execute immediate command.
EXECUTE IMMEDIATE <dynamic query> USING <dynamic clause>;
Rgrds,
Nitin.

Hi,
The problem is I have a query in which in some scenarios I want just one column in where clause, in other scenario I need 2 columns in the where clause.
So I would have to write 2 different queries.
execute immediate 'SELECT 1 FROM DUAL WHERE 1 = :bind1' into a using 1;
execute immediate 'SELECT 1 FROM DUAL WHERE 1 = :bind1 AND 2 = :bind2' into a using 1, 2;
Is there any way I can achieve this in a single query like:
dynamic_using_str varchar2(100);
dynamic_using_str := '1';
execute immediate 'SELECT 1 FROM DUAL WHERE 1 = :bind1' into a using dynamic_using_str;
dynamic_using_str := '1, 2';
execute immediate 'SELECT 1 FROM DUAL WHERE 1 = :bind1 AND 2 = :bind2' into a using dynamic_using_str;
~Nitin.
Edited by: user13060845 on Apr 30, 2010 12:29 PM

Similar Messages

  • Pass Pl/sql table into USING clause in EXECUTE IMMEDIATE statment

    Getting error when I try to pass the PL/SQL table into USING clause in EXECUTE IMMEDIATE statment:
    Declare
    result NUMBER;
    TYPE values_tab IS TABLE OF NUMBER INDEX BY BINARY_INTEGER;
    lv_tab values_tab;
    lv_exp varchar2(300);
    lv_exec varchar2(300);
    BEGIN
    lv_tab(1) := 5;
    lv_tab(2) := 48;
    lv_tab(3) := 7;
    lv_tab(4) := 6;
    lv_exp := ':b1+:b2+(:b3*:b4)';
    lv_exec := 'SELECT '||lv_exp ||' FROM DUAL';
    EXECUTE IMMEDIATE
    lv_exec
    INTO
    result
    USING
    lv_tab;
    DBMS_OUTPUT.PUT_LINE(result);
    END;
    Error at line 1
    ORA-06550: line 20, column 12:
    PLS-00457: expressions have to be of SQL types
    ORA-06550: line 15, column 8:
    PL/SQL: Statement ignored
    I am trying to evaluate the expression ":b1+:b2+(:b3*:b4)" which is stored in table. This table has different expressions (around 300 expressions). I want to use the bind variables in expression because each expression evaluated thousand of time may be more in some case. If I don't use bind variable then it fill shared pool.
    Is there any way I can pass the USING (IN) parameters dynamically instead of writing "USING lv_tab(1), lv_tab(2), lv_tab(3), lv_tab(4)"? As number of input parameters change depend on the expression in the table.
    If not possible please suggest any other ideas/approches
    Please help..
    Edited by: satnam on Jun 11, 2009 11:50 AM

    Well, you keep changing reqs faster I can keep up. Anyway, assuming N-th bind variable (left-to-right) corresponds to collection N-th element:
    Declare
        result NUMBER;
        lv_tab values_tab := values_tab();
        lv_exp varchar2(300);
        lv_exec varchar2(300);
        lv_i number := 0;
    BEGIN
        lv_tab.extend(4);
        lv_tab(1) := 5;
        lv_tab(2) := 48;
        lv_tab(3) := 7;
        lv_tab(4) := 6;
        lv_exp := ':5000135+:5403456+(:5900111*:5200456)';
        lv_exec := lv_exp;
        While regexp_like(lv_exec,':\d+') loop
          lv_i := lv_i + 1;
          lv_exec := REGEXP_REPLACE(lv_exec,':\d+',':b(' || lv_i || ')',1,1);
        end loop;
        lv_exec := 'BEGIN :a := ' || lv_exec || '; END;';
    DBMS_OUTPUT.PUT_LINE(lv_exec);
    EXECUTE IMMEDIATE lv_exec USING OUT result,IN lv_tab;
    DBMS_OUTPUT.PUT_LINE(result);
    END;
    BEGIN :a := :b(1)+:b(2)+(:b(3)*:b(4)); END;
    95
    PL/SQL procedure successfully completed.
    SQL> SY.

  • How to use using clause in execute immediate statement??

    Hi ALL,
    Can u help me ....
    This is the code which I have written...
    declare
    type rec_typ is table of forall_test%rowtype;
    v_rectype rec_typ:=rec_typ();
    begin
    --poputating records
    for i in 1..10000 loop
    v_rectype.extend;
    v_rectype(v_rectype.last).id:=i;
    v_rectype(v_rectype.last).code:=to_char(i);
    v_rectype(v_rectype.last).description:='Description :'||to_char(i);
    end loop;
    execute immediate 'truncate table forall_test';
    forall i in v_rectype.first..v_rectype.last
    execute immediate 'insert into forall_test values :1' using v_rectype(i);
    commit;
    end;
    But I am getting this ERROR....
    execute immediate 'insert into forall_test values :1' using v_rectype(i);
    ERROR at line 14:
    ORA-06550: line 14, column 61:
    PLS-00457: expressions have to be of SQL types
    ORA-06550: line 14, column 1:
    PL/SQL: Statement ignored
    Thanks & Regards,
    T.Halder

    Thatmeans using statement cannot be a non sql type.True: You need an sql type for this:
    e.g. with
    create or replace type emp_typ
    as
       object (empno number (4),
               ename varchar2 (10 byte),
               job varchar2 (9 byte),
               mgr number (4),
               hiredate date,
               sal number (7, 2),
               comm number (7, 2),
               deptno number (2))
    create or replace type emp_tab as table of emp_typ
    /you can do
    --- an empty test table
    SQL> create table emp2
    as
       select *
       from emp
       where 1 = 2
    Table created.
    SQL> declare
       emp2_tab       emp_tab;
    begin
      /* fill the collection */
       select emp_typ (empno,
                       ename,
                       job,
                       mgr,
                       hiredate,
                       sal,
                       comm,
                       deptno)
       bulk collect into emp2_tab
       from emp
       where empno like '77%';
      --  forall loop
       forall c in 1 .. emp2_tab.count
          execute immediate 'begin
                               insert into emp2 select * from table(cast(emp_tab(:1) as emp_tab)) t;
                               update emp2 set sal = null where empno = (:1).empno and empno = 7788;
                             end;' using emp2_tab (c);
    end;
    PL/SQL procedure successfully completed.
    SQL> select empno, ename, sal from emp2
         EMPNO ENAME             SAL
          7782 CLARK            2450
          7788 SCOTT               
    2 rows selected.

  • Dynamic using clause

    Hi All,
    Is it possible to build a dynamic using clause in execute immediate command.
    EXECUTE IMMEDIATE <dynamic query> USING <dynamic clause>;
    If not, is there any alteranate approach there?
    Regards
    Swaminathan

    "there is no such thing as a dynamically generated USING clause."
    Ask Tom &amp;quot;Returning cursor from stored procedure u...&amp;quot;
    Tom's referring to the following approach as an alternive: http://asktom.oracle.com/pls/asktom/f?p=100:11:::::P11_QUESTION_ID:1288401763279

  • Returning (bulk collect) clause with execute immediate

    db version 11.1.0.7
    trying to do a returning bulk collect but it is not working:
    -- my test table
    create table t as
    with f as (select rownum rn from dual connect by rownum <= 10)
    select
    rn,
    lpad('x',10,'x') pad
    from f;
    -- works as expected
    declare
    type aat is table of t%rowtype;
    aay aat;
    begin
    delete from t returning rn,pad bulk collect into aay;
    rollback;
    end;
    -- but the table I really want to do has many columns so I want to dynamically build list of columns for the
    -- returning clause. This way if the table changes the stored proc will not have to be modified
    -- fails PLS-00429: unsupported feature with RETURNING clause
    declare
    type aat is table of t%rowtype;
    aay aat;
    s varchar2(4000);
    begin
    s := 'delete from t returning rn,pad into :1';
    execute immediate s returning bulk collect into aay;
    rollback;
    end;
    -- tried a few other things:
    create or replace type t_obj as object (rn number,pad varchar2(10));
    -- PLS-00497: cannot mix between single row and multi-row (BULK) in INTO list
    declare
    nt t_obj;
    s varchar2(4000);
    begin
    s := 'delete from t returning t_obj(rn,pad) into :1';
    execute immediate s returning bulk collect into nt;
    rollback;
    end;
    -- works, but would require store proc changes if the table changes
    declare
    type t is table of number;
    type v is table of varchar2(10);
    vt v;
    nt t;
    s varchar2(4000);
    begin
    s := 'update t set rn = 10 returning rn,pad into :1,:2';
    execute immediate s returning bulk collect into nt,vt;
    rollback;
    end;
    /basically I want to dynamically build the list of columns with all_tab_cols and put the list into the returning clause
    but seems like I will have to hard code the column lists. This means whenever the table changes I will have to
    modify the store proc .. Any way around this?
    Thanks

    And with object type you were almost there. You forgot to create table of objects type:
    SQL> create or replace type t_obj as object (rn number,pad varchar2(10));
      2  /
    Type created.
    SQL> declare
      2      type aat is table of nt;
      3   aay aat;
      4   s varchar2(4000);
      5  begin
      6   s := 'delete from t returning
    SQL> declare
      2      type aat is table of t_obj;
      3   aay aat;
      4   s varchar2(4000);
      5  begin
      6   s := 'delete from t returning t_obj(rn,pad) into :1';
      7   execute immediate s returning bulk collect into aay;
      8   rollback;
      9  end;
    10  /
    PL/SQL procedure successfully completed.
    SQL> SY.

  • Dynamic SQL Select String VS non-dynamic USING Clause

    This isn’t a question, I just wanted to share a solution to a problem I have encountered several times.
    e.g. You have ‘n’ number of variables, of which any combination could be used in a SQL string. But it’s difficult to execute the SQL string with USING without an unpleasant CASE statement.
    Solution: Reference all ‘n’ bind variables in the FROM clause of the SQL in an in-line-view. And specify all variables in the USING Clause.
    When building the SQL String, specify the view.variable rather than directly referencing bind variables
    This eliminates the requirement to check which variables have been populated.
    <h6>
    t_select := ‘SELECT tab.cola, tab.colb’;
    t_from := ‘FROM (SELECT :1 cola, :2 colb, :3 colc, :4 cold, :5 cole) parms, tablea tab’;
    t_where := ‘WHERE tab.cola = parms.cola’;
    OPEN c t_select||t_from||t_where USING v1, v2, v3, v4, v5;
    </h6>
    I hope this is useful to someone...
    PeteD

    Interesting approach.. but I would still prefer creating a DBMS_SQL cursor instead - as there the binding can be fully controlled at run-time.
    If this has to return a ref cursor (caller is an external process), then I would rather use conditional processing logic (as that is what PL is about) in PL/SQL and build a "+proper+" SQL statement (with only the required bind variables) at run-time. Features like polymorphism makes this easy - and the call interface that is provided to the client is a lot cleaner.

  • Using RETURNING clause with Execute Immediate

    I wrote a query to update a table and return the column in to a nested table type variable using returning clause but its not working I am getting error like
    ORA-06550: line 66, column 22:
    PLS-00306: wrong number or types of arguments in call to '||'
    ORA-06550: line 66, column 4:
    PL/SQL: Statement ignored
    I am getting error in following part of my query
    || 'RETURNING if_row_status bulk collect INTO '
    || v_if_row_status;
    v_if_row_status is defined as -
    TYPE v_count IS TABLE OF varchar2(50) INDEX BY BINARY_INTEGER;
    v_if_row_status v_count;

    I am trying to update a table for diffrent column if they are null and want no of column updated for each column.
    I wrote following query but I am not getting the correct output.
    UPDATE
    Temp_Bulk_Col_POC
    SET if_row_status = 'VALIDATED',
    if_row_processed_date = sysdate,
    if_row_error_msg =
    CASE
    WHEN record_type IS NULL
    THEN 'RECORD_TYPE is a required column and cannot be NULL'
    WHEN source_system IS NULL
    THEN 'SOURCE_SYSTEM is a required column and cannot be NULL'
    WHEN record_company IS NULL
    THEN 'RECORD_COMPANY is a required column and cannot be NULL'
    WHEN record_system IS NULL
    THEN 'RECORD_SYSTEM is a required column and cannot be NULL'
    WHEN txn_flag IS NULL
    THEN 'TXN_FLAG is a required column and cannot be NULL'
    WHEN create_date IS NULL
    THEN 'CREATE_DATE is a required column and cannot be NULL'
    WHEN UPDATE_date IS NULL
    THEN 'UPDATE_DATE is a required column and cannot be NULL'
    WHEN source_customer_id IS NULL
    THEN 'SOURCE_CUSTOMER_ID is a required column and cannot be NULL'
    WHEN source_product_id IS NULL
    THEN 'SOURCE_PRODUCT_ID is a required column and cannot be NULL'
    WHEN az_product_id IS NULL
    THEN 'AZ_PRODUCT_ID is a required column and cannot be NULL'
    WHEN decile IS NULL
    THEN 'DECILE is a required column and cannot be NULL'
    END
    WHERE if_row_status IS NULL
    AND (record_type IS NULL
    OR source_system IS NULL
    OR record_company IS NULL
    OR record_system IS NULL
    OR txn_flag IS NULL
    OR create_date IS NULL
    OR UPDATE_date IS NULL
    OR source_customer_id IS NULL
    OR source_product_id IS NULL
    OR az_product_id IS NULL
    OR decile IS NULL)
    RETURNING if_row_status,record_type,source_system,record_company,record_system,
    txn_flag,create_date,UPDATE_date,source_customer_id,source_product_id,az_product_id,
    decile
    BULK COLLECT INTO
    v_if_row_status,v_record_type,v_source_system,
    v_record_company,v_record_system,v_txn_flag,v_create_date,v_UPDATE_date,
    v_source_customer_id,v_source_product_id,v_az_product_id,v_decile;
    its showing same number for all the column.
    how I can collect based on the coulmn updated

  • Execute immediate and dynamic sql

    Dear all;
    Just curious....Why do developers still use dynamic sql..and execute immediate, because I always thought dynamic sql were bads and the use of execute immediate as well...
    or am I missing something...

    There are no 'bad' things and 'good' things.
    There are 'correctly used' and 'incorrectly used' features.
    It depends what you want to do.
    One simple example: Oracle 11.2 - you write a package that fetches data from range interval partitioned table (a new partition is created automatically every day when new key values are inserted). If you use static SQL then whenever Oracle creates a new partition then your package gets invalidated and has to be compiled. If your package is heavily used (by many sessions running in parallel) then you may get this:
    ORA-04068: existing state of packages has been discarded
    ORA-04061: existing state of package body "PACKAGE.XXXXX" has been invalidated
    ORA-06508: PL/SQL: could not find program unit being called: "PACKAGE.XXXXX" Nice, isn't it?
    You can avoid this kind of problems by simply using dynamic SQL. You break dependency with the table and your package is not invalidated when new partition is created.

  • Cursor in EXECUTE IMMEDIATE

    How to use cursor in EXECUTE IMMEDIATE command.
    i.e. I have a dynamic SELECT query where I will get huge records while using EXECUTE IMEDIATE command, which I want that to be in a cursor.
    Can I do this ? If yes how ?

    Hi, it works perferctly fine with me. Oracle version 10.2.0.4
    SQL> --create table t as select * from user_objects; ----->>> My mistake, there is no owner column in the table user_objects
    SQL> create table t as select * from all_objects;
    Table created.
    SQL> CREATE OR REPLACE PROCEDURE dynamic_table (
      2    table_name IN VARCHAR2,
      condition IN VARCHAR2 DEFAULT NULL) AS
      3    4    where_clause VARCHAR2(100) := ' WHERE ' || condition;
      5    v_table varchar2(30);
      6    type v_typet  is table of t%rowtype index by pls_integer;
      7    v_tableT v_typeT;
      8  BEGIN
      -- first make sure that the table actually exists; if not, raise an exception
      SELECT OBJECT_NAME INTO v_table FROM USER_OBJECTS
      9   10   11    where object_name = upper(table_name) and object_type = 'TABLE';
      if condition is null then where_clause := ' order by object_type'; end if;
    12   13    execute immediate 'SELECT * from ' || v_table || where_clause  bulk collect into v_tablet;
    14    for indx in 1..v_tablet.count loop
        dbms_output.put_line(v_tablet(indx).owner || ' ' || v_tablet(indx).status);
    15   16    end loop;
    17    EXCEPTION
    18    when no_data_found then
    19      dbms_output.put_line ('Invalid table: ' || table_name);
    20  end;
    21  /
    Procedure created.
    begin
      dynamic_table('T', '');
      --Enter the table name you want for the 1st parameter and the condition for the second parameter.
      --I think you just want to switch from tables where you want to select.
    end;
    /Edited by: Spongebob on May 25, 2010 1:33 PM

  • How to see the data from the execute immediate o/p

    Hello
    i tried the following code
    SQL> declare
      2  vSql varchar2(3000);
      3  begin
      4  vSql:='SELECT';
      5   for i in (select column_name from user_tab_columns where table_name='EMP' order by column_name
      6   loop
      7     vSql:=vSql||i.column_name||',';
      8   end loop;
      9   execute immediate rtrim(vSql)||' '||'from emp';
    10  end;
    11  /
    declare
    ERROR at line 1:
    ORA-00900: invalid SQL statement
    ORA-06512: at line 9Please guide me to solve this error
    Edited by: josh1612 on Jun 22, 2010 3:58 AM

    josh1612 wrote:
    I need to execute that printed statment select ... from emp
    You are already executing it by using:
    execute immediate rtrim(vSql,',')||' from emp';What you probably want is to display results, right? Then you can either use bulk collect into clause in execute immediate or use cursor variable. Bulk collect example:
    set serveroutput on
    declare
      vSql varchar2(3000);
      type emp_rec_tbl_type is table of emp%rowtype;
      emp_rec_tbl emp_rec_tbl_type;
    begin
      vSql:='SELECT ';
       for i in (select column_name from user_tab_columns where table_name='EMP' order by column_id)
       loop
         vSql:=vSql||i.column_name||',';
       end loop;
       dbms_output.put_line(rtrim(vSql,',')||' from emp');
       execute immediate rtrim(vSql,',')||' from emp' bulk collect into emp_rec_tbl;
       for i in 1..emp_rec_tbl.count loop
         dbms_output.put_line(rpad(emp_rec_tbl(i).ename,10) || emp_rec_tbl(i).sal);
       end loop;
    end;
    SELECT EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO from emp
    SMITH     800
    ALLEN     1600
    WARD      1250
    JONES     2975
    MARTIN    1250
    BLAKE     2850
    CLARK     2450
    SCOTT     3000
    KING      5000
    TURNER    1500
    ADAMS     1100
    JAMES     950
    FORD      3000
    MILLER    1300
    PL/SQL procedure successfully completed.
    SQL> Cursor variable example:
    set serveroutput on
    declare
      vSql varchar2(3000);
      emp_rec emp%rowtype;
      cv sys_refcursor;
    begin
      vSql:='SELECT ';
       for i in (select column_name from user_tab_columns where table_name='EMP' order by column_id)
       loop
         vSql:=vSql||i.column_name||',';
       end loop;
       dbms_output.put_line(rtrim(vSql,',')||' from emp');
       open cv for rtrim(vSql,',')||' from emp';
       loop
         fetch cv into emp_rec;
         exit when cv%notfound;
         dbms_output.put_line(rpad(emp_rec.ename,10) || emp_rec.sal);
       end loop;
       close cv;
    end;
    SELECT EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO from emp
    SMITH     800
    ALLEN     1600
    WARD      1250
    JONES     2975
    MARTIN    1250
    BLAKE     2850
    CLARK     2450
    SCOTT     3000
    KING      5000
    TURNER    1500
    ADAMS     1100
    JAMES     950
    FORD      3000
    MILLER    1300
    PL/SQL procedure successfully completed.
    SQL> SY.

  • EXECUTE IMMEDIATE within Procedure doesn't work!!

    Hi,
    I have a code in which the procedure is successfully created, but when I try to check if the table TEST_TABLE is created (ex, DESC TEST_TABLE) it generates an error:
    ORA-04043: object TEST_TABLE does not exist
    Which means that my EXECUTE IMMEDIATE didn't work within the procedure for some reason, while it works perfectly alone without the procedure!!
    Hope you help me with this..
    Here's the my code:
    CREATE OR REPLACE PROCEDURE TEST_IMM1
    AS
    BEGIN
    EXECUTE IMMEDIATE ' CREATE TABLE TEST_TABLE (ITEM_DESC VARCHAR2(10))';
    END;
    --Procedure created.
    DESC TEST_TABLE
    ERROR:
    ORA-04043: object TEST_TABLE does not exist

    user11921409 wrote:
    Thanks a lot Ahmed, yes it worked after executing the procedure and table is created. But that's only one part of the problem, in which after dynamically creating the table TEST_TABLE, I should be able to insert data to it such as:
    CREATE OR REPLACE PROCEDURE TEST_IMM1
    AS
    BEGIN
    EXECUTE IMMEDIATE ' CREATE TABLE TEST_TABLE (ITEM_DESC VARCHAR2(10))';
    END;
    --Procedure created.
    CREATE OR REPLACE PROCEDURE INSERT_TEST_TABLE
    AS
    BEGIN
    TEST_IMM1;
    INSERT INTO TEST_TABLE VALUES ('A');
    END;
    --Warning: Procedure created with compilation errors.
    PL/SQL: SQL Statement ignored
    PL/SQL: ORA-00942: table or view does not exist
    Can you tell me how to use INSERT with EXECUTE IMMEDIATE.
    Thanks :)Just as an FYI, this is really not a good methodology to program in Oracle. If you're doing this for purely learning purposes then it's less 'bad', but if you plan on programming as a career in Oracle, this isn't the method you'd want to adopt (i'll not say there is NEVER a case for something like this, but it's a small percentage of the typical use cases).
    You would do better to create a global temporary table if you need a table to muck around with in a session, or create a permanent table (likely not via procedures) and have it persist in the schema of your choice.
    Utilizing execute immediate for insert statements, especially without BIND VARIABLES, will create a sad day for your database.

  • EXECUTE IMMEDIATE usage with SEQUENCE problem

    execute immediate 'INSERT INTO ZLYT_IZRACUN_RANGA_METRIK (IID_IZRACUNA_RANGA_METRIKE, POVPRECJE, DATUM_SPREMEMBE, UPORABNIK, IID_TIPA, IID_IZRACUNA, IID_METRIKE) VALUES (:1, :2, :3, :4, :5, :6, :7)'
    using 'zkes_ZLYZIZRAME.nextval', average_rank, sysdate, user, r_types.iid_tipa, r_types.iid_izracuna, r_types.iid_metrike;
    and i get 01722. 00000 - "invalid number".
    How must i use sequence in execute immediate?

    user13256715 wrote:
    execute immediate 'INSERT INTO ZLYT_IZRACUN_RANGA_METRIK (IID_IZRACUNA_RANGA_METRIKE, POVPRECJE, DATUM_SPREMEMBE, UPORABNIK, IID_TIPA, IID_IZRACUNA, IID_METRIKE) VALUES (:1, :2, :3, :4, :5, :6, :7)'
    using 'zkes_ZLYZIZRAME.nextval', average_rank, sysdate, user, r_types.iid_tipa, r_types.iid_izracuna, r_types.iid_metrike;
    and i get 01722. 00000 - "invalid number".
    How must i use sequence in execute immediate?Lose the quotes.
    create table for_immediate_execution
          col1 number primary key
       ,  col2 number
    create sequence s start with 1 increment by 1;
    begin
       execute immediate 'insert into for_immediate_execution (col1, col2) values (:0, :1)' using s.nextval, 2;
    end;
      4  /
    PL/SQL procedure successfully completed.
    TUBBY_TUBBZ?select * from for_immediate_execution;
          COL1      COL2
          4         2
    1 row selected.
    TUBBY_TUBBZ?

  • BULK SELECT EXECUTE IMMEDIATE

    I want to update 3 lakhs record in EXP__REQUEST_SK table
    How to use bulk select "execute immedaite" in PLSQL
    Update EXP__Request_sk set R_CUSTOMERNAME = cust_name where BFG_CUSTOMERID=cust_id;

    Note: this code is not tested.
    create or replace procedure generic_update (
      tab_name  varchar2,             -- table to update
      field_to_update  varchar2,     -- column to update
      field_value  varchar2,             -- new column value
      id_field_name  varchar2,        -- id column name
      id_value number)                 -- id value to identify the record
    as
      --local variables section
      stmt varchar2(4000) := '';
    begin
      stmt := 'update ' || tab_name || ' set ' || field_to_update || ' = :f_value where '
                 || id_field_name || ' = :i_value';
      savepoint txn1;
      execute immediate stmt using field_value, id_value;
      execute immediate ('commit');
      dbms_output.put_line('Update is done.');
    exception
       when others then
          rollback to txn1;
          dbms_output.put_line('Update has been rolled back.');
    end;
    /Even though I provided this, static SQL is always faster than PL/SQL because there is no context switch.
    An alternative to the above is to write all static SQLs into a script and run it.
    However, if you don't know which tables and column to update before hand, and the update depends on user inputs (e.g. from a JSP), then Java can call a stored procedure to perform the update.
    Edited by: pl/sql novice on Nov 20, 2008 11:24 PM

  • Execute immediate problem...

    Hi, who knows, what is wrong, please?
    PROCEDURE VYKONEJ (var_from IN NUMBER, var_to IN NUMBER) IS
    TYPE tab_of_commands IS TABLE OF PRIKAZY.PRIKAZ%TYPE;
    prikazy_array tab_of_commands; --this is table with sql commands (strings like 'select * from x;')
    BEGIN
    SELECT PRIKAZ
    BULK COLLECT INTO prikazy
    FROM PRIKAZY
    WHERE ID >= var_from and ID <= var_to;
    FORALL i IN prikazy.FIRST .. prikazy.LAST
    -- EXECUTE IMMEDIATE 'EXECUTE IMMEDIATE '':prikazy_array'';' USING prikazy_array(i);
    EXECUTE IMMEDIATE 'BEGIN '||prikazy_array(i)||' END;';
    --what is wrong with excecute command pls?
    END VYKONEJ;
    Can enyone help me, please?

    --what is wrong with excecute command pls?Check this:
    SQL> declare
       type tab_of_commands is table of emp.ename%type;
       prikazy   tab_of_commands;
    begin
       select ename
         bulk collect into prikazy
         from emp;
       forall i in prikazy.first .. prikazy.last
          execute immediate 'BEGIN dbms_output.put_line(:1); END;'
             using prikazy (i);
    end;
    SMITH
    ALLEN
    WARD
    JONES
    MARTIN
    BLAKE
    CLARK
    SCOTT
    KING
    TURNER
    ADAMS
    JAMES
    FORD
    MILLER
    PL/SQL procedure successfully completed.

  • Transaction control with execute immediate

    Hi, What is the correct way to commit/ rollback transactions when executing a dynamic update statement with execute immediate?

    Hi Thanks fo the reply, they don't appear to be. here's my procedure. Is there anything the is obviously wrong?
    PROCEDURE p_move_city_numerics ( p_mode IN VARCHAR2,
    p_schema IN VARCHAR2,
              p_table IN VARCHAR2,
                   p_destination IN VARCHAR2,
                   p_city_name IN VARCHAR2,
                   p_outcome     OUT VARCHAR2)
    IS
    l_valid_object VARCHAR2(200);
         l_error_msg VARCHAR2(200);
         l_outcome     VARCHAR2(2000);
         l_sql_count VARCHAR2(2000);
         l_sql_update VARCHAR2(2000);
         l_record_count NUMBER;
         e_object_error EXCEPTION;
    BEGIN
    l_valid_object := f_is_city_valid (p_schema, p_table, p_destination);
         IF l_valid_object != 'VALID' THEN
         RAISE e_object_error;
         END IF;
         l_sql_count := 'SELECT COUNT(*) FROM ' || p_schema || '.' || p_table;
         l_sql_count := l_sql_count || ' WHERE UPPER(city) LIKE ' || '''' || '%' || UPPER(p_city_name) || '%' || '''';
         l_sql_count := l_sql_count || ' AND ( city LIKE ' || '''' || '%1%' || '''';
         l_sql_count := l_sql_count || ' OR city LIKE ' || '''' || '%2%' || '''';
         l_sql_count := l_sql_count || ' OR city LIKE ' || '''' || '%3%' || '''';
         l_sql_count := l_sql_count || ' OR city LIKE ' || '''' || '%4%' || '''';
         l_sql_count := l_sql_count || ' OR city LIKE ' || '''' || '%5%' || '''';
         l_sql_count := l_sql_count || ' OR city LIKE ' || '''' || '%6%' || '''';
         l_sql_count := l_sql_count || ' OR city LIKE ' || '''' || '%7%' || '''';
         l_sql_count := l_sql_count || ' OR city LIKE ' || '''' || '%8%' || '''';
         l_sql_count := l_sql_count || ' OR city LIKE ' || '''' || '%9%' || '''';
         l_sql_count := l_sql_count || ' OR city LIKE ' || '''' || '%0%' || '''' || ')';
         l_sql_update := 'UPDATE ' || p_schema || '.' || p_table;
         l_sql_update := l_sql_update || ' SET ' || p_destination || ' = city,';
         l_sql_update := l_sql_update || ' city = ' || '''' || p_city_name || '''';
         l_sql_update := l_sql_update || ' WHERE UPPER(city) LIKE ' || '''' || '%' || UPPER(p_city_name) || '%' || '''';
         l_sql_update := l_sql_update || ' AND ( city LIKE ' || '''' || '%1%' || '''';
         l_sql_update := l_sql_update || ' OR ciity LIKE ' || '''' || '%2%' || '''';
         l_sql_update := l_sql_update || ' OR city LIKE ' || '''' || '%3%' || '''';
         l_sql_update := l_sql_update || ' OR city LIKE ' || '''' || '%4%' || '''';
         l_sql_update := l_sql_update || ' OR city LIKE ' || '''' || '%5%' || '''';
         l_sql_update := l_sql_update || ' OR city LIKE ' || '''' || '%6%' || '''';
         l_sql_update := l_sql_update || ' OR city LIKE ' || '''' || '%7%' || '''';
         l_sql_update := l_sql_update || ' OR city LIKE ' || '''' || '%8%' || '''';
         l_sql_update := l_sql_update || ' OR city LIKE ' || '''' || '%9%' || '''';
         l_sql_update := l_sql_update || ' OR city LIKE ' || '''' || '%0%' || '''' || ')';
         IF p_mode = 'SELECT' THEN
         EXECUTE IMMEDIATE(l_sql_count) INTO l_record_count;
         l_outcome := TO_CHAR(l_record_count) || ' records to be updated!';
         ELSIF p_mode = 'UPDATE' THEN
         EXECUTE IMMEDIATE(l_sql_count) INTO l_record_count;
         EXECUTE IMMEDIATE(l_sql_update);
    l_outcome := TO_CHAR(l_record_count) || ' records have been updated!';
         COMMIT;
         ELSE
         l_outcome := ' ERROR - Invalid mode: ' || p_mode || ' has been passed into the function! Valid values are SELECT or UPDATE';
         END IF;
    EXCEPTION
    WHEN e_object_error THEN
         IF l_valid_object = 'INVALID_TABLE' THEN
         l_outcome := 'ERROR - Not a valid table name: ' || p_schema || '.' || p_table;
         ELSIF l_valid_object = 'INVALID_CITY' THEN
         l_outcome := 'ERROR - CITY not a valid field in the table: ' || p_schema || '.' || p_table;
         ELSIF l_valid_object = 'INVALID_DEST' THEN
         l_outcome := 'ERROR - Destination field: ' || p_destination || ' is not a valid field in the table: ' || p_schema || '.' || p_table;
         END IF;
         WHEN OTHERS THEN
         l_error_msg := SQLCODE || '-' || SUBSTR(SQLERRM, 1, 150);
         l_outcome := 'ERROR - ' || l_error_msg;
         ROLLBACK;
    END p_move_city_numerics;

Maybe you are looking for

  • Import table with BLOB Column

    hi I would like to export table "A" containing data in BLOB column and then import the same table into another user's table "B". i got the following error ORA-06502PL/SQL: numeric or value error

  • Trouble creating a pdf converting Word doc ..."Could not find the application that created the file"

    Hello, I am working with Acrobat Pro 8 and am trying to create a pdf by converting a word doc. When I try that, I keep getting an error message, " Could not find the application that created this file...please select a file created by an application

  • Is this a data list photo gallery?

    http://www.heathrowe.com/other/fc2/main.html (click to the gallery page) Someone posted this in the show your Catylst project thread. Is this done with the Data list method? It's late, and I am beat, but I can't seem to get rollover functionality in

  • Org/apache/commons/fileupload/FileUploadException

    hia all, i am trying to save an image using <html:file> tag in jsp pages usig struts Before doing any save action,i am getting this error any one know thanx 8:41:52,714 ERROR [Engine] StandardWrapperValve[FrontController]: Servlet.service() for servl

  • How to get or reinstall complete Software of 2690

    I have nokia 2690 and its software is curpt.  I tried to reinstall from nokia Ovi suit but some files are missing and not correction reinstalling........... files like  Themes, ringtunes, browser, calculater , phone lock, etc. plz guide me i get comp