A loop dynamic sql in the procedure

Hi guys,.

I'm having a procedure that will have two input start_date and end_date parameters

I need a loop in alter procedure statement something like this using dynamic sql


CHANGE TABLE M1

SWAP PARTITION FOR (TO_DATE('01-JAN-2015','dd-MON-yyyy'))

WITH TABLE T2

INCLUDING THE INDEX;

CHANGE TABLE M1

BY EXCHANGE COMPETITION FOR (TO_DATE('02-JAN-2015','dd-MON-yyyy'))

WITH TABLE T3

INCLUDING THE INDEX;

.

.

.


For example I have dates in setting something like January 1, 2015-4 January 2015

I need something like this, where T is a constant variable, I need to add T1, T2. T3 according to the dates of the range.

Hello

your procedure can not simply be rolled up, but the call session can be rolled up the output to a file.

Try something like this

col act_date noprint new_value act_date

SET TERMOUT  OFF

select to_char(sysdate, 'yyyy_mm_dd_hh24miss') act_date
  from dual;

SET TERMOUT      ON
set serveroutput on
set feedback     off
set linesize     3000

col statement for a200 heading "-- alter statements"

spool d:\temp\&act_date._&_CONNECT_IDENTIFIER._alter_partition.sql

-- exec your_procedure

-- or simple plain sql

with data (start_date, end_date) as (
  select to_date('01/01/2016', 'dd/mm/yyyy'),
        to_date('05/01/2016', 'dd/mm/yyyy')
    from dual
    )
