Execute Immediate Statment

Hello Every one,
Can any one explain me Pros & cons for usage of Execute Immediate statement also whether to go for Dynamic Sql or Static Sql
Warm Regards,
Jagdish

Hi,
execute immediate is new for 9i and is more easy to write and understand, it is limited by command max size of 32 k if i remember correctly.
regarding wether to use Dyn. or Stat sql it is up to you to decide if you need it or not.
dBarak.

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.

  • 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

  • Problem when using execute immediate str variable into refcursor var

    Hi,
    I have a packaged function which returns a "strong refcursor" variable to the calling program.
    inside this packaged function, i had to use dynamic sql ....
    i am doing it somethingl like this...
    execute immediate lc_submit_Sql into lrc_submitted;
    where lc_submit_Sql contains the query. and lrc_submitted is declared as a refcursor variable which is a recordtype.
    when calling this packaged function from an anonymous block getting an error.....
    "ora-00932 inconsistent datatypes:expecting - got -"
    I checked the select statment columns and the record type columns, their types and order. they are fine.
    not sure abt the reason.
    any help is appreciated. have some dead lines to meet.
    Thanks in advance.
    Kiran

    Hi John,
    the following is the code ....
    package spec...
    CREATE OR REPLACE PACKAGE refcur_kk IS
    --Sample submitted Function
    TYPE r_submitted IS RECORD(
    q_party_name apps.bpsicc_crm_quote_header_v.party_name%TYPE,
    q_party_id apps.bpsicc_crm_quote_header_v.cust_party_id%TYPE,
    q_quote_header_id apps.bpsicc_crm_quote_header_v.quote_header_id%TYPE,
    q_creation_date apps.bpsicc_crm_quote_header_v.quote_creation_date%TYPE,
    q_created_by apps.bpsicc_crm_quote_header_v.quote_created_by%TYPE,
    q_quote_status apps.bpsicc_crm_quote_header_v.quote_status%TYPE,
    q_quote_name apps.bpsicc_crm_quote_header_v.quote_name%TYPE,
    q_quote_number apps.bpsicc_crm_quote_header_v.quote_number%TYPE,
    q_internal_project_details apps.bpsicc_crm_quote_line_v.internal_project_details%TYPE,
    ql_item apps.bpsicc_crm_item_all_v.item_number%TYPE,
    ql_item_link apps.bpsicc_crm_item_all_v.item_link%TYPE,
    ql_party_link apps.bpsicc_gcm_party_details_v.party_link%TYPE,
    ql_quantity apps.bpsicc_crm_quote_line_v.quantity%TYPE,
    q_country apps.bpsicc_gcm_party_sites_v.party_site_country_name%TYPE,
    q_territory apps.bpsicc_crm_party_by_terr_mv.territory%TYPE,
    q_region apps.bpsicc_crm_party_by_terr_mv.region%TYPE,
    q_created_disp apps.bpsicc_crm_quote_header_v.quote_created_by_username%TYPE,
    reqst_shipdate apps.bpsicc_crm_quote_header_v.date_of_request%TYPE,
    q_item_color apps.bpsicc_crm_item_all_v.item_color%TYPE
    TYPE rc_submitted IS REF CURSOR;
    RETURN r_submitted;
    FUNCTION get_submitted(
    pd_start_date IN DATE,
    pc_time IN VARCHAR2,
    pd_end_date IN DATE,
    pc_party_id IN VARCHAR2,
    pc_price_class IN VARCHAR2,
    ps_section IN VARCHAR2,
    pn_sales_channel IN NUMBER,
    pc_country IN VARCHAR2,
    pc_territory IN VARCHAR2,
    pc_region IN VARCHAR2)
    RETURN rc_submitted;
    and the body of it.....
    ls_submit_sql:= 'SELECT bcqhv.party_name q_party_name,'||
    'bcqhv.cust_party_id q_party_id,'||
    'bcqhv.quote_header_id q_quote_header_id,'||
    'TRUNC(bcqhv.quote_creation_date) q_creation_date,'||
    'bcqhv.quote_created_by q_created_by,'||
    'bcqhv.quote_status q_quote_status,'||
    'bcqhv.quote_name q_quote_name,'||
    'bcqhv.quote_number q_quote_number,'||
    'bcqlv.internal_project_details q_internal_project_details,'||
    'bciav.item_number ql_item,'||
    'bciav.item_link ql_item_link,'||
    'bgpdv.party_link ql_party_link,'||
    'bcqlv.quantity ql_quantity,'||
    'bgpsv.party_site_country_name q_country,'||
    'bcpbtv.territory q_territory,'||
    'bcpbtv.region q_region,'||
    'bcqhv.quote_created_by_username q_created_disp,'||
    'bcqhv.date_of_request reqst_ship_date,'||
    'bciav.item_color q_item_color '||
    'FROM apps.bpsicc_crm_quote_header_v bcqhv,'||
    'apps.bpsicc_crm_party_by_terr_mv bcpbtv,'||
    'apps.bpsicc_gcm_party_sites_v bgpsv,'||
    'apps.bpsicc_crm_quote_line_v bcqlv,'||
    'apps.bpsicc_crm_item_all_v bciav,'||
    'apps.bpsicc_gcm_party_details_v bgpdv '||
    'WHERE bciav.inventory_item_id = bcqlv.inventory_item_id '||
    'AND bcqhv.cust_party_id = bcpbtv.party_id '||
    'AND bcqhv.sold_to_party_site_id = bgpsv.party_site_id '||
    'AND bgpdv.party_id = bcqhv.cust_party_id '||
    'AND bcqlv.quote_header_id = bcqhv.quote_header_id '||
    'AND bcqhv.quote_status IN ('||chr(39)||'Submitted to Lab for Review'||chr(39)||','||chr(39)||'Submitted to US Lab for Review'||chr(39)||')';
    execute immediate ls_submit_sql into lrc_submitted;
    returning lrc_submitted to the report which is calling this function.
    using oracle report builder.
    kiran

  • 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 Immediate. Insert from materialized view.

    Hi,
    I'm using Execute Immediate in a function to insert rows into a table. (Oracle 10G) The function in tern is used in a materialized view. So, effectively the view does an insert.
    When I just had a regular insert statment the view would not compile. When I changed the insert statment into the execute immediate it started working.
    Here are my questions:
    1. Do I need a commit or execute immediate does it automatically? When I create my view that calls the function it looks like the rows do get inserted as expected. But I couldn't find documentation that explains whether execute immediate does the commit.
    2. Is there a drawback to using execute immediate in a function that is used in a materialized view? Is there other way to insert rows from a materialized view? Can I call a stored procedure instead of a function in a materialized view?
    Your help is greatly appreciated.
    Thanks
    NK

    there are things that I'm not able to question yet :)It's a lot easier to write decent programs if we understand the why of the requirement as well as the what. I think you will create a better impression in your new job if you demonstrate some liveliness of thought and personality - by asking questions - rather than merely following orders.
    The question I proposed is not necessarily a hostile one (I can think of at least one good reason for doing what you've been asked to do). Broadening your knowledge of your new system is a good thing in its own right. But you never know, asking the right question may prevent you doing unnecessary work.
    Cheers, APC

  • Executing ddl statment in pl/sql block

    Hi all,
    could anyone pls help in method for executing ddl statment in pl/sql block other than 'execute immediate' ?

    could anyone pls help in method for executing ddl statment in pl/sql block other than 'execute immediate' ?On newer db versions you have more options:
    SQL> desc t
    Error: object t does not exist
    SQL> exec sys.dbms_prvtaqim.execute_stmt('create table michael.t (a integer)')
    PL/SQL procedure successfully completed.
    SQL> desc t
    TABLE t
    Name                                      Null?    Type                       
    A                                                  NUMBER                     
    SQL> exec sys.dbms_utility.exec_ddl_statement('drop table michael.t')
    PL/SQL procedure successfully completed.
    SQL> desc t
    Error: object t does not exist;)

  • 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    : permition denied  for TRUNCATE talble

    Hi
    In my package I have Execute immedidate
      EXECUTE IMMEDIATE 'TRUNCATE TABLE  MYTABLE'; Return me permission denied , I must to veirfy with DBA my permissions
    My ask is : is there some statment in package/procedure that I do not rembember that let it with permissions ?
    I remember something like authid
    Somebody know ?

    SQL> create user u1 identified by u1;
    User created.
    SQL> grant dba to u1;
    Grant succeeded.
    SQL> connect u1/u1
    Connected.
    SQL> exec execute immediate 'truncate table scott.emp1';
    PL/SQL procedure successfully completed.
    SQL> create or replace
      2  procedure p1
      3  is
      4  begin
      5  execute immediate 'truncate table scott.emp1';
      6  end;
      7  /
    Procedure created.
    SQL> exec p1
    BEGIN p1; END;
    ERROR at line 1:
    ORA-00942: table or view does not exist
    ORA-06512: at "U1.P1", line 4
    ORA-06512: at line 1
    SQL> connect / as sysdba
    Connected.
    SQL> grant drop any table to u1;
    Grant succeeded.
    SQL> connect u1/u1
    Connected.
    SQL> exec p1
    PL/SQL procedure successfully completed.
    SQL> SY.

  • Problem with execute immediate code

    I have the following anonymous block:
    declare
    v_id_char varchar2(50) := '2072018827821663';
    trace_flg number := 0;
    v_exe_imm varchar2(500) := 'univdb.EML_CAMPUS_200605(v_id_char, trace_flg)';
    v_exe_imm2 varchar2(500) := 'univdb.EML_CAMPUS_200605(:v_id_char, :trace_flg)';
    begin
    --1) execute immediate v_exe_imm;
    --2) execute immediate v_exe_imm2 using v_id_char, trace_flg);
    univdb.EML_CAMPUS_200605(v_id_char, trace_flg);
    end;
    The compiled procedure that is getting called is defined like this:
    CREATE OR REPLACE PROCEDURE univdb.eml_campus_200605(
    hist_str IN VARCHAR2,
    trace_flg IN NUMBER := 0) IS ...
    As you might expect, when I execute the block without execute immediate it works.
    But either of the execute immediate statements results in ORA-00900: invalid SQL statment.
    I want to use execute immediate because the procedure that gets called will need to be dynamic.
    Help appreciated,
    ...elsa

    For me, thats crude if you have a procedure thats
    getting created dynamically.
    anyway, you could try a
    execute immediate ' begin ' || your_funny_proc || '
    end;';But for sure the whole stuff is very error prone.Actually, the procedure itself isn't getting built dynamically, only which procedure to call. It's really quite clever in my opinion.
    Anyway, I tried you suggestion:
    declare
    v_id_char varchar2(50) := '2072018827821663';
    trace_flg number := 0;
    v_exe_imm varchar2(500) := 'univdb.EML_CAMPUS_200605(v_id_char, trace_flg)';
    v_exe_imm2 varchar2(500) := 'univdb.EML_CAMPUS_200605(:v_id_char, :trace_flg)';
    begin
    execute immediate 'BEGIN ' || v_exe_imm || '; END;';
    --execute immediate v_exe_imm2 using v_id_char, trace_flg);
    --univdb.EML_CAMPUS_200605(v_id_char, trace_flg);
    end;
    and got
    ORA-06550: line 1, column 32:
    PLS-00201: identifier 'V_ID_CHAR' must be declared
    Did I misunderstand what you were trying for?

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

  • Strange Case on Security Rights and Dynamic SQL (Execute Immediate)

    Hi friends, (forgive me if I write with wrong grammar and sentence, I not used English for daily)
    I got a weird trouble yesterday.
    I created a package (we can called it X, OK!?) which containing Execute Immediate Statement, that function to delete a table (we can called it Y).
    Several days ago, it's worked, but yesterday it wasn't. Last things happened before was recreate those table, and regrant to a role which including user account that execute package X.
    Error Msg shown is ORA-00942 : Table or view does not exist. After rechecked and rechecked, I found nothing that could trigger that error, I used DBMS_OUTPUT.PUT_LINE to debug and show what statement resulted and executed, I cut and paste, and it's worked. I created anonymous PL/SQL Block, and wrote it and executed it, and worked.
    Finally, today, We Grant explicitly those table to user account Y, not via Role, ... and it's work. Interesting thing I think :P
    And, I revoke, execute package and run. I think, there's something about Oracle he..he.. :D .
    Can somebody help me and explain me the reason of that strange symptomp? and right solution? I must know it, because several days again, it's launched / install.
    TIA

    Here is the procedure that get troubled into :)
    PROCEDURE DeleteOld_Job(
    p_Job_Code IN VARCHAR2,
    p_User_Id IN VARCHAR2,
    p_Parameter_Entry IN VARCHAR2,
    p_Status OUT NUMBER )
    IS
    StrSql VARCHAR2(1000);
    CURSOR CTable_Used_By_Report IS
    SELECT TABLE_NAME
    ,TABLE_OWNER
    FROM TABLE_USED_BY_JOB
    WHERE
    Job_Code = p_Job_Code
    BEGIN
    p_Status := 1;
    DBMS_OUTPUT.PUT_LINE('p_Job_Code '| |p_Job_Code );
    DBMS_OUTPUT.PUT_LINE('p_Parameter_Entry '| |p_Parameter_Entry );
    FOR Item IN CTable_Used_By_Report
    LOOP
    StrSql := 'DELETE '| |Item.TABLE_OWNER| |'.'| |Item.TABLE_NAME| |' T WHERE EXISTS ( SELECT 1 FROM USERBATCH.HISTORY_JOB H WHERE H.USER_ID = ' ;
    StrSql := StrSql| |''''| |p_User_Id| |''''| |' AND H.Job_Code = '| |''''| |p_Job_Code| |''''| |' AND H.PARAMETER_ENTRY = '| |'''' | |p_Parameter_Entry| |''''| |' AND T.SESSION_ID = H.TRANSACTION_ID)';
    DBMS_OUTPUT.PUT_LINE(StrSql);
    DBMS_OUTPUT.PUT_LINE(Item.TABLE_OWNER| |'.'| |Item.TABLE_NAME);
    EXECUTE IMMEDIATE StrSql;
    END LOOP;
    DBMS_OUTPUT.PUT_LINE('DELETE USERBATCH.HISTORY_JOB WHERE USER_ID ='''| | p_User_Id | |'''
    AND Job_Code ='''| | p_Job_Code | |''' AND PARAMETER_ENTRY = '''| | p_Parameter_Entry | |'''');
    EXECUTE IMMEDIATE 'DELETE USERBATCH.HISTORY_JOB WHERE USER_ID ='''| | p_User_Id | |'''
    AND Job_Code ='''| | p_Job_Code | |''' AND PARAMETER_ENTRY = '''| | p_Parameter_Entry | |'''';
    COMMIT;
    EXCEPTION
    WHEN OTHERS THEN
    ROLLBACK;
    p_Status := 0;
    DBMS_OUTPUT.PUT_LINE( SUBSTR(SQLERRM,1,255) );
    END DeleteOld_Job;
    TIA
    null

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

  • [Solved] Reference apex_application.g_fXX in "execute immediate" statement

    Hi!
    I created a dynamically generated tabular form - the number of columns is not known in advanced. Each cell of the form is a text item generated with apex_item.text().
    I want to write an after-submit process that saves the values from the form into a database table. In this process I already know how many columns there are in the report so I want to do the following:
    --for each row...
    for i in 1..apex_application.g_f01.count loop
      -- and for each column in that row (number of columns is in v_col_count)
      for j in 1..v_col_count loop
        -- get the value of text item
        v_query := 'select apex_application.g_f0' || j || '(' || i || ')' || ' from dual';
        execute immediate v_query into v_value;
        -- now do some DML with v_value
      end loop;
    end loop;The problem is that I get an error: ORA-06553: PLS-221: 'G_Fxx' is not a procedure or is undefined where xx is the number from the generated query.
    My question is - am I doing something wrong or is is just not possible to reference apex_application.g_fxx in "execute immediate"? Will I have to manually check for all 50 possibilites of apex_application.g_fxx? Is there another way?
    TIA,
    Jure

    Well now I know what was wrong and what you were trying to tell me - apex_application.g_fxx is not visible in "plain" SQL. And now I also have a solution to this problem. The point is to wrap the select statement with begin - end block so that the statement is rendered as pl/sql:
    --for each row...
    for i in 1..apex_application.g_f01.count loop
      -- and for each column in that row (number of columns is in v_col_count)
      for j in 1..v_col_count loop
        -- get the value of text item
        v_query := 'begin select apex_application.g_f0' || j || '(:i)' || ' into :x from dual; end;';
        execute immediate v_query using i, out v_value;
        -- now do some DML with v_value
      end loop;
    end loop;This works great :).
    Jure

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

Maybe you are looking for

  • How to restore iPhone 4 after interrupted update to iOS 5? iTunes returns unknown error

    Hello! I've tried to install iOS 5 beta on my iPhone 4. in the middle of the update XCode displayed something like "couldn't update", then I tried to restore iPhone using iTunes, but each time it throws "couldn't restore. unknown error". now i can't

  • How to copy a music CD using OS X

    I have been trying to figure out how to simply copy a music CD. My sister likes me to make copies of CDs and give them to her; if she likes it, then she buys the real thing. I am not looking to sell or pirate anything. It is just a way for her to sam

  • Sql statement from UI

    Hi, I am new to EBS and it's ui. I would need to fire sql statements on the database. Is there a interface in EBS from which i can fire sql statements ? thanks, ravi.

  • Screaming Fans ,10.4.3., and Single User Mode.

    I wanted to add my experience with the screaming fan problem and 10.4.3. When I ran Disk Utility and used the “verify disk” option I received some minor errors and proceeded to boot the OS X install disk and run Disk Utility from there. When I did I

  • Use of exception message

    HI all Can any one tell me waht is used of exception message in md04? thx in advance Surendra