How to use execute immediate for character string value 300

how to use execute immediate for character string value > 300 , the parameter for this would be passed.
Case 1: When length = 300
declare
str1 varchar2(20) := 'select * from dual';
str2 varchar2(20) := ' union ';
result varchar2(300);
begin
result := str1 || str2 ||
str1 || str2 ||
str1 || str2 ||
str1 || str2 ||
str1 || str2 ||
str1 || str2 ||
str1 || str2 ||
str1 || str2 ||
str1 || str2 ||
str1 || str2 ||
str1 || ' ';
dbms_output.put_line('length : '|| length(result));
execute immediate result;
end;
/SQL> 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
length : 300
PL/SQL procedure successfully completed.
Case 2: When length > 300 ( 301)
SQL> set serveroutput on size 2000;
declare
str1 varchar2(20) := 'select * from dual';
str2 varchar2(20) := ' union ';
result varchar2(300);
begin
result := str1 || str2 ||
str1 || str2 ||
str1 || str2 ||
str1 || str2 ||
str1 || str2 ||
str1 || str2 ||
str1 || str2 ||
str1 || str2 ||
str1 || str2 ||
str1 || str2 ||
str1 || ' ';
dbms_output.put_line('length : '|| length(result));
execute immediate result;
end;
/SQL> 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
declare
ERROR at line 1:
ORA-06502: PL/SQL: numeric or value error: character string buffer too small
ORA-06512: at line 6

result varchar2(300);Answer shouldn't be here ? In the greater variable ?
Nicolas.

