Execute immediate return

Hi,
I want to know if it's possible to use the return clause with the insert statement with EXECUTE IMMEDIATE.
My code looks like:
execute immediate('INSERT INTO '||p_nameDB||'.TableName (col1) VALUES (:1)
     ') using p_col1;
The id is generated automatically with a trigger (and a sequence). And I want to recuperate it in a variable after the insert statement. I don't want to do a seq.currval after because i'm afraid that there will be a mix match with the sequence and the id if 2 persons accede at the same time.
thanks

execute immediate 'insert into trial (a) values (:1) returning a into :2' using a1 returning into x ;

Similar Messages

  • EXECUTE IMMEDIATE dynamic statement and return procedure value

    Hello guys,
    How can i return values from procedure using EXECUTE IMMEDIATE statment?
    I made easy example:
    CREATE OR REPLACE PACKAGE pac_test
    IS
    PROCEDURE pro_calc_average( p_age_1 IN NUMBER,
    p_age_2 IN NUMBER,
    p_age_3 IN NUMBER,
    v_out OUT NUMBER);
    PROCEDURE pro_calc(p_age_1 IN NUMBER,
    p_age_2 IN NUMBER,
    p_age_3 IN NUMBER);
    END;
    CREATE OR REPLACE PACKAGE BODY pac_test
    IS
    PROCEDURE pro_calc_average( p_age_1 IN NUMBER,
    p_age_2 IN NUMBER,
    p_age_3 IN NUMBER,
    v_out OUT NUMBER)
    IS
    BEGIN
    v_out:=(p_age_1+p_age_2+p_age_3)/3;
    END pro_calc_average;
    PROCEDURE pro_calc(p_age_1 IN NUMBER,
    p_age_2 IN NUMBER,
    p_age_3 IN NUMBER)
    IS
    x number;
    v_sql varchar2(4000);
    BEGIN
    v_sql:='pac_test.pro_calc_average('||p_age_1||','||p_age_2||','||p_age_3||',x)';
    dbms_output.put_line(v_sql);
    EXECUTE IMMEDIATE v_sql;
    dbms_output.put_line(' ====> '||x);
    END pro_calc;
    END;
    -- Run procedures [Faild]
    EXEC pac_test.pro_calc(2,9,19);
    When i run:
    DECLARE
    x number;
    BEGIN
    pac_test.pro_calc_average(2,9,9,x);
    dbms_output.put_line(' ====> '||x);
    END;
    It's works, but this is not what i am looking for.
    Thank you guys,
    Sam.

    Hi Sam,
    Like this?
    CREATE OR REPLACE PACKAGE pac_test
    IS
    pac_var number;  /* added new*/
    PROCEDURE pro_calc_average( p_age_1 IN NUMBER,
    p_age_2 IN NUMBER,
    p_age_3 IN NUMBER,
    v_out OUT NUMBER);
    PROCEDURE pro_calc(p_age_1 IN NUMBER,
    p_age_2 IN NUMBER,
    p_age_3 IN NUMBER);
    END;
    CREATE OR REPLACE PACKAGE BODY pac_test
    IS
    PROCEDURE pro_calc_average( p_age_1 IN NUMBER,
    p_age_2 IN NUMBER,
    p_age_3 IN NUMBER,
    v_out OUT NUMBER)
    IS
    BEGIN
    v_out:=(p_age_1+p_age_2+p_age_3)/3;
    END pro_calc_average;
    PROCEDURE pro_calc(p_age_1 IN NUMBER,
    p_age_2 IN NUMBER,
    p_age_3 IN NUMBER)
    IS
    pack_local_var number;
    v_sql varchar2(4000);
    BEGIN
    --v_sql:='pac_test.pro_calc_average('||p_age_1||','||p_age_2||','||p_age_3||',x)';
    v_sql:=' declare x number; begin pac_test.pro_calc_average('||p_age_1||','||p_age_2||','||p_age_3||',x);
    dbms_output.put_line(x);
    pac_test.pac_var:=x; /* added new*/
    end;';
    dbms_output.put_line(v_sql);
    EXECUTE IMMEDIATE v_sql;
    pack_local_var:=pac_var; /* added new*/
    dbms_output.put_line(pack_local_var); /* added new*/
    END pro_calc;
    END;Declared a package variable.
    But be aware this variable is accessible to everyone and they can read or change its value if they have the right privileges.
    REgards,
    Bhushan

  • Obtaining a collection as a return from an execute immediate pl/sql block

    version 10.2
    I need to obtain the collection back from the execute immediate of a pl/sql block:
    procedure block(owner varchar2) is
    stmt                   long;
    objecttab_coll         dbms_stats.objecttab;
    begin
    stmt := '
       begin
        dbms_stats.gather_schema_stats(''' || owner || '''
         ,options => ''LIST AUTO''
         ,objlist => :objecttab_coll
       end;'; 
    execute immediate stmt returning into objecttab_coll;
    -- do more stuff here
    end block;I have tried this + a few variations but with no luck. In looking through the docs I do not see an example. can this be done?
    Thanks
    Ox

    I dont find any need for an execute immediate here. This must be just enough.
    procedure block(owner varchar2)
    is
         objecttab_coll         dbms_stats.objecttab;
    begin
         dbms_stats.gather_schema_stats(ownname => owner, options => 'LIST AUTO', objlist => objecttab_coll);
         -- do more stuff here
    end block;Thanks,
    Karthick.

  • Dyn sql execute immediate  A plsql block returning sql%rowcount of rows inserted

    I would like to get the number of rows inserted in a dynamic sql statement like following.
    execute immediate
    'begin insert into a(c1,c2,c3)
    (select ac1,ac2,ac3 from ac where ac1=:thekey
    UNION select tc1,tc2,tc3 from tc where tc1=:otherkey); end;' using in key1,in key2;
    I have tried per the oracle8i dyn sql chapter in doc RETURN sql%rowcount won't compile.
    also add this to statement 'returning sql%rowcount into :rowcount'
    no luck.
    any ideas.?
    thanks.

    Quick comment first - I'm not sure why you are even using dynamic SQL here. There is nothing dynamic about your statement. It is equivalent to just:
    insert into a (c1, c2, c3)
      select ac1, ac2, ac3
        from ac
       where ac1 = key1
      UNION
      select tc1, tc2, tc3
        from tc
       where tc1 = key2;Unless you are really dynamically changing a table or column name here and just didn't show it in your example, you certainly don't want the overhead of NDS for this insert in this situation.
    In any case, you just need to evaluate SQL%ROWCOUNT in the next statement.
    insert ...;
    if sql%rowcount = 0 then
      -- nothing was inserted
    end if;

  • How to use Execute Immediate to execute a procedure and return a cursor?

    The procedure name is unknown until run time so I have to use Execute immediate to execute the procedure, the procedure return a cursor, but I can't figure out the right syntax to pass the cursor out.
    To simplify the issue here, I create two procedures as examples.Assume I have a procedure called XDTest:
         p_cur OUT SYS_REFCURSOR
    IS
    BEGIN
    OPEN p_cur FOR
    Select * from dummy_table;
    END XDTest;
    In another procedure, I want to use Execute Immediate to execute XDTest and obtain the result that return from the cursor into a local cursor so I can process the records:
         p_cur OUT SYS_REFCURSOR
    IS
    l_cur SYS_REFCURSOR;
    BEGIN
    execute immediate 'BEGIN XDTest (:1); END;' using OUT l_cur;
    END XDTest2;
    However, this is not working, I get ORA-03113: end-of-file on communication channel error.
    What syntax should I use here?
    Cheers

    well...
    I update the XDTest2 procedure as below but when execute it get exceptions.I think the v_sqlstmt syntax is wrong, but I don't know what meant to be correct, please give some suggestions .
    Cheers
    -------------------- XDTest procedure --------------------------------------------------
         p_cur OUT SYS_REFCURSOR
    --AUTHID CURRENT_USER
    IS
    BEGIN
    OPEN p_cur FOR
    Select Table_Name from USER_Tables;
    END XDTest;
    -------------------- XDTest2 procedure --------------------------------------------------
         p_cur OUT SYS_REFCURSOR
    IS
    TYPE T1 IS TABLE OF VARCHAR2(4000) INDEX BY BINARY_INTEGER ;
    v_sqlstmt varchar2(1000):=null;
    v_cursor number;
    x_num number;
    LN$Lig number := 0 ;
    LN$MaxCol number := 0 ;
    t T1 ;
    v VARCHAR2(4000) ;
    userTb User_Tables%ROWTYPE;
    l_cur number;
    BEGIN
    LN$Lig := 0 ;
    LN$MaxCol := 1 ;
    --OPEN p_cur FOR
    --execute immediate 'BEGIN XDTest (:1); END;' using OUT l_cur;
    v_cursor:=dbms_sql.open_cursor;
    v_sqlstmt:='begin :p_cur:="XDTest"(); END; ';
    dbms_sql.parse(v_cursor,v_sqlstmt,DBMS_SQL.NATIVE);
    dbms_sql.bind_variable(v_cursor,':p_cur',l_cur);
    dbms_output.put_line('1');
    -- Define the columns --
    dbms_sql.DEFINE_COLUMN(v_cursor, 1, userTb.Table_Name, 30);
    x_num:=dbms_sql.execute(v_cursor);
    dbms_output.put_line('2');
    -- Fetch the rows from the source query --
    LOOP
    IF dbms_sql.FETCH_ROWS(v_cursor)>0 THEN
    -- get column values of the row --
    dbms_sql.COLUMN_VALUE(v_cursor, 1,userTb.Table_Name);
    dbms_output.put_line(userTb.Table_Name);
    ELSE
    -- No more rows --
    EXIT;
    END IF;
    END LOOP;
    dbms_sql.close_cursor(v_cursor);
    END XDTest2;
    ---------------------- Error when execute ------------------------------------------------
    1
    BEGIN DevD0DB.XDTest2(:l_cur); END;
    ERROR at line 1:
    ORA-01007: variable not in select list
    ORA-06512: at "SYS.DBMS_SYS_SQL", line 1010
    ORA-06512: at "SYS.DBMS_SQL", line 212
    ORA-06512: at "DEVD0DB.XDTEST2", line 35
    ORA-06512: at line 1
    ----------------------------------------------------------------------------------------------------

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

  • EXECUTE IMMEDIATE with an update FUNCTION returning VARCHAR2

    I need to execute immediate a stored Function (that updates the database) and returns a VARCHAR2 value.
    What does my execute immediate statement look like:
    EXECUTE IMMEDIATE 'BEGIN my_function_call; END;' RETURNING INTO return_variable;

    A set of procedures would be far preferable to a set of functions if you're doing DML.
    Additionally, I'd get rid of the status value entirely and just raise an exception if there is an error. It's a heck of a lot easier to catch (or propagate) exceptions correctly than to ensure that each and every piece of code that calls these procedures checks the return code and acts appropriately. It's way too easy to miss/ hide an error in a missed return code.
    Justin

  • Error while insert data using execute immediate in dynamic table in oracle

    Error while insert data using execute immediate in dynamic table created in oracle 11g .
    first the dynamic nested table (op_sample) was created using the executed immediate...
    object is
    CREATE OR REPLACE TYPE ASI.sub_mark AS OBJECT (
    mark1 number,
    mark2 number
    t_sub_mark is a class of type sub_mark
    CREATE OR REPLACE TYPE ASI.t_sub_mark is table of sub_mark;
    create table sam1(id number,name varchar2(30));
    nested table is created below:
    begin
    EXECUTE IMMEDIATE ' create table '||op_sample||'
    (id number,name varchar2(30),subject_obj t_sub_mark) nested table subject_obj store as nest_tab return as value';
    end;
    now data from sam1 table and object (subject_obj) are inserted into the dynamic table
    declare
    subject_obj t_sub_mark;
    begin
    subject_obj:= t_sub_mark();
    EXECUTE IMMEDIATE 'insert into op_sample (select id,name,subject_obj from sam1) ';
    end;
    and got the below error:
    ORA-00904: "SUBJECT_OBJ": invalid identifier
    ORA-06512: at line 7
    then when we tried to insert the data into the dynam_table with the subject_marks object as null,we received the following error..
    execute immediate 'insert into '||dynam_table ||'
    (SELECT

    887684 wrote:
    ORA-00904: "SUBJECT_OBJ": invalid identifier
    ORA-06512: at line 7The problem is that your variable subject_obj is not in scope inside the dynamic SQL you are building. The SQL engine does not know your PL/SQL variable, so it tries to find a column named SUBJECT_OBJ in your SAM1 table.
    If you need to use dynamic SQL for this, then you must bind the variable. Something like this:
    EXECUTE IMMEDIATE 'insert into op_sample (select id,name,:bind_subject_obj from sam1) ' USING subject_obj;Alternatively you might figure out to use static SQL rather than dynamic SQL (if possible for your project.) In static SQL the PL/SQL engine binds the variables for you automatically.

  • Java Stored Procedure in EXECUTE IMMEDIATE

    Hi,
    I need advice for the following.
    I'm on Oracle 11g R2. I'm testing application in Oracle 11gR1 and R2 and Oracle Express.
    Purpose is to generate XML reports.
    I have PLSQL Stored Procedure which does that, but since there is bug in Oracle11gR2 related to XMLTRANSFORM I have and Java Stored Procedure which is workaround. They are both compiled, valid etc.
    Java class is :
    import java.io.PrintWriter;
    import java.io.Writer;
    import oracle.xml.parser.v2.DOMParser;
    import oracle.xml.parser.v2.XMLDocument;
    import oracle.xml.parser.v2.XSLProcessor;
    import oracle.xml.parser.v2.XSLStylesheet;
    * This class is used as Java stored procedure
    * There is a bug on Oracle11gR2, related to the limitation on the number of style sheet instructions
    * This stored procedure is workaround when PLSQL code can not be used.
    * File must not have package, otherwise is wrongly compiled in DB
    public class JavaXslt {
         public static void XMLTtransform(oracle.sql.CLOB xmlInput,oracle.sql.CLOB xslInput,oracle.sql.CLOB output) throws Exception{
              DOMParser parser;
              XMLDocument xml;
              XMLDocument xsldoc;
              try{
                   parser = new DOMParser();
                   parser.parse(xmlInput.getCharacterStream());
                   xml = parser.getDocument();
                   parser.parse(xslInput.getCharacterStream());
                   xsldoc = parser.getDocument();
                   XSLProcessor processor = new XSLProcessor();
                   XSLStylesheet xsl = processor.newXSLStylesheet(xsldoc);
                   Writer w = output.setCharacterStream(1L);
                   PrintWriter pw = new PrintWriter(w);
                   processor.processXSL(xsl, xml, pw);
              }catch (Exception ex){
                   throw ex;
    PROCEDURE Java_XmlTransform (xml CLOB, xslt CLOB, output CLOB) AS LANGUAGE JAVA
    NAME 'JavaXslt.XMLTtransform(oracle.sql.CLOB, oracle.sql.CLOB, oracle.sql.CLOB)';
    I'm calling Java stored procedure from PLSQL Stored procedure (if it is Oracle11gR2) like that :
    Java_Proc.Java_XmlTransform(inputXML, xslt, res);
    So till here everything works ok. XSLT as applied and output XML (res) is OK.
    But when Oracle Express is used Java is out of the question, so there is no Java stored procedure. Howewer PLSQL Stored procedure is still needed.
    So I had to put call to Java Stored procedure in EXECUTE IMMEDIATE statement in order to compile to PLSQL package.
    But when I do that :
    EXECUTE IMMEDIATE 'BEGIN Java_Proc.Java_XmlTransform (:1, :2, :3); END;' USING inputXML, xslt, res;
    result value CLOB (res) has zero length...
    What am I missing? Should i set return value to Java class?
    Hope my explanations are clear though.
    Thanks

    Hi odie_63,
    Thanks for quick response.
    I didn't clearly explained.
    When using Oracle 11gR1 and Oracle Express I'm using only PLSQL Procedure.
    When using Oracle 11gR2 i have to use Java Stored procedure because there is documented bug in R2.
    That's why i have to use EXECUTE IMMEDIATE. I don't know which version is the client DB and whether there is or no Java procedures.
    I did tried
    EXECUTE IMMEDIATE 'BEGIN Java_Proc.Java_XmlTransform (:1, :2, :3); END;' USING IN inputXML, IN xslt, OUT res; and the result was ORA-06537: OUT bind variable bound to an IN position
    When using IN OUT for last parameter i.e.
    EXECUTE IMMEDIATE 'BEGIN Java_Proc.Java_XmlTransform (:1, :2, :3); END;' USING IN inputXML, IN xslt, IN OUT res;
    there is no exception, but still DBMS_LOB.getlength(res) = 0
    Thanks

  • Execute immediate with using clause to pass column name dynamically

    Hai,
    Is there any way using execute immeidate to pass the column name dynamically. I used to pass the column value as dynamic with the help of "Using clause" . But if i use to pass column name, it is giving numberic error at run time. Eg,. for testing has been given below.
    1. Column value as dynamic, which is working correctly.
    create or replace function testexeimm (acctnum char)
    return number as
    acctbal number;
    begin
    execute immediate 'select balance from acct_master where acct_no=:a' into acctbal using acctnum;
    return acctbal;
    end;
    2. Column name as dynamic which is not working
    create or replace function testexeimm (colnam char)
    return char as
    acctbal char;
    begin
    execute immediate 'select :a from ch_acct_mast where rownum=1' into acctbal using colnam;
    return acctbal;
    end;
    Any help in this regard will be highly appericated.
    Regards
    Sridhar

    So the variable has to be numeric too:
    create or replace function testexeimm (colnam char)
    return number as
    acctbal number;
    begin
    execute immediate 'select '|||colnam||' from ch_acct_mast where rownum=1' into acctbal;
    return acctbal;
    end;Max
    http://oracleitalia.wordpress.com

  • Using EXECUTE IMMEDIATE with an NVARCHAR2 parameter

    Hi everyone,
    In the system I'm working on, my stored procedure receives an NVARCHAR2 parameter which contains a complete SQL statement. e.g.
    'UPDATE SomeTable SET SomeTable.Value = 0'
    I want to be able to execute this dynamically, using EXECUTE IMMEDIATE, but I've encountered a problem where this is apparently not supported (because the string is Unicode).
    It is crucial that the string stays as Unicode, because of the data that may possible be baked into the SQL statement, and so casting it off to a VARCHAR2, or changing the parameter type, is not acceptable.
    Is there a work-around for this issue? Even if it has a performance hit, it will be better than nothing :)
    I've tried something like this;
    declare
    myVal VARCHAR2(256);
    begin
    myVal := 'UPDATE SomeTable SET SomeTable.Value = 2';
    execute immediate 'BEGIN :x; END;' using myVal;
    end;
    But I get an error;
    "bind variable 'X' not allowed in this context"
    Has anyone any ideas?
    Thanks for your help,
    Chris

    uhmm, I'm not sure if this is a valid testcase. the string could still be converted into ascii or we8iso8859p1 or whatever 1byte-character set you want.
    What I think you should test is a two-byte character as part of SQL, PL/SQL resp. as in SQL all words are defined you can not enlarge it with a 2byte word. But in PL/SQL you could define your own variable. If this variable name itself contains a 2byte character, I guess it will fail.
    (I have used l_vär and l_vér as variable names in my example)
    Message was edited by:
    Leo Mannhart
    According to the PL/SQL Reference Guide PL/SQL consists of:
    PL/SQL programs are written as lines of text using a specific set of characters:
    Upper- and lower-case letters A .. Z and a .. z
    Numerals 0 .. 9
    Symbols ( ) + - * / < > = ! ~ ^ ; : . ' @ % , " # $ & _ | { } ? [ ]
    Tabs, spaces, and carriage returns
    This seems no longer up-to-date as I could define a variable l_vär for instance as long as I use a 1byte char set (like iso8859p1). The same variable name as a n-byte char set like UTF-8 will give an error as my example shows.

  • Execute immediate for stored procedure with out parameter

    Hi,
    I have problem with dynamically executing the statement hope anyone can help me.
    I have a table which stores the procedure names. and procedure parameter values are stored on another column with parameter values coming from java side.
    I have to create a procedure that dynamically executes the procedure on table1 with the values from table 2.
    Now I'm getting real trouble to execute immediate this proc with parameters. I tried the DBMS_SQL package as well.
    Problem is you need to mention the OUT mode specifically for the out parameter. Can anybody plz help me with this issue??
    TABLE1_
    PROCESS_ID     PROC_NAME
    1      proc1(p1 IN number, p2 IN varchar2, p3 OUT varchar2)
    2     proc2(p1 IN number, p2 out varchar2, p3 OUT varchar2)
    TABLE2_
    PROCESS_ID     PROC_PARMS
    1     100, 'test', :return
    2     200, :return1, :return2
    Thank You

    826957 wrote:
    Hi,
    I have problem with dynamically executing the statement hope anyone can help me.
    I have a table which stores the procedure names. and procedure parameter values are stored on another column with parameter values coming from java side.
    I have to create a procedure that dynamically executes the procedure on table1 with the values from table 2.
    Now I'm getting real trouble to execute immediate this proc with parameters. I tried the DBMS_SQL package as well.
    Problem is you need to mention the OUT mode specifically for the out parameter. Can anybody plz help me with this issue??
    TABLE1_
    PROCESS_ID     PROC_NAME
    1      proc1(p1 IN number, p2 IN varchar2, p3 OUT varchar2)
    2     proc2(p1 IN number, p2 out varchar2, p3 OUT varchar2)
    TABLE2_
    PROCESS_ID     PROC_PARMS
    1     100, 'test', :return
    2     200, :return1, :return2
    Thank YouSounds like an appalling design and a nightmare waiting to happen.
    Why not have your Java just call the correct procedures directly?
    Such design smells badly of an entity attribute value modelling style of coding. Notoriously slow, notoriously buggy, notoriously hard to maintain, notoriously hard to read. It really shouldn't be done like that.

  • Need suggestion on PLSQL Create and EXECUTE IMMEDIATE

    Most of you already know plsql doesn't like create table, so we have to use EXECUTE IMMEDIATE for creating table. What if I want to see if the table exist, if not then create the table, later I will insert data into that table.
    My problem is it returned me the error saying I am try to insert into a non existing table when trying to compile my code. I think plsql doesn't pick up the execute statement.
    what I did is, both create and insert are executed by using EXECUTE IMMEDIATE. Anyone have such experience before and willing to share your knowledge?
    PS: I am having same problem for creating sequence as well.

    I think plsql doesn't pick up the execute statement.Since it is a runtime instruction, it will pick it up at runtime. but to be able to run, it needs to compile the code and in order to compile (so it can run) the code it needs that table/sequence you are referencing to exist already. So, you need to run the code to get the table and run needs to compile the code and compile needs the table to compile. can't go from here to there when you try to mix dynamic sql with static sql on the same object within the same program unit (or dependent units).

  • Execute immediate of Anonymous pl/sql block stored in a VARCHAR2 DB Column

    Hi Guys,
    I really hope someone can help me with this.
    I have the following pl/sql anonymous block stored in a varchar2 database column.
    declare
        l_my_val varchar2(32767);
                cursor c_activity is
                SELECT
                      l.DESCRIPTION || decode(l2.DESCRIPTION,null,'',l2.description,  '-' || l2.description) || decode(a.DT,'Y',' - Distributed Training','N',null,null) as value1
                FROM   ACTIVITY a
                      ,MOUNTAINEERING m
                      ,LOV l
                      ,LOV l2
                WHERE  a.JSATFA_ID = 82
                AND    a.SPECIFIC_ACTIVITY_LOV_ID = l.LOV_ID
                AND    m.ACTIVITY_ID(+) = a.ACTIVITY_ID
                AND    m.CLASSIFICATION_LOV_ID = l2.LOV_ID(+);
    begin
    for each_row in c_activity loop
      l_my_val := l_my_val ||  ', ' || each_row.value1;
    end loop;
    :l_value := ltrim (l_my_val, ', ');
    end;  The code is select out of the database and assigned to a local variable within my pl/sql package. The following code should demonstrate what I'm trying to achieve:
    declare
      l_sql varchar2(32767);
      l_value varchar2(32767);
    begin
      select query_sql into l_sql from lov where lov_id = 100;
      execute immediate l_sql using out :l_value;
      dbms_output.put_line(l_value);
    end;However Oracle (10.2.0.2) seems to be doing something funny with the single quotes. and gives the following error:
    ORA-06550: line 1, column 1:
    PLS-00103: Encountered the symbol "" when expecting one of the following:
       begin case declare exit for function goto if loop mod null
       package pragma procedure raise return select separate type
       update while with <an identifier>
       <a double-quoted delimited-identifier> <a bind variable> <<
       form table call close current define delete fetch lock insert
       open rollback savepoint set sql execute commit forall merge
       library OPERATOR_ pipe
    errorIf I embed the code I'm trying to execute within the package i.e
    declare
      l_sql varchar2(32767);
      l_value varchar2(32767);
    begin
      l_sql := q'~declare
        l_my_val varchar2(32767);
                cursor c_activity is
                SELECT
                      l.DESCRIPTION || decode(l2.DESCRIPTION,null,'',l2.description,  '-' || l2.description) || decode(a.DT,'Y',' - Distributed Training','N',null,null) as value1
                FROM   ACTIVITY a
                      ,MOUNTAINEERING m
                      ,LOV l
                      ,LOV l2
                WHERE  a.JSATFA_ID = 82
                AND    a.SPECIFIC_ACTIVITY_LOV_ID = l.LOV_ID
                AND    m.ACTIVITY_ID(+) = a.ACTIVITY_ID
                AND    m.CLASSIFICATION_LOV_ID = l2.LOV_ID(+);
    begin
    for each_row in c_activity loop
      l_my_val := l_my_val ||  ', ' || each_row.value1;
    end loop;
    :l_value := ltrim (l_my_val, ', ');
    end; 
      ~';
      execute immediate l_sql using out :l_value;
      dbms_output.put_line(l_value);
    end;It works perfectly. Notice I have used the q syntax when embedding the sql directly into the package.
    I have tried
    - appending the q syntax directly to query_sql stored in the database
    - escaping the quotes i.e. ' become ''
    Neither method seem to work. We are running 10.2.0.2 on Windows Server 2003. If anyone has any suggestions I would love to hear from you as this has me stumped.
    Regards
    Kris
    - http://kristianjones.blogspot.com

    If you do:
    declare
    l_sql varchar2(32767);
    l_value varchar2(32767);
    begin
    select query_sql into l_sql from lov where lov_id = 100;
    dbms_output.put_line(l_sql);
    end;
    You'll see something like that:
    SELECT
    l.DESCRIPTION || decode(l2.DESCRIPTION,null,'',l2.description, '-' || l2.description) || decode(a.DT,'Y',' - Distributed Training','N',null,null) as value1
    FROM ACTIVITY a
    ,MOUNTAINEERING m
    ,LOV l
    ,LOV l2
    WHERE a.JSATFA_ID = 82
    AND a.SPECIFIC_ACTIVITY_LOV_ID = l.LOV_ID
    AND m.ACTIVITY_ID(+) = a.ACTIVITY_ID
    AND m.CLASSIFICATION_LOV_ID = l2.LOV_ID(+);
    you need to duplicate the '
    you can do many things like:
    CTH@> select * from sqls;
    C
    select first_name || ' ' || last_name as value1 from employees where rownum=1
    1 fila seleccionada.
    CTH@>
    CTH@> ;
    1 declare
    2 l_sql varchar2(32767);
    3 l_value varchar2(32767);
    4 type generic_cursor is ref cursor;
    5
    6 c generic_cursor;
    7
    8 begin
    9 select replace(c, ''', ''''') into l_sql from sqls;
    10
    11 execute immediate l_sql into l_value;
    12 dbms_output.put_line(l_value);
    13* end;
    CTH@> /
    Ellen Abel
    Procedimiento PL/SQL terminado correctamente.
    CTH@>

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

Maybe you are looking for