Inserting variables to table by "execute immediate' statement

EXECUTE IMMEDIATE 'INSERT INTO aTABLETKA_HISTORY
(update_id,
update_type,
update_date,
old_db_version,
description,
patch_number)
values
(' || m_id(v_rec) || ',
'||m_package(v_rec)||',
''07-NOV-07'',
''snezi'',
'||m_desc(v_rec)||',
'||m_new(v_rec)||')';
ORA-00917: missing comma
ORA-06512: at line 142
i cannot solve this,. can anybody help me?
i have simillar insert:
EXECUTE IMMEDIATE 'INSERT INTO ab_table_VERSION(property,version) values (''wiz_datalink'',' || my_version || ')';
and this one statement work correctly

Do NOT use "execute immediate". What is the reason for trying it this way?
If the column update_date is of type date, then you have a bug in the code, as you are trying to insert a string. If it is not a date data type, then you have a bug, because it is not of type date.
What is the outcome of:
INSERT INTO aTABLETKA_HISTORY (update_id, update_type, update_date,
      old_db_version, description, patch_number)
   values (m_id(v_rec), m_package(v_rec), to_date('07-NOV-2007', 'dd-MON-yyyy'),
      snezi, m_desc(v_rec), m_new(v_rec) )
/

Similar Messages

  • How to insert variable for table name in Select statement ?

    I am creating a stored procedure which will take two table names as IN parameters. Within the procedures I would like to use the parameters in the following manner;
         SELECT count(*)
         INTO v_target_cnt
         FROM TargetTable;
    TargetTable is one of the parameters passed in. When I do this however it does not recognize the parameter. I have tried assigning the parameter to a local variable and using the variable, with not luck.
    Any help....thanks

    Null,
    What you are describing is called a LEXICAL parameter, which is allowed (preceded by an ampersand) in sql but not in pl/sql because it would not be possible to compile it. That is why you need to use Andrew's suggestion to make the sql dynamic. In older versions you would need to use DBMS_SQL which is horrible and now thankfully redundant.

  • Unable to INSERT PL/SQL  record with EXECUTE IMMEDIATE

    Hi All,
    I am selecting data from a source table and after some modification inserting into a target table. Source and target table name are available at run time. You can say only source table structure is fixed.
    I have crated a pl/sql table of type source record and inserting record by record in target table using execute immediate. But I am not able to write
    EXECUTE IMMEDIATE string USING pl_sql_table(index); and getting error as
    PLS-00457: expressions have to be of SQL types
    Please see the part of code below. Is it possible to use FORALL with dynamic sql like
    FORALL pl_sql_table.FIRST .. pl_sql_table.COUNT
    EXECUTE IMMEDIATE .... pl_sql_table(j); -- Like this.
    Please suggest why I am not able to write record here. I also want to replace 'INSERT in a loop' with a single INSERT statement out of the loop, to upload whole pl_sql table into target table in one go.
    Thanks,
    Ravi
    DECLARE
        TYPE rec_tab_CMP IS RECORD
         model_id          NUMBER(38),   
         absolute_rank          NUMBER(5)         
        v_rec_tab_CMP  rec_tab_CMP;
        TYPE t_rec_tab_CMP IS TABLE OF v_rec_tab_CMP%TYPE INDEX BY BINARY_INTEGER;
        v_records_CMP               t_rec_tab_CMP;
        rc                          SYS_REFCURSOR;
        v_old_table_name            VARCHAR2(30); -- passed from parameter 
        v_new_table_name            VARCHAR2(30); -- passed from parameter 
        dyn_str                     VARCHAR2(500);
        v_columns_str               VARCHAR2(200) := ' model_id, absolute_rank ';
    BEGIN
           EXECUTE IMMEDIATE 'CREATE TABLE '|| v_new_table_name || ' AS SELECT * FROM ' || v_old_table_name ||' WHERE 1 = 2 ' ;
         OPEN rc FOR 'SELECT '|| v_columns_str ||' FROM '|| v_old_table_name;
         FETCH rc BULK COLLECT INTO v_records_CMP;
         FOR j IN 1..v_records_CMP.COUNT
         LOOP
            v_records_CMP(j).model_id := 1; -- Do someting here, This thing can be performed in SQL stmt directly.
            dyn_str := 'INSERT INTO '|| v_new_table_name ||' ( '|| v_columns_str || ' ) VALUES (:1, :2) ';
            EXECUTE IMMEDIATE dyn_str USING v_records_CMP(j).model_id          ,
                                v_records_CMP(j).absolute_rank     ;
         -- Here in place of two columns I want to use one record like
         -- EXECUTE IMMEDIATE dyn_str USING v_records_CMP(j);
         -- But it is giving me error like
            --          EXECUTE IMMEDIATE dyn_str USING v_records_st(j);
            --   PLS-00457: expressions have to be of SQL types
         END LOOP;
         CLOSE rc;
    END;
    /

    You cannot bind PL/SQL record types to dynamic SQL.
    Possibly you could work around this by declaring the INDEX-BY table of records at package specification level, e.g.
    Oracle Database 10g Express Edition Release 10.2.0.1.0 - Production
    SQL> CREATE PACKAGE package_name
      2  AS
      3     TYPE tt_emp IS TABLE OF emp%ROWTYPE;
      4     t_emp tt_emp;
      5  END package_name;
      6  /
    Package created.
    SQL> CREATE TABLE new_emp
      2  AS
      3     SELECT *
      4     FROM   emp
      5     WHERE  1 = 0;
    Table created.
    SQL> DECLARE
      2     v_table_name user_tables.table_name%TYPE := 'NEW_EMP';
      3  BEGIN
      4     SELECT *
      5     BULK COLLECT INTO package_name.t_emp
      6     FROM   emp;
      7
      8     EXECUTE IMMEDIATE
      9        'BEGIN ' ||
    10        '   FORALL i IN 1 ..package_name.t_emp.COUNT ' ||
    11        '      INSERT INTO ' || v_table_name ||
    12        '      VALUES package_name.t_emp (i); ' ||
    13        'END;';
    14  END;
    15  /
    PL/SQL procedure successfully completed.
    SQL> SELECT empno, ename
      2  FROM   new_emp;
         EMPNO ENAME
          7369 SMITH
          7499 ALLEN
          7521 WARD
          7566 JONES
          7654 MARTIN
          7698 BLAKE
          7782 CLARK
          7788 SCOTT
          7839 KING
          7844 TURNER
          7876 ADAMS
          7900 JAMES
          7902 FORD
          7934 MILLER
    14 rows selected.
    SQL>

  • Problem with alter table in execute immediate

    We have PL/SQL scripts which modify the data structure, add data etc to en existing database. These scripts basically ensure that the db is compatible with what the software expects.
    The reason for doing it in PL/SQL rather than SQL script is A) The scripts are launched using GUI so that they are more user friendly. sqlplus is launched as a background process which executes these scripts. All the scripts have EXIT FAILURE WHENEVER SQLERROR as first line to ensure that the control does not hang in the sqlplus background process.
    Going from one version to other, we have added a few tables to the database and also modified a few tables. since (i think) DDL is not allowed in PL/SQL block, we had to resort to putting them in EXECUTE IMMEDIATE enclosures.
    Now for the real question,
    If I create a table using EXECUTE IMMEDIATE clause, I can immediately have insert as a next statement to insert data in this table. but, if I alter the table and add a column to the existing table, I cannot immediately add data to that column, it throws an error saying 'invalid identifier'
    At code level, the following is allowed :
    EXECUTE IMMEDIATE 'CREATE TABLE SP_TEST_TABLE
                             ID     NUMBER,
                             NAME     Varchar2(40)
    INSERT INTO SP_TEST_TABLE(ID, NAME) Values(1, 'SP');
    but I get error for the following :
    EXECUTE IMMEDIATE 'ALTER TABLE SP_TEST_TWO ADD
                        ANOTHER_COLUMN     number
    UPDATE     SP_TEST_TWO SET          ANOTHER_COLUMN = 1;
    In this case, it says ANOTHER_COLUMN invalid identifier
    Does anybody know why?
    any workaround will be greatly appreciated.
    --SP                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       

    Friends,
    Thanks to all of you for your help. The spelling mistakes might have occurred becuase i changed the actual script to something 'short and complete' to show the problem.
    I could solve the problem by having more than one PL/SQL block within my file. something like :
    BEGIN
    --alter table statements here
    END;
    BEGIN
    --insert the values in column here.
    END;
    I am still not sure why the error is presented only on alter table statement and not for create table statement. Probably somebody with the knowledge of oracle internals will be able to throw more light on it. I am trying to get the naswers, if I get the answer, I'll surely post it here.
    Regards,
    --Saurabh                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           

  • How to make my Execute Immediate statement to work??

    Hi, Sir:
    I use SQL Oracle 9i,
    I have following procedure called by a trigger:
    CREATE OR REPLACE PROCEDURE emps_check IS
    s_sql VARCHAR2(500);
    BEGIN
    DBMS_OUTPUT.PUT_LINE('1. Start to Delete all rows in the emps' );
    delete from emps;
    DBMS_OUTPUT.PUT_LINE('2. Start to insert into emps from emp' );
    s_sql:='INSERT INTO Emps(EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) select EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO from emp;';
    DBMS_OUTPUT.PUT_LINE('3. s_sql= ' || s_sql );
    EXECUTE IMMEDIATE s_sql;
    DBMS_OUTPUT.PUT_LINE('3. Procedure call OK, Employ Name ' );
    EXCEPTION
    -- Use this to trap the ORA-00942: table or view does not exist
    WHEN OTHERS THEN
    DBMS_OUTPUT.PUT_LINE('Procedure call Exception: Employ Name ' );
    end emps_check;
    My trigger is:
    CREATE OR REPLACE TRIGGER emps_biu_after
    after INSERT OR UPDATE or delete on emp
    for each row
    begin
    if TO_CHAR(SYSDate,'DY')= 'SUN'
    then
    DBMS_OUTPUT.PUT_LINE('Today is holiday, ename =' );
    else
    DBMS_OUTPUT.PUT_LINE('Today is working Day, ename =');
    emps_check();
    end if;
    end;
    When I use following to update emp table:
    SQL> UPDATE emp T1
    2 SET T1.sal = 200.0
    3 WHERE T1.ename like '%SCOTT%';
    I got:
    Today is working Day, Trigger Call Procedure emps_check()
    1. Start to Delete all rows in the emps
    2. Start to insert into emps from emp
    3. s_sql= INSERT INTO Emps(EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) select EMPNO,ENAME,JOB,MGR
    Procedure call Exception: Employ Name
    Trigger Call Procedure emps_check() Success
    Looks like EXECUTE IMMEDIATE s_sql; did not work.
    What is wrong??
    How to make my Execute Immediate statement to work??
    Thanks

    You may omit the ';' character...
    s_sql:='INSERT INTO
    Emps(EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO)
    select EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO
    from emp';
    ... and something else
    IN PL/SQL when a procedure , function doews not take
    any parameter(s) then its call can be made as simply
    its name without a pair of parenthesis....(It seems
    that you are influenced by Java...:) )
    SimThanks Sir:
    I remove this ; but still error:
    1 UPDATE emp T1
    2 SET T1.sal = 768.1
    3* wHERE T1.ename like '%SCOTT%'
    SQL> /
    Today is working Day, Trigger Call Procedure emps_check()
    1. Start to Delete all rows in the emps
    2. Start to insert into emps from emp
    3. s_sql= INSERT INTO Emps(EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) select EMPNO,ENAME,JOB,MGR,
    Procedure call Exception: Employ Name
    Trigger Call Procedure emps_check() Success
    My code as follows:
    CREATE OR REPLACE PROCEDURE emps_check IS
    s_sql VARCHAR2(500);
    BEGIN
    DBMS_OUTPUT.PUT_LINE('1. Start to Delete all rows in the emps' );
    delete from emps;
    DBMS_OUTPUT.PUT_LINE('2. Start to insert into emps from emp' );
    s_sql:='INSERT INTO Emps(EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) select EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO from emp';
    DBMS_OUTPUT.PUT_LINE('3. s_sql= ' || s_sql );
    EXECUTE IMMEDIATE s_sql;
    DBMS_OUTPUT.PUT_LINE('3. Procedure call OK, Employ Name ' );
    EXCEPTION
    -- Use this to trap the ORA-00942: table or view does not exist
    WHEN OTHERS THEN
    DBMS_OUTPUT.PUT_LINE('Procedure call Exception: Employ Name ' );
    end emps_check;
    what is other wrong??
    Yes, I am java guys,
    Thanks

  • Create temp table using EXECUTE IMMEDIATE

    Is there any performance issue in creating globally temp table
    using EXECUTE IMMEDIATE or creating globally temp table from
    SQL PLUS.
    Any response will be greatly appreciated.
    null

    Anish,
    Creating tables is likely to be an expensive operation.
    Performance issues can only be considered in comparison to
    alternatives.
    Alternatives include: PLSQL tables, cursors and/or recoding so
    that tmp tables are not required. (One of our consultants reckons
    that sqlserver temp tables are usually used to get around
    limitations in sqlserver, ie slightly more complicated sql
    statements could be used instead of simpler sql and temporary
    tables).
    I would think creating the temp table once during sqlplus would
    be cheaper than creating and deleting it repeatedly at run time.
    Note that EXECUTE IMMEDIATE may do an implicit commit (dbms_sql
    certainly does). This may be got over my using the PRAGMA
    AUTONOMOUS_TRANSACTION; direction which places a
    procedure/function in a seperate transaction.
    Turloch
    P.S. We have some difficulty in getting information back from the
    field/customer sites. If you have questions and answers that are
    likely to be useful to other Oracle Migration Workbench
    users, and migrators in general, please send them in for possible
    inclusion in our Frequently Asked Question list.
    Oracle Migration Workbench Team
    Anish (guest) wrote:
    : Is there any performance issue in creating globally temp table
    : using EXECUTE IMMEDIATE or creating globally temp table from
    : SQL PLUS.
    : Any response will be greatly appreciated.
    Oracle Technology Network
    http://technet.oracle.com
    null

  • CREATE TEMPORARY TABLE USING EXECUTE IMMEDIATE

    Hi All,
    i have a question,
    how can i create a temporary table using EXECUTE IMMEDIATE ??
    Like:
    CREATE GLOBAL TEMPORARY table new_table as (Select * from old_table);
    Thanks,
    Edited by: xDeviates on Jun 11, 2012 3:13 PM

    It looks like you are approaching the problem incorrectly. As I suggested in Dynamic Select, it sounds like you, at most, want a function that returns a SYS_REFCURSOR (it's still not obvious to me why you would even want/ need to resort to dynamic SQL in the first place)
    CREATE OR REPLACE FUNCTION get_dynamic_cursor( p_table_name IN VARCHAR2 )
      RETURN sys_refcursor
    IS
      l_rc sys_refcursor;
    BEGIN
      OPEN l_rc FOR 'SELECT * FROM ' || dbms_assert.sql_object_name( p_table_name );
      RETURN l_rc;
    END;which you can then call from your application
    SQL> variable rc refcursor;
    SQL> exec :rc := get_dynamic_cursor( 'EMP' );
    PL/SQL procedure successfully completed.
    SQL> print rc
         EMPNO ENAME      JOB              MGR HIREDATE         SAL       COMM
        DEPTNO
          7369 SMITH      CLERK           7902 17-DEC-80        801
            20
          7499 ALLEN      SALESMAN        7698 20-FEB-81       1601        300
            30
          7521 WARD       SALESMAN        7698 22-FEB-81       1251        500
            30
         EMPNO ENAME      JOB              MGR HIREDATE         SAL       COMM
        DEPTNO
          7566 JONES      MANAGER         7839 02-APR-81       2976
            20
          7654 MARTIN     SALESMAN        7698 28-SEP-81       1251       1400
            30
          7698 BLAKE      MANAGER         7839 01-MAY-81       2851
            30
         EMPNO ENAME      JOB              MGR HIREDATE         SAL       COMM
        DEPTNO
          7782 CLARK      MANAGER         7839 09-JUN-81       2451
            10
          7788 SCOTT      ANALYST         7566 19-APR-87       3001
            20
          7839 KING       PRESIDENT            17-NOV-81       5001
            10
         EMPNO ENAME      JOB              MGR HIREDATE         SAL       COMM
        DEPTNO
          7844 TURNER     SALESMAN        7698 08-SEP-81       1501          0
            30
          7876 ADAMS      CLERK           7788 23-MAY-87       1101
            20
          7900 JAMES      CLERK           7698 03-DEC-81        951
            30
         EMPNO ENAME      JOB              MGR HIREDATE         SAL       COMM
        DEPTNO
          7902 FORD       ANALYST         7566 03-DEC-81       3001
            20
          7934 MILLER     CLERK           7782 23-JAN-82       1301
            10
    14 rows selected.Justin

  • Selecting data from a table using Execute Immediate in 9i

    Hi,
    I was wondering how I would be able to do a SELECT on a table using EXECUTE IMMEDIATE. When I tried it (with the proc below), I got the following below. Can anyone tell me what I am doing wrong?
    Using Scott/Tiger I tried this:
    SQL> ed
    Wrote file afiedt.buf
    1 CREATE OR REPLACE PROCEDURE P2
    2 IS
    3 v_SQL VARCHAR2(100);
    4 BEGIN
    5 v_SQL := 'Select * from Emp';
    6 EXECUTE IMMEDIATE v_SQL;
    7* END;
    SQL> /
    Procedure created.
    SQL> exec p2
    PL/SQL procedure successfully completed.
    SQL> set serveroutput on
    SQL> exec p2
    PL/SQL procedure successfully completed.
    SQL>
    Thanks in advance.
    Sincerely,
    Nikhil Kulkarni

    1 CREATE OR REPLACE PROCEDURE P2
    2 IS
    3 v_SQL VARCHAR2(100);
    erec emp%rowtype;
    4 BEGIN
    5 v_SQL := 'Select * from Emp';
    6 EXECUTE IMMEDIATE v_SQL into erec;
    7* END;
    SQL> /

  • How to run a EXECUTE IMMEDIATE statement in a Interactive Report

    Hello all!!
    I need to make a dinamic construction of a query to execute in a Interactive Report, but the Region Source only allows simple SELECT statements. There is any way to run a EXECUTE IMMEDIATE statement in a Interactive Report Region Source?
    Regards Pedro.

    Thank you Andy for your reply.
    I have been testing for a while the use of a collection in the interactive report but i am unable to load data in the interactive report.
    I created the collection successfully in the SQL Commands with the code:
    declare
    v_sql varchar2(32000);
    begin
    v_sql:='select ename, job from emp';
    APEX_COLLECTION.CREATE_COLLECTION_FROM_QUERY('coleccao_emp',v_sql,'NO' );
    end;
    I tested successfully the creation of the collection with the query:
    SELECT c001, c002
    FROM APEX_collections
    WHERE collection_name = 'COLECCAO_EMP'
    My problem is: the data of the collection are returned in the SQL Commands but when i run the query statament in an interactive report, report or even a Pl/Sql region in my application the data aren't displayed, only the message (No data found).
    Can someone explain why, besides the collection is populated the interactive can not print the results.
    Regards Pedro.

  • Andydata.getObject in piecewise mode using execute immediate statement err

    andydata.getObject in piecewise mode using execute immediate statement
    will get ora-00600 arguments:[kopuigpfx1], [14] in Oracle 11.1.0.6 and Oralce 10.2.0.2.
    andydata.getObject in piecewise mode using execute immediate statement err
    andydata.getObject in piecewise mode using execute immediate statement will get ora-00600 arguments:[kopuigpfx1], [14] in Oracle 11.1.0.6 and Oralce 10.2.0.x
    The following are test scripts in HR schema.
    create type ob_test is object(c1 varchar2(10));
    script 1: not using execute immediate statement and works fine.
    DECLARE
    p_anytype anytype;
    p_anydata anydata;
    p_value_1 VARCHAR2(4000);
    p_value_2 ob_test;
    p_result PLS_INTEGER;
    BEGIN
    anytype.begincreate(dbms_types.typecode_object, p_anytype);
    p_anytype.addattr('A1',
    dbms_types.typecode_varchar2,
    NULL,
    NULL,
    4000,
    NULL,
    NULL,
    NULL);
    p_anytype.addattr('A2',
    dbms_types.typecode_object,
    NULL,
    NULL,
    NULL,
    NULL,
    NULL,
    anytype.getpersistent('HR', 'OB_TEST'));
    p_anytype.endcreate();
    anydata.begincreate(p_anytype, p_anydata);
    p_anydata.setvarchar2('abc');
    p_anydata.setobject(OB_TEST('abc'));
    p_anydata.endcreate;
    DECLARE
    p_anydata2 anydata := p_anydata;
    p_value_1 VARCHAR2(4000);
    p_value_2 OB_TEST;
    p_result PLS_INTEGER;
    BEGIN
    p_anydata2.piecewise;
    p_result := p_anydata2.getvarchar2(p_value_1);
    p_result := p_anydata2.getobject(p_value_2);
    END;
    END;
    script 2: using execute immediate statement will get ora-600
    DECLARE
    p_anytype anytype;
    p_anydata anydata;
    p_value_1 VARCHAR2(4000);
    p_value_2 ob_test;
    p_result PLS_INTEGER;
    BEGIN
    anytype.begincreate(dbms_types.typecode_object, p_anytype);
    p_anytype.addattr('A1',
    dbms_types.typecode_varchar2,
    NULL,
    NULL,
    4000,
    NULL,
    NULL,
    NULL);
    p_anytype.addattr('A2',
    dbms_types.typecode_object,
    NULL,
    NULL,
    NULL,
    NULL,
    NULL,
    anytype.getpersistent('HR', 'OB_TEST'));
    p_anytype.endcreate();
    anydata.begincreate(p_anytype, p_anydata);
    p_anydata.setvarchar2('abc');
    p_anydata.setobject(OB_TEST('abc'));
    p_anydata.endcreate;
    EXECUTE IMMEDIATE 'DECLARE' || chr(10) ||
    ' p_anydata2 anydata := :1;' || chr(10) ||
    ' p_value_1 VARCHAR2(4000);' || chr(10) ||
    ' p_value_2 OB_TEST;' || chr(10) ||
    ' p_result PLS_INTEGER;' || chr(10) || 'BEGIN' ||
    chr(10) || ' p_anydata2.piecewise;' || chr(10) ||
    ' p_result := p_anydata2.getvarchar2(p_value_1);' ||
    chr(10) ||
    ' p_result := p_anydata2.getobject(p_value_2);' ||
    chr(10) || 'END;'
    USING p_anydata;
    END;
    script 3: comment statment "p_result := p_anydata2.getobject(p_value_2);", it works ok.
    DECLARE
    p_anytype anytype;
    p_anydata anydata;
    p_value_1 VARCHAR2(4000);
    p_value_2 ob_test;
    p_result PLS_INTEGER;
    BEGIN
    anytype.begincreate(dbms_types.typecode_object, p_anytype);
    p_anytype.addattr('A1',
    dbms_types.typecode_varchar2,
    NULL,
    NULL,
    4000,
    NULL,
    NULL,
    NULL);
    p_anytype.addattr('A2',
    dbms_types.typecode_object,
    NULL,
    NULL,
    NULL,
    NULL,
    NULL,
    anytype.getpersistent('HR', 'OB_TEST'));
    p_anytype.endcreate();
    anydata.begincreate(p_anytype, p_anydata);
    p_anydata.setvarchar2('abc');
    p_anydata.setobject(OB_TEST('abc'));
    p_anydata.endcreate;
    EXECUTE IMMEDIATE 'DECLARE' || chr(10) ||
    ' p_anydata2 anydata := :1;' || chr(10) ||
    ' p_value_1 VARCHAR2(4000);' || chr(10) ||
    ' p_value_2 OB_TEST;' || chr(10) ||
    ' p_result PLS_INTEGER;' || chr(10) || 'BEGIN' ||
    chr(10) || ' p_anydata2.piecewise;' || chr(10) ||
    ' p_result := p_anydata2.getvarchar2(p_value_1);' ||
    chr(10) ||
    ' /*p_result := p_anydata2.getobject(p_value_2);*/' ||
    chr(10) || 'END;'
    USING p_anydata;
    END;
    Could someone tell me how to solve this problem.
    Thanks a lot.
    Daniel

    andydata.getObject in piecewise mode using execute immediate statement
    will get ora-00600 arguments:[kopuigpfx1], [14] in Oracle 11.1.0.6 and Oralce 10.2.0.2.
    andydata.getObject in piecewise mode using execute immediate statement err
    andydata.getObject in piecewise mode using execute immediate statement will get ora-00600 arguments:[kopuigpfx1], [14] in Oracle 11.1.0.6 and Oralce 10.2.0.x
    The following are test scripts in HR schema.
    create type ob_test is object(c1 varchar2(10));
    script 1: not using execute immediate statement and works fine.
    DECLARE
    p_anytype anytype;
    p_anydata anydata;
    p_value_1 VARCHAR2(4000);
    p_value_2 ob_test;
    p_result PLS_INTEGER;
    BEGIN
    anytype.begincreate(dbms_types.typecode_object, p_anytype);
    p_anytype.addattr('A1',
    dbms_types.typecode_varchar2,
    NULL,
    NULL,
    4000,
    NULL,
    NULL,
    NULL);
    p_anytype.addattr('A2',
    dbms_types.typecode_object,
    NULL,
    NULL,
    NULL,
    NULL,
    NULL,
    anytype.getpersistent('HR', 'OB_TEST'));
    p_anytype.endcreate();
    anydata.begincreate(p_anytype, p_anydata);
    p_anydata.setvarchar2('abc');
    p_anydata.setobject(OB_TEST('abc'));
    p_anydata.endcreate;
    DECLARE
    p_anydata2 anydata := p_anydata;
    p_value_1 VARCHAR2(4000);
    p_value_2 OB_TEST;
    p_result PLS_INTEGER;
    BEGIN
    p_anydata2.piecewise;
    p_result := p_anydata2.getvarchar2(p_value_1);
    p_result := p_anydata2.getobject(p_value_2);
    END;
    END;
    script 2: using execute immediate statement will get ora-600
    DECLARE
    p_anytype anytype;
    p_anydata anydata;
    p_value_1 VARCHAR2(4000);
    p_value_2 ob_test;
    p_result PLS_INTEGER;
    BEGIN
    anytype.begincreate(dbms_types.typecode_object, p_anytype);
    p_anytype.addattr('A1',
    dbms_types.typecode_varchar2,
    NULL,
    NULL,
    4000,
    NULL,
    NULL,
    NULL);
    p_anytype.addattr('A2',
    dbms_types.typecode_object,
    NULL,
    NULL,
    NULL,
    NULL,
    NULL,
    anytype.getpersistent('HR', 'OB_TEST'));
    p_anytype.endcreate();
    anydata.begincreate(p_anytype, p_anydata);
    p_anydata.setvarchar2('abc');
    p_anydata.setobject(OB_TEST('abc'));
    p_anydata.endcreate;
    EXECUTE IMMEDIATE 'DECLARE' || chr(10) ||
    ' p_anydata2 anydata := :1;' || chr(10) ||
    ' p_value_1 VARCHAR2(4000);' || chr(10) ||
    ' p_value_2 OB_TEST;' || chr(10) ||
    ' p_result PLS_INTEGER;' || chr(10) || 'BEGIN' ||
    chr(10) || ' p_anydata2.piecewise;' || chr(10) ||
    ' p_result := p_anydata2.getvarchar2(p_value_1);' ||
    chr(10) ||
    ' p_result := p_anydata2.getobject(p_value_2);' ||
    chr(10) || 'END;'
    USING p_anydata;
    END;
    script 3: comment statment "p_result := p_anydata2.getobject(p_value_2);", it works ok.
    DECLARE
    p_anytype anytype;
    p_anydata anydata;
    p_value_1 VARCHAR2(4000);
    p_value_2 ob_test;
    p_result PLS_INTEGER;
    BEGIN
    anytype.begincreate(dbms_types.typecode_object, p_anytype);
    p_anytype.addattr('A1',
    dbms_types.typecode_varchar2,
    NULL,
    NULL,
    4000,
    NULL,
    NULL,
    NULL);
    p_anytype.addattr('A2',
    dbms_types.typecode_object,
    NULL,
    NULL,
    NULL,
    NULL,
    NULL,
    anytype.getpersistent('HR', 'OB_TEST'));
    p_anytype.endcreate();
    anydata.begincreate(p_anytype, p_anydata);
    p_anydata.setvarchar2('abc');
    p_anydata.setobject(OB_TEST('abc'));
    p_anydata.endcreate;
    EXECUTE IMMEDIATE 'DECLARE' || chr(10) ||
    ' p_anydata2 anydata := :1;' || chr(10) ||
    ' p_value_1 VARCHAR2(4000);' || chr(10) ||
    ' p_value_2 OB_TEST;' || chr(10) ||
    ' p_result PLS_INTEGER;' || chr(10) || 'BEGIN' ||
    chr(10) || ' p_anydata2.piecewise;' || chr(10) ||
    ' p_result := p_anydata2.getvarchar2(p_value_1);' ||
    chr(10) ||
    ' /*p_result := p_anydata2.getobject(p_value_2);*/' ||
    chr(10) || 'END;'
    USING p_anydata;
    END;
    Could someone tell me how to solve this problem.
    Thanks a lot.
    Daniel

  • Execute a DML query its length exceeds 4000 characters with execute immediate statement.

    I want to execute a DML query with execute immediate statement. That DML query length exceeds 4000 characters. This query has Xquery related conditions, i can not split the query. when i tried execute it is giving "string literal too long".  I tried with DBMS_SQL.Parse() and DBMS_SQL.Execute also, but it is giving same error. I have to execute this DML query inside a Procedure. Please help me to resolve this. We are using oracle 10g version
    Thanks & Regards,
    K.Kedarnadh

    Actually Query is a dynamic query. Query length will exceeds if the no of domains\domain values \products exceeds. Any way Below one is current dynamic query, which is generated within procedure
    SELECT
      IVT.ID_IVT
        ,IVT.ID_INS_IVT
      ,EXTRACTVALUE(IVT.DOCUMENT_IVT,'//productName/text()') AS PRODUCTNAME
      ,EXTRACTVALUE(IVT.DOCUMENT_IVT,'//elementName/text()') AS INSTANCENAME
      ,EXTRACTVALUE(IVT.DOCUMENT_IVT,'//elementInternalName/text()') AS INTERNALNAME
        ,CTG.NAME_CTG
        ,CTR.NAME_CTR
        ,MDL.NAME_MDL
      ,IVT.REEDIT_FLAG_IVT
        FROM  VARIATION_IVT IVT INNER JOIN CATEGORY_CTG CTG ON CTG.ID_CTG=IVT.ID_CTG_IVT
      AND IVT.STATUS_IVT='Active' AND IVT.DELETE_FLAG_IVT=0 AND IVT.ID_PRJ_IVT=1
      AND  IVT.DOCUMENT_IVT.existsnode('.//domain[domainName="Jurisdictions" and (domainValue="Delhi" or domainValue="Bangladesh" or domainValue="Mumbai" or domainValue="India" or domainValue="Pakistan" or domainValue="Nepal" or domainValue="Maldives" or domainValue="Kolkata" or domainValue="Bhutan" or domainValue="Chennai" or domainValue="ALL")]')=1 AND  IVT.DOCUMENT_IVT.existsnode('.//domain[domainName="Channels" and (domainValue="Agents" or domainValue="SBI" or domainValue="Maruti" or domainValue="Direct" or domainValue="CitiFinancial" or domainValue="SCB" or domainValue="BankAssurance" or domainValue="CitiBank" or domainValue="Employees" or domainValue="GE" or domainValue="Brokers" or domainValue="Telemarketing" or domainValue="Agency" or domainValue="ALL")]')=1 AND  IVT.DOCUMENT_IVT.existsnode('.//domain[domainName="ModeofDelivery" and (domainValue="Walkin" or domainValue="Internet" or domainValue="ALL")]')=1 AND  IVT.DOCUMENT_IVT.existsnode('.//context[(productName="ALL" or productName="A009" or productName="A010" or productName="A046" or productName="AccidentShieldClassic" or productName="AccidentShieldOnline" or productName="AM01" or productName="AM02" or productName="AME_Cancellation" or productName="ARHG" or productName="ARPA" or productName="B003" or productName="B004" or productName="B007" or productName="B008" or productName="B009" or productName="B010" or productName="B012" or productName="B013" or productName="B015" or productName="B016" or productName="B017" or productName="BC04_PA" or productName="BC06_FDP" or productName="BC06_PA" or productName="BC09" or productName="BC10" or productName="BC12" or productName="BC13" or productName="BF03" or productName="BS01" or productName="BS02" or productName="C017" or productName="C035" or productName="C036" or productName="C037" or productName="C038" or productName="C040" or productName="C041" or productName="C041Gold" or productName="C041New" or productName="C045HomeContents" or productName="C048" or productName="C049" or productName="C054" or productName="C057" or productName="C060Building" or productName="C060Contents" or productName="C060FDP" or productName="C061Building" or productName="C061Contents" or productName="C062" or productName="C063" or productName="C067" or productName="C070" or productName="C072" or productName="C074" or productName="C077" or productName="C081" or productName="C082" or productName="C087" or productName="C088" or productName="CITIFOREVER" or productName="CITISECURE" or productName="CITICHILDPLAN" or productName="D001" or productName="DB01" or productName="DD01" or productName="DD02" or productName="DD03" or productName="DD04" or productName="DD09" or productName="DD10" or productName="E005" or productName="E011" or productName="E016" or productName="E020" or productName="E030" or productName="E034" or productName="E040" or productName="E041" or productName="E045HCP" or productName="E045HSP" or productName="E049" or productName="E049New" or productName="E052" or productName="E053" or productName="E054FDP" or productName="E055" or productName="E056" or productName="E057" or productName="E058" or productName="E061" or productName="E061BATCH" or productName="E062" or productName="E063" or productName="E064HCP" or productName="E064HSP" or productName="E066" or productName="E069" or productName="E073" or productName="E075" or productName="E076" or productName="E088" or productName="E090" or productName="E093A" or productName="E093B" or productName="E095" or productName="E099A" or productName="E099B" or productName="E106" or productName="E107" or productName="E110" or productName="E112" or productName="E114" or productName="E115" or productName="E116" or productName="F001" or productName="FamilyHealthInsurance" or productName="FamilyHospitalBenefits" or productName="FamilyHospitalisationCoverBenefit" or productName="G001" or productName="G002" or productName="HealthShieldOnline" or productName="Health_B005" or productName="Health_S057" or productName="HealthSheild" or productName="HealthWalkin" or productName="HomeContentOnline" or productName="HomeShieldOnline" or productName="HomeShieldWalkin" or productName="HospitalCashOnline" or productName="J001" or productName="J008" or productName="K001" or productName="KV02" or productName="LC03" or productName="ML01" or productName="MP02" or productName="MP03" or productName="MR01" or productName="O005" or productName="PO01" or productName="PO02" or productName="PO03" or productName="PO04" or productName="PO05" or productName="PO06" or productName="RR02" or productName="RR03" or productName="RR04" or productName="S006" or productName="S033" or productName="S049" or productName="S051" or productName="S054" or productName="S057" or productName="S060" or productName="S061" or productName="S065" or productName="S065TM" or productName="S068" or productName="S076" or productName="S077" or productName="S079" or productName="S080" or productName="S081" or productName="S084" or productName="S085" or productName="S086" or productName="S087" or productName="S088" or productName="S091" or productName="S092" or productName="S093" or productName="S094" or productName="S095" or productName="S097" or productName="S098" or productName="S099" or productName="S100" or productName="S101" or productName="S102" or productName="S103" or productName="S104" or productName="S106" or productName="S107" or productName="S108" or productName="S109" or productName="S110" or productName="S111" or productName="S113" or productName="SCBNAC" or productName="SF02" or productName="SS01" or productName="SS02" or productName="SUNFHM" or productName="SurgicalShield" or productName="TD01" or productName="TD02" or productName="TP01" or productName="U002Building" or productName="U002Contents" or productName="U004Building" or productName="U007" or productName="U009" or productName="U013" or productName="U014" or productName="U015" or productName="U016" or productName="V001" or productName="V002" or productName="V005" or productName="V006" or productName="V008" or productName="W008" or productName="W020" or productName="W021" or productName="W022" or productName="W023" or productName="W024" or productName="W026" or productName="W027" or productName="W028" or productName="W029" or productName="W105" or productName="W106" or productName="WI01" or productName="WI02" or productName="WI03" or productName="WI07" or productName="WI08" or productName="WI09" or productName="WI10" or productName="WI11" or productName="WI12" or productName="WI13" or productName="WI14" or productName="WI17" or productName="WI20" or productName="WI21" or productName="WI21_Health" or productName="WI23" or productName="WI24" or productName="WI26" or productName="WI30" or productName="WI31" or productName="WI33" or productName="WI34" or productName="X001" or productName="X002" or productName="X003" or productName="X004" or productName="X005" or productName="X008" or productName="Y001" or productName="Y007" or productName="Y009" or productName="Y010" or productName="Y011" or productName="Y011H" or productName="Y020" or productName="Y020N" or productName="Z008" or productName="ZI001")]')=1
      INNER JOIN CENTER_CTR CTR ON CTR.ID_CTR=CTG.ID_CTR_CTG
        INNER JOIN MODEL_MDL MDL ON  MDL.ID_MDL=CTR.ID_MDL_CTR

  • How to insert String variables in Execute Immediate Statement

    Hi,
    I have query
    dept varchar2(10);
    dept :='electronics'
    l_dyn_sql_input600_cbs := 'select EmployeeName,AGE,' || dept ||' from Employee'
    EXECUTE immediate l_dyn_sql_input600_cbs;
    when i run the query it says "electonics" Invalid identifier.
    Can anyone help to rectify the query

    What are you actually trying to do here:
    - Select employee name, age and the string literal 'electronics' from the employee table?
    - Select employee name, age and the dept field from the employee table?
    - Select employee name, age and dept from the employee table where dept = 'electronics'?
    - Something else...?
    Doesn't seem to make much sense as it is right now...
    cheers,
    Anthony

  • How can i have '&' in EXECUTE IMMEDIATE statement?

    I have PL/SQL procedure, where i will be inserting data to a table. Data which i am inserting is having a '&' value in it. If i use the below statement for insert then sqlplus is prompting for the value '&' like a substitution value while compile this procedure.
    EXECUTE IMMEDIATE 'INSERT INTO TABLE_TEST VALUES (:1,:2)' USING 'root/direct/&text', SYSDATE;If i replace the '&' by assigning this value to VARCHAR field then it is working properly. But when i populate an xml from this table using DBMS.XMLGEN, then the value '&' is not entity escaped in the XML which is normally replaced with '&' in XML.
    Please help.

    Vel wrote:
    i can't go by both ways because i am using '&' in various files to get some values. Also in this package i am using the '&' to get another substitution variable. Is there any other workaround possible?I prefer not to mess with server-side SQL and PL/SQL code to fix, or work around, a problem on the client-side. Not a very sensible thing IMO.
    In such a case as you've described, I would set the substitution character to a character that I'm not using in server-side code. I have done this a couple of times in the past for db create and installation scripts. Oracle uses the same approach for their Apex installation scripts.
    I think Oracle uses the +#+ character for their Apex install scripts. E.g.
    set define '#'I think I used the tilde (char <i>~</i>) at a stage as substitution char as it is very seldom used in PL/SQL and SQL code.
    The bottom line is that you will need to change your code. Set define off and on where needed to ensure substitution only happens where needed. Set the substitution char to a different char and update your substitution variables. Or hack your server-side code (as suggested above) and use the char() function instead.

  • Oracle 11G Copying a table using execute immediate

    I want to copy the contents of one table into another using execute immediate.
    This keeps failing with the following error
    ORA-00903: invalid table name
    ORA-06512: at "TABLE_COPY", line 6
    ORA-06512: at line 8
    create or replace
    procedure TABLE_COPY(
    TABLE1 varchar2,
    TABLE2 varchar2)
    is
    begin
    execute immediate 'insert into '||TABLE2||' (select * from '||TABLE1||')';
    end;
    Edited by: user9213000 on 24-Jan-2013 07:38

    user9213000 wrote:
    I want to copy the contents of one table into another using execute immediate.
    This keeps failing with the following error
    ORA-00903: invalid table name
    ORA-06512: at "TABLE_COPY", line 6
    ORA-06512: at line 8
    create or replace
    procedure TABLE_COPY(
    TABLE1 varchar2,
    TABLE2 varchar2)
    is
    begin
    execute immediate 'insert into '||TABLE2||' (select * from '||TABLE1||')';
    end;
    Edited by: user9213000 on 24-Jan-2013 07:38The standard advice when (ab)using EXECUTE IMMEDIATE is to compose the SQL statement in a single VARCHAR2 variable
    Then print the variable before passing it to EXECUTE IMMEDIATE.
    COPY the statement & PASTE into sqlplus to validate its correctness.

  • 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.

Maybe you are looking for