select --level, to_char(start_date + (level - 1), 'dd-MON-yyyy' ) cur_date
                 'ALTER TABLE M1 EXCHANGE PARTITION FOR (TO_DATE(''' || to_char(start_date + (level - 1), 'dd-MON-yyyy' ) || ''',''dd-MON-yyyy'')) WITH TABLE T2 INCLUDING INDEXES;'
  || chr(10) ||  'ALTER TABLE M1 EXCHANGE PARTITION FOR (TO_DATE(''' || to_char(start_date + (level - 1), 'dd-MON-yyyy' ) || ''',''dd-MON-yyyy'')) WITH TABLE T3 INCLUDING INDEXES;'  as statement
  from data
connect by level <= end_date - start_date + 1;

spool off

set feedback    on

prompt @d:\temp\mk_alter.sql

concerning
Kay

Tags: Database

Similar Questions

  • Using of the dynamic SQL and the cursor in a procedure

    Here is the procedure:
    create or replace
    Procedure type_paiement_total
    is
    
        cursor xbtable is select table_name from user_tables where table_name like 'XB%';
        n_table user_tables.table_name%type;
        req     varchar2(256);
        journal varchar2(2);
        mois varchar2(2);
        an varchar2(2);
      begin
        for n_table in xbtable
        loop
          execute immediate 'insert into xx_jk_xb (
          clie_code,journal, periode,origine, xb_ecri,xb_libe  ,dos_code,xb_debi,xb_cred,xb_term
    )
    select c.code,  
    substr(:1,3,2),
    substr(:1,7,2)||substr(:1,5,2)||,
    :1,  
    xb.ecri,  
    xb.libe,  
    d.code,
    xb.debi,
    xb.cred,
    xb.terme
    from                      
    '||n_table.table_name ||' xb,
    dossier d,                      
    client c                    
    where xb.cmpt=''4111''                    
    and xb.doss  =d.code                    
    and c.code   =d.clie
    and c.role=''1''' using n_table.table_name;
          execute immediate 'insert into xx_jk_logxb (recnum,xb_ref,trsf) values (seq_logmreg.nextval,:1,''OK'')' using n_table.table_name;
          commit;
          fetch xbtable into n_table;
        end loop;
      end;
    What he does (or what I intend to do)
    take the datas of a whole bunch of pictures and put them in the "XX_JK_XB" table and make a log of the tables covered in xx_jk_logxb give just the source table and the status (OK).
    Now when I run the procedure I get a "missing expression" th ' immediate «insert into xx_jk...»» »

    I just can't tell what is the problem here.

    clues?

    Seems to me that you are wrong assuming that the binding can be done by name and no position when you use immediate enforcement.

    Example:

    SQL> create table foo_tab( c1 varchar2(10), n1 number );
    
    Table created.
    
    SQL>
    SQL> begin
      2          for i in 1..10
      3          loop
      4                  execute immediate 'insert into foo_tab values( to_char(:1), :1 )' using i;
      5          end loop;
      6          commit;
      7  end;
      8  /
    begin
    *
    ERROR at line 1:
    ORA-01008: not all variables bound
    ORA-06512: at line 4
    
    SQL>
    SQL>
    SQL> begin
      2          for i in 1..10
      3          loop
      4                  execute immediate 'insert into foo_tab values( to_char(:1), :1 )' using i,i;
      5          end loop;
      6          commit;
      7  end;
      8  /
    
    PL/SQL procedure successfully completed.
    
    SQL>
    

    As you can see, 1 PL/SQL block attempts to re - use bind variable: 1 new - and only link once.

    Fact does not work like that - you must link it again. Binding is done by bind - 1 position = 1 to the help of var, 2nd = 2nd bind using var, etc.. Name of the connection variable used is irrelevant and not unique.

  • Tables created in a stored procedure cannot be used with dynamic SQL? The impact?

    There is a thread on the forum which explains how to create tables within a stored procedure (How to create a table in a stored procedure , however, it does create a table as such, but not how to use it (insert, select, update, etc.) the table in the stored procedure.) Looking around and in the light of the tests, it seems that you need to use dynamic SQL statements to execute ddl in a stored procedure in Oracle DB. In addition, it also seems that you cannot use dynamic SQL statements for reuse (insert, select, update, etc.) the table that was created in the stored procedure? Is this really the case?

    If this is the case, I am afraid that if tables cannot be 'created and used"in a stored procedure using the dynamic SQL, as is the case with most of the servers of DB dynamic SQL is not a part of the implementation plan and, therefore, is quite expensive (slow). This is the case with Oracle, and if yes what is the performance impact? (Apparently, with Informix, yield loss is about 3 - 4 times, MS SQL - 4 - 5 times and so on).

    In summary, tables created within a stored procedure cannot be 'used' with dynamic SQL, and if so, what is the impact of performance as such?

    Thank you and best regards,
    Amedeo.

    Published by: AGF on March 17, 2009 10:51

    AGF says:
    Hi, Frank.

    Thank you for your response. I understand that the dynamic SQL is required in this context.

    Unfortunately, I am yet to discover "that seeks to" using temporary tables inside stored procedures. I'm helping a migration from MySQL to Oracle DB, and this was one of the dilemmas encountered. I'll post what is the attempt, when more.

    In Oracle, we use [global temporary Tables | http://www.psoug.org/reference/OLD/gtt.html?PHPSESSID=67b3adaeaf970906c5e037b23ed380c2] aka TWG these tables need only be created once everything like a normal table, but they act differently when they are used. The data inserted in TWG will be visible at the session that inserted data, allowing you to use the table for their own temporary needs while not collide with them of all sessions. The data of the TWG will be automatically deleted (if not deleted programmatically) when a) a commit is issued or b) the session ends according to the parameter that is used during the creation of the TWG. There is no real need in Oracle to create tables dynamically in code.

    I noticed that many people say that the "Creation of the tables within a stored procedure" is not a good idea, but nobody seems necessarily explain why? Think you could elaborate a little bit? Would be appreciated.

    The main reason is that when you come to compile PL/SQL code on the database, all explicit references to tables in the code must correspond to an existing table, otherwise a djab error will occur. This is necessary so that Oracle can validate the columns that are referenced, the data types of those columns etc.. These compilation controls are an important element to ensure that the compiled code is as error free as possible (there is no accounting for the logic of programmers though ;)).

    If you start to create tables dynamically in your PL/SQL code, so any time you want to reference this table you must ensure that you write your SQL queries dynamically too. Once you start doing this, then Oracle will not be able to validate your SQL syntax, check the types of data or SQL logic. This makes your code more difficult to write and harder to debug, because inevitably it contains errors. It also means that for example if you want to write a simple query to get that one out in a variable value (which would take a single line of SQL with static tables), you end up writing a dynamic slider all for her. Very heavy and very messy. You also get the situation in which, if you create tables dynamically in the code, you are also likely to drop tables dynamically in code. If it is a fixed table name, then in an environment multi-user, you get in a mess well when different user sessions are trying to determine if the table exists already or is the last one to use so they can drop etc. What headache! If you create tables with table names, then variable Dynamics not only make you a lot end up creating (and falling) of objects on the database, which can cause an overload on the update of the data dictionary, but how can ensure you that you clean the tables, if your code has an exception any. Indeed, you'll find yourself with redundant tables lying around on your database, may contain sensitive data that should be removed.

    With the TWG, you have none of these issues.

    Also, what is the impact on the performance of the dynamic SQL statements in Oracle? I read some contrasting opinions, some indicating that it is not a lot of difference between static SQL and SQL dynamic in more recent versions of Oracle DB (Re: why dynamic sql is slower than static sql is this true?)

    When the query runs on the database, there will be no difference in performance because it is just a request for enforcement in the SQL engine. Performance problems may occur if your dynamic query is not binding variable in the query correctly (because this would cause difficult analysis of the query rather than sweet), and also the extra time, to dynamically write the query running.

    Another risk of dynamic query is SQL injection which may result in a security risk on the database.

    Good programming will have little need for the tables of dynamically created dynamically or SQL.

  • How to use Bulk collect in dynamic SQL with the example below:

    My Question is

    Using of dynamic SQL with collection in bulkif we pass the name of the table as "to the parameter' function, I want to display those

    An array of column names without vowels (replace the vowels by spaces or remove vowels and display).

    Please explain for example.

    Thank you!!

    It's just a predefined type

    SQL> desc sys.OdciVarchar2List
     sys.OdciVarchar2List VARRAY(32767) OF VARCHAR2(4000)
    

    You can just as easily declare your own collection type (and you are probably better served declaring your own type of readability if nothing else)

    SQL> ed
    Wrote file afiedt.buf
    
      1  CREATE OR REPLACE
      2     PROCEDURE TBL_COLS_NO_VOWELS(
      3                                  p_owner VARCHAR2,
      4                                  p_tbl   VARCHAR2
      5                                 )
      6  IS
      7     TYPE vc2_tbl IS TABLE OF varchar2(4000);
      8     v_col_list vc2_tbl ;
      9  BEGIN
     10      EXECUTE IMMEDIATE 'SELECT COLUMN_NAME FROM DBA_TAB_COLUMNS WHERE OWNER = :1 AND TABLE_NAME = :2 ORDER BY COLUMN_ID'
     11         BULK COLLECT
     12         INTO v_col_list
     13        USING p_owner,
     14              p_tbl;
     15      FOR v_i IN 1..v_col_list.COUNT LOOP
     16        DBMS_OUTPUT.PUT_LINE(TRANSLATE(v_col_list(v_i),'1AEIOU','1'));
     17      END LOOP;
     18*  END;
    SQL> /
    
    Procedure created.
    
    SQL> exec tbl_cols_no_vowels( 'SCOTT', 'EMP' );
    MPN
    NM
    JB
    MGR
    HRDT
    SL
    CMM
    DPTN
    
    PL/SQL procedure successfully completed.
    

    Justin

  • Dynamic insertion within the procedure error


    Hi all

    I use under oracle database on HP - UX.

    BANNER
    ----------------------------------------------------------------
    Oracle Database 10g Enterprise Edition Release 10.2.0.5.0 - 64bi
    PL/SQL Release 10.2.0.5.0 - Production
    CORE Production 10.2.0.5.0
    AMT for HP - UX: 10.2.0.5.0 - Production Version
    NLSRTL Version 10.2.0.5.0 - Production

    I'm not able to run the procedure below and it returns the error. The insert in the procedure returns error. If I execute manually only the insert statement, it works well.
    Can someone help me with this issue?

    Procedure
    ===========
    create or replace procedure test_proc
    as
    v_sql varchar2 (2000);
    Start
    v_sql: =' insert into TABLE1 (select owner, object_type, object_name, "S", "Y" of dba_objects@LINK1 )
    where owner = "ABC" and object_type = "TABLE" less
    Select the owner, object_type, object_name, "S", "Y" from dba_objects where owner = "ABC" and object_type = "TABLE") ';

    immediately run v_sql;
    commit;
    end;
    /

    exec test_proc;

    Error:
    ===========

    ERROR on line 1:
    ORA-00942: table or view does not exist
    ORA-06512: at "SYSTEM. TEST_PROC", line 8
    ORA-06512: at line 1

    user running this procedure doesn't have permission to read data from DBA_OBJECTS. Grant select permission on that to the user running this procedure and it should work.

    Onkar

  • Oracle dynamic SQL with the UNION operator

    Hello

    I have sql string contains two queries separated by the UNION operator. When I run by using DBMS_SQL, it becomes only the first SQL before the UNION operator.

    Can anyone correct how can I Sue UNION in DBMS_SQL?

    Thank you

    Sorry,

    I tested my code

                        BEGIN
                          FOR I IN (SELECT 1 A FROM DUAL UNION
                                  SELECT 2 A  FROM DUAL UNION
                                  SELECT 3 A  FROM DUAL) LOOP
                          DBMS_OUTPUT.PUT_LINE(I.A);
                         END LOOP;
                        END;
                        
    

    and copy this code and PASE to DBMS_SQL. PARSE (all other cases, "... and insert the INSERT STATEMENT but does not erase DBMS_OUTPUT.")

    Edited by: Quluzade Mr. Mahir on February 28, 2011 12:18

  • Creating views, dynamic SQL within stored procedure

    I'm having a problem with the creation of dynamic views of in a stored procedure. The following declare block works fine:

    DECLARE
    parameter i_nom_table varchar2 (200): = 'abc ';
    xyz cursor script, SELECT step
    STARTING from scripts
    WHERE table_name = i_nom_table parameter
    ORDER BY step CAD;
    l_sql scripts.script%TYPE;
    l_step scripts.step%TYPE;
    l_error VARCHAR2 (200);
    l_code VARCHAR2 (200);
    Start
    XYZ OPEN;
    LOOP
    XYZ-FETCH INTO l_step, l_sql;
    OUTPUT WHEN xyz % NOTFOUND;
    immediately run l_sql;
    insert into ingest_log values (null, sysdate, i_nom_table, l_step, l_sql, 'Success' parameter);
    END LOOP;
    CLOSE XYZ;
    insert into ingest_log values (null, sysdate, parameter i_nom_table, 0, "Accomplished all the steps.", "Success");
    EXCEPTION WHEN OTHERS THEN
    l_error: = substr (SQLERRM, 1, 200);
    l_code: = SQLCODE;
    insert into ingest_log values (null, sysdate, parameter i_nom_table, l_step, l_sql, l_code |) ' - ERROR - ' | l_error);
    END;

    However, if I create a procedure with this block and try to run it I get an insufficient privileges error. Do not know why. All tables, views, procedures are under the same user, and the user that I'm connected as the runtime of the declare block. The user has the following privileges:

    Connect, resource, xdbadmin, s/n

    Any reason you can think of for this? Script values are generally "CREATE OR REPLACE VIEW As.... » ;

    Permissions in Oracle to do indirectly through roles are not available when compiling packages, functions, and stored procedures. Direct subsidies are required during the creation of these objects in the database.

    http://articles.TechRepublic.com.com/5100-10878_11-6183799.html

  • Dynamic SQL in the discharge line to cursor line

    Hello

    Anoyne can help me?

    I want to create a function that returns an object in the pipeline.

    I have create a type of oracle and a collection, lets say:

    CREATE or REPLACE type line_man as an object (LTC varchar2 (2),)
    number of num,
    user_t varchar2 (10));

    CREATE or REPLACE type table_man as the line_man table;

    After that, I created a function that returns a pipeline:


    create or replace function man_tst (p_1 in number,
    P_2 number,
    P_3 number) table_man return pipeline is
    vStm varchar2 (4000);
    v_cur sys_refcursor;
    line_man s_cur;
    Start
    vStm: = ' select LTC, num, user_t from table_1 where 1 = 1';
    -a code that might change the place where clause
    ...
    Open the v_cur for vstm;
    loop
    extract the v_cur in s_cur;
    When the output v_cur % notfound;
    line (table_man (s_cur));
    end loop;
    end man_tst;


    But when I try to compile, it gives me an error:
    [Error] PLS-00382 (167:15): PLS-00382: expression is of the wrong type

    What I am doing wrong? That's how I suppose maybe pass the values of the line?

    Thank you

    Here is an example of work - using treatment in bulk to minimize the change between SQL and PL/SQL context engines:

    SQL> create or replace type TLineMan as object(
      2          sld     varchar2(2),
      3          num     number,
      4          user_t  varchar2(10)
      5  );
      6  /
    
    Type created.
    
    SQL>
    SQL>
    SQL> create or replace type TLineManTable as table of TLineMan;
      2  /
    
    Type created.
    
    SQL>
    SQL> create or replace function ManTest( rowCount number )  return TLineManTable pipelined is
      2          buf     TLineManTable;
      3          c       sys_refcursor;
      4  begin
      5          open c for
      6                  'select
      7                          TLineMan( rownum, object_id, substr(object_name,1,10) )
      8                  from    user_objects
      9                  where   rownum <= :0'
     10          using IN rowcount;
     11
     12          loop
     13                  fetch c bulk collect into buf limit 100;
     14
     15                  for i in 1..buf.Count loop
     16                          pipe row( buf(i) );
     17                  end loop;
     18
     19                  exit when c%NotFound;
     20          end loop;
     21
     22          close c;
     23
     24          return;
     25  end;
     26  /
    
    Function created.
    
    SQL> show errors
    No errors.
    SQL>
    SQL> select * from TABLE( ManTest(2) );
    
    SL        NUM USER_T
    -- ---------- ----------
    1      284971 ADDFORMULA
    2      282910 ASSERT
    
    SQL> 
    

    However-, I wouldn't really code of any pipeline in this way. It makes no sense to slap a pipeline on top a cursor like the fact this example.

    The pipes are ideal for transforming data - for example, transform a web URL that returns a CSV text in a table. Pushing data Oracle SQL in a PL/SQL pipeline and then pushing that same data back to the ranks... as channeled SQL engine parts of additional mobile this performance impact without real justification and benefits...

  • How the date can be filled with timestamp in dynamic sql

    Hi all

    Below we have sql

    v_stmt: = ' MERGE IN MTH_EQUIPMENTS_EXT_B ED

    WITH THE HELP OF)

    SELECT TAG_DATA,

    EQUIPMENT_FK_KEY,

    WORKORDER_FK_KEY,

    SEGMENT_FK_KEY,

    SHIFT_WORKDAY_FK_KEY,

    HOUR_FK_KEY,

    ITEM_FK_KEY,

    READ_TIME,

    ATTR_GROUP_ID,

    RECIPE_NUM,

    RECIPE_VERSION,

    NVL (FND_GLOBAL. User_Id,-1) l_updated_by,.

    NVL (FND_GLOBAL. Login_Id,-1) l_last_update_login

    OF MTH_TAG_READINGS_T_STG

    WHERE DB_COL = ' | " ' || v_colname | " ' ||') TS

    WE (';)

    v_stmt: = v_stmt | "ED. EQUIPMENT_PK_KEY = TS. EQUIPMENT_FK_KEY AND

    ED. READ_TIME = TS. READ_TIME AND

    ED. ATTR_GROUP_ID = TS. ATTR_GROUP_ID)

    WHEN MATCHED THEN

    UPDATE

    GAME ED.'. v_colname |' = TS. TAG_DATA,

    ED. LAST_UPDATED_BY = 1, ";

    v_stmt: = v_stmt | "ED. LAST_UPDATE_LOGIN = TS.l_last_update_login

    WHEN NOT MATCHED THEN

    INSERT ('| v_colname |', EXTENSION_ID, EQUIPMENT_PK_KEY, WORKORDER_FK_KEY, SEGMENT_FK_KEY, SHIFT_WORKDAY_FK_KEY, HOUR_FK_KEY, ITEM_FK_KEY, READ_TIME, ATTR_GROUP_ID, LAST_UPDATE_DATE, LAST_UPDATED_BY,';)

    v_stmt: =.

    v_stmt | ("LAST_UPDATE_LOGIN, CREATED_BY, CREATION_DATE, RECIPE_NUM, RECIPE_VERSION)

    VALUES (TS. TAG_DATA, EGO_EXTFWK_S.NEXTVAL, TS. EQUIPMENT_FK_KEY, TS. WORKORDER_FK_KEY, TS. SEGMENT_FK_KEY, TS. SHIFT_WORKDAY_FK_KEY, TS. HOUR_FK_KEY, TS. ITEM_FK_KEY, TS. READ_TIME,';

    v_stmt: = v_stmt | "TS. ATTR_GROUP_ID,' | '''|| SYSDATE | "'| ', 1, TS.l_last_update_login, TS.l_updated_by,'|"' | SYSDATE | " ' ||', TS. RECIPE_NUM, TS. RECIPE_VERSION)';

    EXECUTE IMMEDIATE v_stmt;

    After you run the above SQL last date of update in MTH_EQUIPMENST_EXT_B is filled with sysdate but timestamp is not its always 00:00:00. Can we have timestamp also populated through dynamic sql in the last update date

    Kind regards

    Amrit

    Values to hardcode in the dynamic SQL - never use bind variables:

    v_stmt: = ' MERGE IN MTH_EQUIPMENTS_EXT_B ED

    WITH THE HELP OF)

    SELECT TAG_DATA,

    EQUIPMENT_FK_KEY,

    WORKORDER_FK_KEY,

    SEGMENT_FK_KEY,

    SHIFT_WORKDAY_FK_KEY,

    HOUR_FK_KEY,

    ITEM_FK_KEY,

    READ_TIME,

    ATTR_GROUP_ID,

    RECIPE_NUM,

    RECIPE_VERSION,

    NVL (FND_GLOBAL. User_Id,-1) l_updated_by,.

    NVL (FND_GLOBAL. Login_Id,-1) l_last_update_login

    OF MTH_TAG_READINGS_T_STG

    WHERE DB_COL =: 1) TS

    WE (';)

    v_stmt: = v_stmt | "ED. EQUIPMENT_PK_KEY = TS. EQUIPMENT_FK_KEY AND

    ED. READ_TIME = TS. READ_TIME AND

    ED. ATTR_GROUP_ID = TS. ATTR_GROUP_ID)

    WHEN MATCHED THEN

    UPDATE

    GAME ED.'. v_colname |' = TS. TAG_DATA,

    ED. LAST_UPDATED_BY = 1, ";

    v_stmt: = v_stmt | "ED. LAST_UPDATE_LOGIN = TS.l_last_update_login

    WHEN NOT MATCHED THEN

    INSERT ('| v_colname |', EXTENSION_ID, EQUIPMENT_PK_KEY, WORKORDER_FK_KEY, SEGMENT_FK_KEY, SHIFT_WORKDAY_FK_KEY, HOUR_FK_KEY, ITEM_FK_KEY, READ_TIME, ATTR_GROUP_ID, LAST_UPDATE_DATE, LAST_UPDATED_BY,';)

    v_stmt: =.

    v_stmt | ("LAST_UPDATE_LOGIN, CREATED_BY, CREATION_DATE, RECIPE_NUM, RECIPE_VERSION)

    VALUES (TS. TAG_DATA, EGO_EXTFWK_S.NEXTVAL, TS. EQUIPMENT_FK_KEY, TS. WORKORDER_FK_KEY, TS. SEGMENT_FK_KEY, TS. SHIFT_WORKDAY_FK_KEY, TS. HOUR_FK_KEY, TS. ITEM_FK_KEY, TS. READ_TIME,';

    v_stmt: = v_stmt | "TS. ATTR_GROUP_ID,: 2, 1, TS.l_last_update_login, TS.l_updated_by,: 3, TS. RECIPE_NUM, TS. RECIPE_VERSION)';

    V_stmt EXECUTE IMMEDIATE

    With the HELP of v_colname,.

    SYSDATE,

    SYSDATE;

    SY.

  • RE: Dynamic Sql

    Hello gurus,

    I have a doubt!

    How to get the value of a dynamic sql inside the loop For?
    DECLARE
       lv_v_sql   VARCHAR2 (4000);
    BEGIN
       lv_v_sql :=
             'SELECT a FROM'
          || ' (SELECT 1 AS a FROM DUAL '
          || 'UNION ALL '
          || 'SELECT 2 AS a FROM DUAL'
          || ' UNION ALL '
          || 'SELECT 3 AS a FROM DUAL '
          || ' UNION ALL '
          || ' SELECT 4 AS a FROM DUAL'
          || ' UNION ALL '
          || ' SELECT 5 AS a FROM DUAL) '
          || ' where a='
          || 1;
    
       EXECUTE IMMEDIATE lv_v_sql;
    
       DBMS_OUTPUT.put_line (lv_v_sql);
    
       FOR i IN lv_v_sql
       LOOP
          DBMS_OUTPUT.put_line (i);
       END LOOP;
    END;
    Kind regards
    a friend :)

    Just add IN the clause EXECUTE IMMEDIATE. Example below shows you how to select EXECUTE IMMEDIATE data when it returns a single row and when she returns several lines:

    SQL> DECLARE
      2     lv_v_sql   VARCHAR2 (4000);
      3     v_a number;
      4     v_a_tbl sys.OdciNumberList;
      5  BEGIN
      6     lv_v_sql :=
      7           'SELECT a FROM'
      8        || ' (SELECT 1 AS a FROM DUAL '
      9        || 'UNION ALL '
     10        || 'SELECT 2 AS a FROM DUAL'
     11        || ' UNION ALL '
     12        || 'SELECT 3 AS a FROM DUAL '
     13        || ' UNION ALL '
     14        || ' SELECT 4 AS a FROM DUAL'
     15        || ' UNION ALL '
     16        || ' SELECT 5 AS a FROM DUAL) '
     17        || ' where a='
     18        || 1;
     19     EXECUTE IMMEDIATE lv_v_sql
     20       INTO v_a;
     21     DBMS_OUTPUT.put_line (lv_v_sql);
     22     DBMS_OUTPUT.put_line(v_a);
     23     lv_v_sql :=
     24           'SELECT a FROM'
     25        || ' (SELECT 1 AS a FROM DUAL '
     26        || 'UNION ALL '
     27        || 'SELECT 2 AS a FROM DUAL'
     28        || ' UNION ALL '
     29        || 'SELECT 3 AS a FROM DUAL '
     30        || ' UNION ALL '
     31        || ' SELECT 4 AS a FROM DUAL'
     32        || ' UNION ALL '
     33        || ' SELECT 5 AS a FROM DUAL) ';
     34     EXECUTE IMMEDIATE lv_v_sql
     35       BULK COLLECT
     36       INTO v_a_tbl;
     37     DBMS_OUTPUT.put_line (lv_v_sql);
     38     FOR i IN 1..v_a_tbl.COUNT
     39     LOOP
     40        DBMS_OUTPUT.put_line(v_a_tbl(i));
     41     END LOOP;
     42  END;
     43  /
    SELECT a FROM (SELECT 1 AS a FROM DUAL UNION ALL SELECT 2 AS a FROM DUAL UNION ALL SELECT 3 AS a FROM DUAL  UNION ALL  SELECT 4 AS a
    FROM DUAL UNION ALL  SELECT 5 AS a FROM DUAL)  where a=1
    1
    SELECT a FROM (SELECT 1 AS a FROM DUAL UNION ALL SELECT 2 AS a FROM DUAL UNION ALL SELECT 3 AS a FROM DUAL  UNION ALL  SELECT 4 AS a
    FROM DUAL UNION ALL  SELECT 5 AS a FROM DUAL)
    1
    2
    3
    4
    5
    
    PL/SQL procedure successfully completed.
    

    SY.

  • Dynamic SQL with a Ref Cursor

    Hello

    I have a package that returns a Ref Cursor, in this procedure, I have a dynamic sql code that is built according to certain values, and the query is a select query, is it possible that I can put that dynamic sql in the ref cursor and return of the procedure.

    Or y at - it no alternative better workaround.

    Thanks in advance.

    Naveen

    Yes you can.

    Try this...

    create or replace package test_pack is
    type ref_cur is ref cursor;
    procedure just_print(ref_var ref_cur);
    end;
    /
    
    create or replace package body test_pack is
    procedure just_print(ref_var ref_cur) is
    l_var emp%rowtype;
    begin
    loop
    fetch ref_var into l_var;
    exit when ref_var%notfound;
    dbms_output.put_line(l_var.ename);
    end loop;
    end;
    end;
    /
    
    declare
    cur_var test_pack.ref_cur;
    dsql varchar2(100);
    begin
    dsql := 'select * from emp where deptno=10';
    open cur_var for dsql;
    test_pack.just_print(cur_var);
    end;
    /
    CLARK
    KING
    MILLER
    
    PL/SQL procedure successfully completed.
    
    Elapsed: 00:00:00.00
    

    Kind regards
    Prazy

  • Run the procedure by table number

    Hello

    Oracle 11.2.0.1
    Windows

    Create the np_type type is varray (3) of the number
    /

    Create the cn_type type is varray (3) the number;
    /

    Create the cxn_type type is varray (3) of varchar2 (2000)
    /

    I created the TEST table by pl/sql block below:

    declare
    execstr varchar2 (2000): =' create table test(';)
    Start
    because me in 1.80 loop
    execstr: = execstr | 'col ' | TO_CHAR (i) | 'number,';
    end loop;
    execstr: = substr (execstr, 1, length (execstr)-1);
    execstr: = execstr | ')';
    dbms_output.put_line (execstr);
    run immediately execstr;
    end;
    /

    create the table sp_table (Splitid number, Delimiterlength number);
    insert into sp_table values (1.4);
    insert into sp_table values (2.7);
    insert into sp_table values (3.9);
    insert into sp_table values (4.1);
    insert into sp_table values (5.2);
    insert into sp_table values (6,6);


    create or replace procedure myproc1
    (
    STRX in varchar2, delarray in np_type)
    as
    execstr varchar2 (2000);
    CN cn_type.
    CXN cxn_type;
    XPos number: = 1;
    Start
    execstr: = ' insert into test values(';)
    because loop me in 1.3
    Select Delimiterlength in the sp_table (i) cn where Splitid = delarray (i);
    CXN (i):=substr(STRX,XPos,CN(i));
    execstr: = execstr | CXN (i) | ',';
    XPos: = (i) cn + xpos;
    end loop;
    execstr: = RTRIM (execstr, ','). ')';
    dbms_output.put_line (execstr);
    -execution immediate execstr;
    end;
    /

    SQL> declare
      2    v np_type:=np_type(4,4,4);
      3    begin
      4    --dbms_output.put_line(v(3));
      5    exec myproc1('111',v);
      6    end;
      7  /
      exec myproc1('111',v);
           *
    ERROR at line 5:
    ORA-06550: line 5, column 8:
    PLS-00103: Encountered the symbol "MYPROC1" when expecting one of the
    following:
    := . ( @ % ;
    The symbol ":=" was substituted for "MYPROC1" to continue.
    
    SQL>
    The procedure above will insert the numbered string supplied in the test table that splits the chain numbered inspires provided splitids of sp_table.

    In fact, I tried to help a member of the forum for his question, but I'm not get where I get the above error. Of course, there is little that miss me, but not that. This thread is running too in this forum.

    Please help me.

    Concerning
    Girish Sharma
    create or replace procedure myproc1
    (
    strx in varchar2, delarray in np_type)
    as
    execstr varchar2(2000);
    cn cn_type := cn_type(null, null, null);
    cxn cxn_type := cxn_type(null, null, null);
    xpos number:=1;
    begin
    execstr := 'insert into test values(';
    for i in 1..3 loop
    select Delimiterlength into cn(i) from sp_table where Splitid = delarray(i);
    cxn(i):=substr(strx,xpos,cn(i));
    execstr :=execstr || cxn(i) || ',';
    xpos :=cn(i) + xpos;
    end loop;
    execstr := RTRIM(execstr,',')|| ')';
    
    dbms_output.put_line(execstr);
    --execute immediate execstr;
    end;
    /
    

    and

    declare
        a1 number :=4; a2 number :=4; a3 number :=4;
        v np_type:=np_type(a1,a2,a3);
        begin
        for i in 1..3 loop
        dbms_output.put_line('v(' || i || ') = ' || v(i));
        end loop;
        myproc1('111',v);
        end;
    /
    

    Your varrays are null or empty in myproc1

  • Do not pass the datetime value to store the procedure

    Hello

    I have a stored procedure that will receive the date passmeter and I write it as follows:

    < IsDefined ("form.yy") cfif >

    < cfsqltype cfprocparam = "CF_SQL_TIMESTAMP" value = "#CreateDate (form.yy, form.mm, form.dd) #" >

    < cfelse >

    < cfprocparam cfsqltype = "CF_SQL_TIMESTAMP" value = "null" null = "yes" >

    < / cfif >

    When I do not seized the day, it runs correctly.

    However, it returns me error when I entered the day:

    Conversion failed when converting datetime from character string.

    How can I solve the problem?

    Finally, I find that I can pass the parameter correctly. The problem is the dynamic sql in the stored procedure and already solved using CAST and CONVERT. Thank you.

  • Quoted string inside dynamic SQL

    Hello
    My version of db: database Oracle 11 g Enterprise Edition Release 11.2.0.1.0 - 64 bit Production

    I run this code in a proc, and it goes to the exception block without running the code below:
    EXECUTE IMMEDIATE
        'SELECT LISTAGG (entitlement, '''','''') WITHIN GROUP (ORDER BY entitlement)
        FROM XMLTABLE (                                 
            ''''/ROWSET/ROW/ENTITLEMENTS/ENTITLEMENT''''      
            PASSING XMLPARSE (DOCUMENT p_xmldoc)        
            COLUMNS ENTITLEMENT VARCHAR2(30) PATH ''''.'''')'                                    
        INTO v_str;
    I made a mistake with quoted strings and I have tried different combinations, but anyway his does not work.
    THA real piece of code is below, whick I'm trying to run immediately in a string
    SELECT LISTAGG (entitlement, ',') WITHIN GROUP (ORDER BY entitlement)                              
                                            FROM XMLTABLE (                                 
                                                '/ROWSET/ROW/ENTITLEMENTS/ENTITLEMENT'      
                                                PASSING XMLPARSE (DOCUMENT p_xmldoc)        
                                                COLUMNS ENTITLEMENT VARCHAR2(30) PATH '.')
    p_xmldoc sample is less than
    '<?xml version="1.0"?>
    <ROWSET>
    <ROW>
    <ENTITLEMENTS>
      <ENTITLEMENT>AMEX</ENTITLEMENT>
      <ENTITLEMENT>JCB</ENTITLEMENT>
    </ENTITLEMENTS>
    </ROW>
    </ROWSET>'
    My goal is to get
    v_str = AMEX, JCB
    Please advice.

    Hello

    Use the notation by Q.
    Assuming that the dynamic control does not contain a right brace immediately before a single quote, you can say:

    
    sql_txt := Q'{SELECT LISTAGG (entitlement, ',') WITHIN GROUP (ORDER BY entitlement) ...}';
    dbms_output.put_line (sql_txt || ' = sql_txt');
    -- EXECUTE IMMEDIATE sql_txt;
    

    With Q-rating, you can't excape double, or otherwise single quotes.

    When you write code dynamic SQL, put the full command in a single VARCHAR2 variable (such as the sql_txt above). During the test, display it. If it looks right, then a comment the line where actually run you it.

  • Dynamic SQL with dynamic identifiers

    Oracle 10gXE

    I have a table that is used to track student attendance. If the student is present on a given day, a record is inserted with the student ID and the date they frequented.

    presence of DESC
    Name of Type Null
    --------------- -------- ------
    ID NOT NULL NUMBER
    NUMBER OF STUDENT_ID
    SITE_ID NUMBER
    DATE OF ATTENDANCE_DATE

    I want to view a report (in TOP) which shows all students for a given site, the days when school was in session for this month (not Saturday, Sunday) and a X for the student who participated in that day or a null value if they do not have.

    ID # FNAME LNAME 3 4 5 6 7 10
    38754636 POE JANE X X X
    81248754 DOE KAYLA X X X X X
    43127409 RAO JOHN X X X X X

    In this example, days 3 and 7 of the month are from Monday to Friday and 8-9 have been excluded because they are on Saturday and Sunday.

    I am trying to find a way to dynamically generate a query for any month/year and returning to a report. The obstacle is that given that I don't know in advance which days in a month will be excluded, the identifiers in my select statement are not fixed.

    I found a query that returns a list of day numbers and dates for a given month. It looks like this:

    SELECT LEVEL lv, TO_DATE (TO_CHAR (LEVEL, '09')
    || TO_CHAR (EXTRACT (TO_DATE('01-SEP-11') MONTHS), '09')
    || To_char (EXTRACT (TO_DATE('01-SEP-11') YEAR), ' 9999'), 'dd.mm.yyyy') mon_day
    OF double WHERE ROWNUM < = EXTRACT (DAY OF LAST_DAY (TO_DATE('01-SEP-11')))
    CONNECT BY LEVEL = ROWNUM

    I combined this request with another brings my student attendance data and results that pivots in what I need. Then, I created a function that accepts the month and year as parameters and creates the combined request.

    SELECT c.ID student_id, c.last_name, c.first_name,.
    MAX (decode (mon_day, ' 01 - SEPT.-11', 'X', NULL)) '1', MAX (decode (mon_day, ' 02 - SEVEN.-11', 'X', NULL)) '2 ',.
    MAX (decode (mon_day, ' 05 - SEPT.-11', 'X', NULL)) '5', MAX (decode (mon_day, ' 06 - SEPT.-11', 'X', NULL)) "6."
    MAX (decode (mon_day, ' 07-SEP-11', 'X', NULL)) '7', MAX (decode (mon_day, ' 08 - SEPT.-11', 'X', NULL)) "8."
    MAX (decode (mon_day, ' 09 - SEPT.-11', 'X', NULL)) '9', MAX (decode (mon_day, 12-SEP-11', 'X', NULL)) "12."
    MAX (decode (mon_day, 13-SEP-11', 'X', NULL)) '13', MAX (decode (mon_day, 14-SEPT-11', 'X', NULL)) "14."
    MAX (decode (mon_day, 15-SEP-11', 'X', NULL)) '15', MAX (decode (mon_day, 16-SEP-11', 'X', NULL)) "16."
    MAX (decode (mon_day, 19-SEP-11', 'X', NULL)) '19', MAX (decode (mon_day, 20-7.-11', 'X', NULL)) "20."
    MAX (decode (mon_day, 21-SEP-11', 'X', NULL)) "21", MAX (decode (mon_day, 22-SEP-11', 'X', NULL)) '22 ',.
    MAX (decode (mon_day, 23-SEP-11', 'X', NULL)) '23', MAX (decode (mon_day, 26-SEP-11', 'X', NULL)) "26."
    MAX (decode (mon_day, 27-SEP-11', 'X', NULL)) '27', MAX (decode (mon_day, 28-SEP-11', 'X', NULL)) "28."
    MAX (decode (mon_day, 29-SEP-11', 'X', NULL)) '29', MAX (decode (mon_day, 30-SEP-11', 'X', NULL)) '30 '.
    (SELECT level lv, TO_DATE (TO_CHAR (LEVEL, '09')
    || TO_CHAR (EXTRACT (TO_DATE('01-SEP-11') MONTHS), '09')
    || To_char (EXTRACT (TO_DATE('01-SEP-11') YEAR), ' 9999'), 'dd.mm.yyyy') mon_day
    OF double WHERE ROWNUM < = EXTRACT (DAY OF LAST_DAY (TO_DATE('01-SEP-11')))
    CONNECTION LEVEL = ROWNUM) a, b of attendance, student c
    WHERE to_char(MON_DAY,'D') NOT IN (1.7)
    AND A.mon_day = b.attendance_date
    AND b.student_id = c.ID
    Group of c.ID, student_id, c.last_name, c.first_name

    Now I'm stuck on what I can pass this request in to get my result. What I've read, dynamic SQL method 4 would work if I incorporate Pro/C. I hope this isn't my only option. I have considered to try to do this in a function table in pipeline, but since the columns returned are not known in advance, I can't create a corresponding data type.

    I'm starting to wonder if I forgot a simpler method to achieve this.

    Published by: David Sumner on April 12, 2012 18:52

    Ignoring the actual query and looking at the dynamic SQL part of the question.

    There are 3 ways to run a dynamic select SQL in PL/SQL code.

    immediate execution requires a fixed number of bind variables, linking him in position and translates a single implicit output cursor extraction - requiring the projection of cursor to be known at the time of coding.

    REF CURSOR are essentially the same - the only exception being that it requires an extraction explicit coding process output cursor.

    DBMS_SQL differs in all of these respects. The connection is by name and not position. The connection is dynamic. Projection of the cursor must not be known at the time of coding. It is dynamically determined at runtime and fetch offers of release of the cursor with the projection of unknown SQL - coding time.

    Apex uses DBMS_SQL internally (in fact he uses DBMS_SYS_SQL properly run dynamic and specific patterns of Oracle SQL, as if a session connected as a scheme runs the SQL code). This allows the Apex to take a dynamic SQL that you enter a region (which can contain a variable number of bind variable), run it and make the projection of the cursor as an HTML report.

    Apex supports in turn also dynamic SQL - what you (Apex developer) to provide a dynamic SQL code for a region, instead of you having to provide SQL fixed (with bind variable) for the region in question.

    As Apex creates DBMS_SQL sliders, it doesn't have your SQL as a ref cursor or cursor DBMS_SQL. All they need is the source of the SQL statement. And that's what you create dynamically.

    So in the Apex, dynamic SQL means using a function from PL/SQL to return the source for Apex SQL analyze like a slider DBMS_SQL - instead of coding the source SQL statement in a fixed statement.

    So, using the option of function for an area considered in the Apex, allows you to provide the following in the code that must run to get the SQL statement for the report Apex (Apex running this as a dynamic function that returns a string):

    --// return the dynamic SQL for the report region
    return(
      case
        when :P1_OPTION = 1 then
          'select * from emp'
    
        when :P1_OPTION = 2 then
          'select * from dept where dept_id = :P1_DEPT_ID'
      end
    );
    

    You can also write a PL/SQL function that is stored in the database and call this function so that it can determine what is the dynamic SQL source code statement. For example

    --// calling database function to return the dynamic SQL for the report region
    return(
      GetDynamicReportSQL( page => 1, option => :P1_OPTION )
    );
    

    Dynamic SQL is fully supported by Apex - but does not require that you, the developer, to create the dynamic cursor and treat the dynamic linking and dynamic recovery of this slider.

    All you have to do is to provide the source code of this dynamic SQL (including the bind variable) to the Apex and it will do the whole thing from cursor for you.

Maybe you are looking for

  • iCloud Photo - need more disk space on my MacBook

    This is my first post, so I sorry if it's really obvious, but I have some don't search online but cannot find an answer to my problem. I hope you guys can help. Ok. Here's my problem. My Macbook Pro has a 250 GB hard drive and is now complete. I noti

  • Portege M400: Since the installation of Vista the advanced instant security does not work

    Since installing vista advanced instant security functions does not have accelerometer HD works fine. Someone at - it suggestions?Does the re - install usual

  • Model hp desktop a622n... updated for Windows 7 video graphics card

    I have a hp a622n office that has a motherboard Micro Star International. The serial number of the motherboard is 4311483428... bus clock mghz 100... Award BIOS software 3 26 05/09/2005 My screen is a Samsung SyncMaster s/n GS19H9NX601526 Intel 82845

  • Flashlight camera enabled to better focus before taking the picture

    How do you thing, wiil be a good solution?Several times my camera did not a good development and ligthinig in the dark, I had a feeling, which did not have enough time to concentrate, it could be solved if there is an option to activate flashligth du

  • Help: WRT160N v3

    Hello I have a WRT160N Version 3 with Time Warner Cable Road Runner in New York and I'm under the 3.001 Firmware.  Yesterday morning, it seems that resets the connection and now my wireless connection is either intermetinent, inactive, or extremely s