Similar Messages

  • Using EXECUTE IMMEDIATE for DML

    What benefit is there to use EXECUTE IMMEDIATE for an UPDATE statement like the one below.
    EXECUTE IMMEDIATE 'UPDATE mytable SET bonus = v_bonus(i)
                       WHERE empid =:empid AND sal =:sal'
                              USING v_empid(n),v_sal(i);This UPDATE sql is not dynamically created. So is there any performance benefit associated with using
    EXECUTE IMMEDIATE for this UPDATE? Can't i just use a normal UPDATE without EXECUTE IMMEDIATE?

    Ran these same tests with SQL_Trace turned on, ran the trace files through tkprof and look at what it tells us:
    declare
    begin
    for i in 1 .. 100000 loop
    gso_prueba_a();
    end loop;
    end;
    call     count       cpu    elapsed       disk      query    current        rows
    Parse        1      0.00       0.00          0          0          0           0
    Execute      1      0.07       0.08          0          0          0           1
    Fetch        0      0.00       0.00          0          0          0           0
    total        2      0.07       0.08          0          0          0           1Now a slow one
    declare
    begin
    for i in 1 .. 100000 loop
    execute immediate 'call gso_prueba_b()';
    end loop;
    end;
    call     count       cpu    elapsed       disk      query    current        rows
    Parse        1      0.00       0.00          0          0          0           0
    Execute      1     36.07      36.54          0          0          0           1
    Fetch        0      0.00       0.00          0          0          0           0
    total        2     36.07      36.54          0          0          0           1but with this slow one, we see LOTS of recursive SQL - the 'call gso_prueba_b' is getting parsed a gazillion times with the resulting CPU consumption
    SQL ID : 1uu09hhqdp1yz
    call gso_prueba_b()
    call     count       cpu    elapsed       disk      query    current        rows
    Parse        1      0.00       0.00          0          0          0           0
    Execute  99989      2.42       2.72          0          0          0           0
    Fetch        0      0.00       0.00          0          0          0           0
    total    99990      2.42       2.72          0          0          0           0
    Misses in library cache during parse: 1
    Optimizer mode: ALL_ROWS
    Parsing user id: 91     (recursive depth: 1)
    SQL ID : dcstr36r0vz0d
    select procedure#,procedurename,properties,itypeobj#
    from
    procedureinfo$ where obj#=:1 order by procedurename desc, overload# desc
    call     count       cpu    elapsed       disk      query    current        rows
    Parse        1      0.00       0.00          0          0          0           0
    Execute      1      0.00       0.00          0          0          0           0
    Fetch        2      0.00       0.00          0          4          0           1
    total        4      0.00       0.00          0          4          0           1Lots, lots more like this last one but having different SQL ID
    By wrapping the procedure call in a dynamically created anonymous block, you are incurring LOTS of overhead.
    What is it that you are trying to simulate.
    What are you trying to do?

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

  • How to use execute query For a single record

    Hi All,
    I am working with oracle forms 10g, I have developed a custom form.
    I the form user enter some data and click save in the menu and data get inserted into my table. Inside the insert statement i have hardcoded some values, Once data get inserted i cant able to see that values immediately in my text fields in my form.After requerying i can able to see the changes.
    For example I have a text field called Status, and the field is display only field. At that time of insert i have hardcoded as "INCOMPLETE". Once the user enter the data in the form and click save the data get inserted in to my table but i cant able to see the status, after i requery my form i can able to see the status.
    Here i do multi insert also.
    This is my insert statement
    BEGIN
    First_Record;
    LOOP
    IF :BANK_GUARANTEE_BLK.CHECK_IN_OUT = 'Y'
    THEN
    XXBANK_GUARANTEE_HEADERS.XXBANK_GUARANTEE_INSERT;(this is my package, here i have written my insert statement)
    --Execute_query;
    END IF;
    Exit when :System.Last_Record = 'TRUE';
    Next_Record;
    END LOOP;
    END;
    If user insert only one row and i gave Execute_query to refresh and bring the current data, but when i use execute_query Its brings up all the datas in the table.
    Can any one tell me how to avoid this and how to use execute_query for a single record.
    Thanks &regards
    Srikkanth

    Solved,
    The solution is
    set_block_property('LC_REQ_BLK',ONETIME_WHERE,'LC_PO_NUMBER='||:LC_REQ_BLK.LC_PO_NUMBER);
    go_block('LC_REQ_BLK');
    execute_query;
    Works fine.
    Regards
    Sri

  • Using execute immediate for select

    Hi all,
    i am writing a procedure as follows:
    create or replace procedure ( run_yr_mo in number) as
    v_count number := 0;
    begin
    execute immediate 'select count(*)
    from all_tables
    where owner = ''MGR''
    and table_name = ''FC_PR_'''||run_yr_mo||''
    into v_count;
    dbms_output.put_line(v_count);
    end;
    this is returning me an error at the execute immediate.
    run_yr_mo = 200912
    Can someone help me out with this.
    Thanks

    Just to show it working:
    SQL> create or replace procedure myproc ( run_yr_mo in number) as
      2   v_count number := 0;
      3  begin
      4    select count(*)
      5    into v_count
      6    from all_tables
      7    where owner = 'MGR'
      8    and table_name = 'FC_PR_'||to_char(run_yr_mo,'fm999999');
      9
    10    dbms_output.put_line(v_count);
    11  end;
    12  /
    Procedure created.
    SQL> set serverout on
    SQL> exec myproc('200910');
    0
    PL/SQL procedure successfully completed.
    SQL>

  • Using PL/SQL Block how do you check if the character string value is aA-zZ

    I have a pl/sql block that I prompt the user for password and I load the string into an array and interrogate each index(entry) to see if it is "aA-zZ".
    How can I check if the value entered is Alpha.
    I need to do the same for number and Special character. Please advise. Thanks

    Thanks to All of you. The desired solution to verify complex password that enforces desired security policies. An example that I am using is the following:
    IF NOT (regexp_like(sz_complex_pw,'[[:digit:]]') AND regexp_like(sz_complex_pw,'[[:alpha:]]') AND regexp_like(sz_complex_pw,'[[:punct:]]') AND regexp_like(sz_complex_pw,'[[:upper:]]'))
    THEN
    --dbms_output.put_line('Password is not complex...');
    RAISE sz_err_pw_complex; -- Complex Password is not compliant
    END IF;
    DBMS_OUTPUT.PUT_LINE('Complex Password is in Compliance...');
    EXCEPTION
    This was helpful to me and I trust others will find helpful.
    Edited by: yakub21 on Oct 16, 2010 9:05 PM

  • How to use Browser Control for displaying String Results..

    Hi All,
    I m new to java. I am developing desktop application for java getting problem at some stage , i hav some string Results which i got some how by searching in some text files. Now i want to show / format this string in Browser control.
    Please help me in this...
    Thanks in Advance
    Nicky

    i m sonu could u piz tell me how to read integer from keyboard

  • Execute Immediat for Cursor

    Dear all
    can i use Execute Immediate For a Cursor..
    for example :-
    create or replace procedure P_MAPPING(iaap_name varchar2)
    IS
    CUR_C1 varchar2(200);
    CUR_C2 varchar2(200);
    Cur_c1 := 'cursor C1 is
                 select recid, xmlrecord
                 from'|| OSRC_TAB;   *---Here How can i pass the value which i am getting from the Procedure Below(P_PAPULATE_TAB)*
    Cur_c2 := 'Cursor C2 is
               Select column_name,Seq_no
               from TMP_MAP_TAB2
               Where Appl_name = '||ODEST_TAB;*---Here How can i pass the value which i am getting from the Procedure Below(P_PAPULATE_TAB_)*
       Type array_type IS TABLE of varchar2(1000) index by binary_integer;
       OSRC_TAB varchar2(100);
       ODEST_TAB varchar2(100);
    begin
      P_PAPULATE_TAB(IAAP_NAME,OSRC_TAB,ODEST_TAB);
      execute immediate CUR_C1 using OSRC_TAB; 
      execute immediate CUR_C2 using ODEST_TAB;I want to pass the OSRC_TAB,ODEST_TAB which ore the OUT parameters in the procedure In the Cursor..
    Is it possible ?

    Hi,
    You can try this using sys_refcursor as like below
    cur_c1 VARCHAR2(200);
    cur_c2 VARCHAR2(200);
    osrc_tab VARCHAR2(100);
    odest_tab VARCHAR2(100);
    l_sys_ref_1 sys_refcursor;
    l_sys_ref_2 sys_refcursor;
    BEGIN
    P_PAPULATE_TAB(IAAP_NAME,OSRC_TAB,ODEST_TAB);
    cur_c1 := 'SELECT recid, xmlrecord
    FROM'|| osrc_tab;
    cur_c2 := ' SELECT column_name,Seq_no
    FROM tmp_map_tab2
    WHERE appl_name = '||odest_tab;
    OPEN l_sys_ref_1
    FOR cur_c1;
    OPEN l_sys_ref_2
    FOR cur_c2;
    Thanks

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

  • Using EXECUTE IMMEDIATE with XML

    Database version : 10.2.0.3.0 - 64bi
    Hi All,
    I have a xml which is stored in a table, xmltype column.
    i have target insert tables whose column names and xml nodes are same.
    using all_tab_columns table i will generate columns to be passed to xmltable.
    all these data i will store in variables and finally pass to xmltable as below
    just want to know using execute immediate is good to use in XML?
    SQL_STMT := 'insert into '||table_name|| ' ( '||V_COLUMN_NAME||')';
    SQL_STMT := SQL_STMT ||' SELECT ' ||V_XTAB_COLUMN_NAME ||
    ' FROM TO_XML,
    XMLTABLE(' ||v_xpath||
    'PASSING XML_VALUE
    columns ' || V_COLUMNS_DATA_TYPE
    ||') XTAB
    WHERE Seq_NO = ' || P_SEQUENCE_NO ;
    EXECUTE IMMEDIATE SQL_STMT ;
    Thanks and Regards,
    Rubu

    1) is it OK? As I stated above, it can be made to work. It would not be my first choice, but then none of us here know the full details as well as you do so maybe there is a compelling reason to use dynamic SQL.
    Here is the documentation for [url http://docs.oracle.com/cd/E11882_01/appdev.112/e25519/executeimmediate_statement.htm#LNPLS01317]EXECUTE IMMEDIATE.
    Actually now I finally realize your XML resides in table TO_XML so that means you won't be putting the actual XML into the shared pool via an incorrectly written dynamic SQL statement at least. That is what Odie and I were first concerned about with dynamic SQL usage, that the XML would be hard-coded into your SQL_STMT variable. You are simply changing the columns (in 3 locations). With that setup, you have no need for (nor can use) bind variables. The overall issue of dynamic SQL being slightly slower than static SQL still exists as the SQL statement will first have to be parsed and validated.
    A larger issue in terms of performance is how 10.2 handles XMLTypes. If the underlying XML is large, XMLTable performance degrades quickly. Options around this are to parse the XML in PL/SQL or to upgrade to some version of 11 and use SECUREFILE BINARY XML as the underlying storage structure for the TO_XML.XML_VALUE column.

  • Using execute immediate creating a table from another

    hi friend i wanted to create a table from a select statement in a pl sql procedure. i am using execute immediate but getting problems with it pls can anyone help me.
    here is the query i am using
    EXECUTE IMMEDIATE 'CREATE TABLE table_name AS  (SELECT * FROM  a_view   WHERE column_name LIKE '%some_string%');
    i need to know if this can be done and if yes how. pls help me its bit urgent too. the schema name is same and the privileges are available to create tables too.

    Your syntax is wrong.
    If you would use a syntax higlighted editor, it would show.
    Yout try to execute a string where another string is embedded. Please use two times single quote or use the 'q' function:
    This one is correct:
    EXECUTE IMMEDIATE 'CREATE TABLE table_name AS (SELECT * FROM  a_view WHERE column_name LIKE ''%some_string%'')';
    or this one:
    EXECUTE IMMEDIATE q'|CREATE TABLE table_name AS (SELECT * FROM  a_view WHERE column_name LIKE '%some_string%')|';
    good luck

  • 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

  • Why or When should we use Execute Immediate in PLSQL??

    Hi Frnds,
    Long Ago i have received a interview question that ...
    How can U create a table in the PLSQL object(Function or procedure)?
    But the thing y should we use execute immediate?
    In which scenario we should we should use????????????
    Why or When should we use Execute Immediate in PLSQL????

    OR
    http://stackoverflow.com/questions/18375990/oracle-what-does-execute-immediate-means
    For DML you'd use it when running statements that you don't have available at compile time, e.g. if the column list is based on a selection from the user.
    In your case it's being used because DDL cannot be run as static SQL from within PL/SQL. Only certain query, DML and TCL commands are valid. Anything else has to be treated as dynamic.
    I'd say it's rare to need to use DDL from a PL/SQL block. TRUNCATE might be reasonable; if you find anything creating or dropping objects on the fly then that might be more of a concern as it can suggest a suboptimal data model.
    EXECUTE IMMEDIATE itself does not automatically commit; but if you execute DDL then that will behave the same as if you ran it outside PL/SQL, so it will commit in your case, yes.
    Incidentally, I'm not sure why your code is using an intermediate variable to hold the statement; that's useful if you want to display what it's going to run maybe, but you don't seem to be doing that. What you have could be done as:
    EXECUTE IMMEDIATE 'TRUNCATE TABLE BD_BIDS_EXT_DET';
    Thank you

  • Delete From More than 1 table without using execute immediate

    Hi,
    Am new to PL/SQL, I had been asked to delete few of the table for my ETL jobs in Oracle 10G R2. I have to delete(truncate) few tables and the table names are in another table with a flag to delete it or not. So, when ever I run the job it should check for the flag and for those flag which is 'Y' then for all those tables should be deleted without using the Execute Immediate, because I dont have privilages to use "Execute Immediate" statement.
    Can anyone help me in how to do this.
    Regards
    Senthil

    Then tell you DBA's, or better yet their boss, that they need some additional training in how Oracle actually works.
    Yes, dynamic sql can be a bad thing when it is used to generate hundreds of identical queries that differ ony in the literals used in predicates, but for something like a set of delte table statements or truncate table statements, dynamic sql is no different in terms of the effect on the shared pool that hard coding the sql statements.
    This is a bad use of dynamic sql, because it generates a lot of nearly identical statements due to the lack of bind variables. It is the type of thing your DBA's should, correctly, bring out the lead pipe for.
    DECLARE
       l_sql VARCHAR2(4000);
    BEGIN
       FOR r in (SELECT account_no FROM accounts_to_delete) LOOP
          l_sql := 'DELETE FROM accounts WHERE account_no = '||r.account_no;
          EXECUTE IMMEDIATE l_sql;
       END LOOP;
    END;This will result in one sql statement in the shared pool for every row in accounts_to_delete. Although there is much else wrong with this example, from the bind variable perspective it should be re-written to use bind variables like:
    DECLARE
       l_sql  VARCHAR2(4000);
       l_acct NUMBER;
    BEGIN
       FOR r in (SELECT account_no FROM accounts_to_delete) LOOP
          l_sql := 'DELETE FROM accounts WHERE account_no = :b1';
          EXECUTE IMMEDIATE l_sql USING l_acct;
       END LOOP;
    END;However, since you cannot bind object names into sql statements, the difference in terms of the number of statements that end up in the shared pool between this:
    DECLARE
       l_sql VARCHAR2(4000);
    BEGIN
       FOR r in (SELECT table_name, delete_tab, trunc_tab
                 FROM tables_to_delete) LOOP
          IF r.delete_tab = 'Y' THEN
             l_sql := 'DELETE FROM '||r.table_name;
          ELSIF r.trunc_tab = 'Y' THEN
             l_sql := 'TRUNCATE TABLE '||r.table_name;
          ELSE
             l_sql := NULL;
          END IF;
          EXECUTE IMMEDIATE l_sql;
       END LOOP;
    END;and something like this:
    BEGIN
       DELETE FROM tab1;
       DELETE FROM tab2;
       EXECUTE IMMEDIATE 'TRUNCTE TABLE tab3';
    END;or this as a sql script
    DELETE FROM tab1;
    DELETE FROM tab2;
    TRUNCTE TABLE tab3;is absolutley nothing.
    Note that if you are truncating some of the tables, and wnat/need to use a stored procedure, you are going to have to use dynamic sql for the truncates anyway since trncate is ddl, and you cannot do ddl in pl/sql wiothout using dynamic sql.
    John

  • Insertion Using Execute Immediate

    How to use Execute Immedaite in Inserting while with dynamic table name. Is that possible or we can use only the dynamic values.
    Execute Immediate "Insert Into " || DYNAMIC TABLE NAME || "Values"
    Using (Value_1, Value 2, Value3).

    Why would tables be added later? Are they storing the same structure of data? If so you should be using a single table possibly with partitioning.
    If they are of different structures then you should have one procedure for each structure, otherwise you're going down the Bad Design ^tm^ route. A single procedure should perform a single task. Trying to make generic code is generally considered a lazy design and often leads to performance issues and difficult to track bugs.

Maybe you are looking for