Loop Cursor Fetch Insert Rows Into Global Temporay Table

Hi friends,
I want to know there where is the problem in my this query
cursor c1 is select empno
from scott.emp;
b number;
begin
open c1;
     loop
     fetch c1 into b;
     exit when c1%notfound;
insert into scheema.a1
(a)
values
(b);
commit;
     end loop;
     close c1;
end;
There is no any error displaying while proceeding this query but the table a1 which is (Global temporary table with ON COMMIT PRESERVE ROWS option enabled) does not gets any records. Kindly let me know the problem.
Regards.

Are you sure that the query returns date? Are you checking the table via the same session?
SQL> create global temporary table a1 (
  a  number)
  on commit preserve rows
Table created.
SQL> declare
   cursor c1 is select empno
      from scott.emp;
   b number;
begin
   open c1;
   loop
      fetch c1 into b;
      exit when c1%notfound;
      insert into a1
      (a)
      values
      (b);
      commit;
   end loop;
   close c1;
end;
PL/SQL procedure successfully completed.
SQL> select * from a1
         A
      7369
      7499
      7521
      7566
      7654
      7698
      7782
      7788
      7839
      7844
      7876
      7900
      7902
      7934
14 rows selected.

Similar Messages

  • Insert slow into Global Temporary Table...

    I am working with a stored procedure that does a select into a global temporary table and it is really slow. I have read up on the append hint and know that it is not a solution since GTT's are in the temporary table space which are thus always appended and never have logs.
    Is there something else that I need to know about performance for GTT? I find it hard to believe that Oracle would find it acceptable to take 50 seconds to insert 3300 rows.

    My apologies in advance as my skill level with Oracle is not as high as I would like for this type of analysis and remediation.
    I had thought of it being the select as well but if I run it by itself it takes about 1 second. The interesting part is when I explain plan on it with the Insert, the SQL plan changes.
    Here is the Non-insert explain plan:
    <code class="jive-code jive-java">
    Plan hash value: 3474166068
    | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
    | 0 | SELECT STATEMENT | | 1 | 51 | 8 (38)| 00:00:01 |
    | 1 | HASH GROUP BY | | 1 | 51 | 8 (38)| 00:00:01 |
    | 2 | VIEW | VM_NWVW_1 | 1 | 51 | 7 (29)| 00:00:01 |
    | 3 | HASH UNIQUE | | 1 | 115 | 7 (29)| 00:00:01 |
    | 4 | NESTED LOOPS | | | | | |
    | 5 | NESTED LOOPS | | 1 | 115 | 6 (17)| 00:00:01 |
    | 6 | NESTED LOOPS | | 1 | 82 | 5 (20)| 00:00:01 |
    | 7 | SORT UNIQUE | | 1 | 23 | 2 (0)| 00:00:01 |
    | 8 | TABLE ACCESS BY INDEX ROWID| PEAKSPEAKDAYSEG$METERMASTER | 1 | 23 | 2 (0)| 00:00:01 |
    |* 9 | INDEX RANGE SCAN | IDX_PDSEG$MTR_SEGID | 1 | | 1 (0)| 00:00:01 |
    |* 10 | TABLE ACCESS BY INDEX ROWID | FC_FFMTR_DAILY | 1 | 59 | 2 (0)| 00:00:01 |
    |* 11 | INDEX RANGE SCAN | FC_FFMTRDLY_IDX10 | 2461 | | 2 (0)| 00:00:01 |
    |* 12 | INDEX UNIQUE SCAN | FC_METER_PK | 1 | | 0 (0)| 00:00:01 |
    | 13 | TABLE ACCESS BY INDEX ROWID | FC_METER | 1 | 33 | 1 (0)| 00:00:01 |
    Predicate Information (identified by operation id):
    9 - access("SM"."SEGID"=584)
    10 - filter(TO_DATE(TO_CHAR("V"."MEASUREMENT_DAY"),'YYYYMMDD')>=TO_DATE(' 2002-01-01 00:00:00',
    'syyyy-mm-dd hh24:mi:ss') AND TO_DATE(TO_CHAR("V"."MEASUREMENT_DAY"),'YYYYMMDD')<TO_DATE(' 2003-01-01
    00:00:00', 'syyyy-mm-dd hh24:mi:ss') AND ("V"."ADJUSTED_TOTAL_VOLUME"<>0.0 OR
    ROUND("V"."ADJUSTED_TOTAL_ENERGY",3)<>0.0))
    11 - access("V"."METER_NUMBER"="SM"."METER_ID")
    12 - access("M"."METER_NUMBER"="V"."METER_NUMBER")
    </code>
    Here is the Insert explain plan:
    <code class="jive-code jive-java">
    Plan hash value: 4282493455
    | Id | Operation | Name | Rows | Bytes |TempSpc| Cost (%CPU)| Time |
    | 0 | INSERT STATEMENT | | 39 | 2886 | | 7810 (1)| 00:01:34 |
    | 1 | LOAD TABLE CONVENTIONAL | PEAKDAY_TEMP_CONSECUTIVEVALUES | | | | | |
    | 2 | HASH GROUP BY | | 39 | 2886 | | 7810 (1)| 00:01:34 |
    |* 3 | HASH JOIN RIGHT SEMI | | 39 | 2886 | | 7809 (1)| 00:01:34 |
    | 4 | VIEW | VW_NSO_1 | 1 | 10 | | 2 (0)| 00:00:01 |
    | 5 | NESTED LOOPS | | 1 | 27 | | 2 (0)| 00:00:01 |
    |* 6 | INDEX UNIQUE SCAN | PK_PEAKSPEAKDAYSEG | 1 | 4 | | 0 (0)| 00:00:01 |
    | 7 | TABLE ACCESS BY INDEX ROWID | PEAKSPEAKDAYSEG$METERMASTER | 1 | 23 | | 2 (0)| 00:00:01 |
    |* 8 | INDEX RANGE SCAN | IDX_PDSEG$MTR_SEGID | 1 | | | 1 (0)| 00:00:01 |
    | 9 | VIEW | PEAKS_RP_PEAKDAYMETER | 3894 | 243K| | 7807 (1)| 00:01:34 |
    | 10 | SORT UNIQUE | | 3894 | 349K| 448K| 7807 (1)| 00:01:34 |
    | 11 | NESTED LOOPS | | | | | | |
    | 12 | NESTED LOOPS | | 3894 | 349K| | 7722 (1)| 00:01:33 |
    | 13 | TABLE ACCESS FULL | FC_METER | 637 | 21021 | | 18 (0)| 00:00:01 |
    |* 14 | INDEX RANGE SCAN | FC_FFMTRDLY_IDX11 | 6 | | | 10 (0)| 00:00:01 |
    |* 15 | TABLE ACCESS BY INDEX ROWID| FC_FFMTR_DAILY | 6 | 354 | | 12 (0)| 00:00:01 |
    Predicate Information (identified by operation id):
    3 - access("METER_ID"="METER_ID")
    6 - access("GS"."SEGID"=584)
    8 - access("SM"."SEGID"=584)
    14 - access("M"."METER_NUMBER"="V"."METER_NUMBER")
    filter(TO_DATE(TO_CHAR("V"."MEASUREMENT_DAY"),'YYYYMMDD')>=TO_DATE(' 2002-01-01 00:00:00', 'syyyy-mm-dd
    hh24:mi:ss') AND TO_DATE(TO_CHAR("V"."MEASUREMENT_DAY"),'YYYYMMDD')<TO_DATE(' 2003-01-01 00:00:00', 'syyyy-mm-dd
    hh24:mi:ss'))
    15 - filter("V"."ADJUSTED_TOTAL_VOLUME"<>0.0 OR ROUND("V"."ADJUSTED_TOTAL_ENERGY",3)<>0.0)
    </code>
    As you can see there is a real spike in the cost and yet the only thing that was done was the addition of the Insert to GTT. From what I can ascertain the solution may be in an alternate SQL or finding some way to push Oracle into running the query as it would have for the first execution (non-insert).
    I tried creating a simple view out of the SELECT statement to see if that would precompile it but in the end it ran exactly the same way.
    The next thing that I am going to try is removing the PEAKS_RP_PEAKDAYMETER view by going more direct.
    I have not done the trace file analysis yet. Should I still do that?

  • FETCH multiple rows into different variables

    Dear all,
    i would like to have a loop to fetch 2 rows into 2 separate variables at a time. Does anybody have a clue how could I do this?
    Thanx

    You can't do it with a FOR record IN cursor LOOP. You'll have to do it the old-fashioned way, something like this:
    DECLARE
       CURSOR c1 IS SELECT blah....
       r1 C1%ROWTYPE;
       r2 C1%ROWTYPE;
    BEGIN
       OPEN c1;
       LOOP
          FETCH c1 INTO r1;
          IF C1%NOTFOUND THEN
             EXIT;
          END IF;
          FETCH c1 INTO r2;
          IF C1%NOTFOUND THEN
             EXIT;
          END IF;
          do_some_stuff;
       END LOOP;
       CLOSE c1;
    END;
    /Cheers, APC

  • Need to insert rows into 100 tables at a time

    hi there,
    below is our script for creation of 100 tables...
    we need a plsql script, to insert rows into 100 tables at a single time...
    please help us...vey urgent...
    DECLARE
    counter NUMBER;
    sql_string VARCHAR2(2000);
    BEGIN FOR counter IN 1..100 LOOP sql_string := 'CREATE TABLE emp_table'||counter||'
    (id integer primary key, col_a VARCHAR2(42),col_b date,col_c number,col_d varchar2(20),col_e varchar2(20),
    col_f varchar2(20),col_g varchar2(20),col_h date,col_i varchar2(20),col_j varchar2(20),col_k date)';
    EXECUTE IMMEDIATE sql_string;
    END LOOP;
    END;
    /

    hi,
    below is our procedure and the error we are getting...
    Name Null? Type
    ID VARCHAR2(10)
    COL_A VARCHAR2(10)
    COL_B VARCHAR2(10)
    COL_C VARCHAR2(10)
    COL_D VARCHAR2(10)
    COL_E VARCHAR2(10)
    COL_F VARCHAR2(10)
    COL_G VARCHAR2(10)
    COL_H VARCHAR2(10)
    COL_J DATE
    DECLARE
    counter NUMBER;
    sql_string VARCHAR2(4000);
    BEGIN FOR counter IN 1..100 LOOP sql_string := 'CREATE TABLE emp_a'||counter||'
    (id varchar2(10), col_a varchar2(10), col_b varchar2(10), col_c varchar2(10), col_d varchar2(10), col_e varchar2(10),
    col_f varchar2(10), col_g varchar2(10), col_h varchar2(10), col_j date)';
    EXECUTE IMMEDIATE sql_string;
    END LOOP;
    END;
    DECLARE
    counter NUMBER;
    sql_string VARCHAR2 (2000);
    BEGIN
    FOR OuterCounter IN 1 .. 100 LOOP --- table prefix in which it is to be inserted
    FOR InnerCounter IN 1 .. 100 LOOP --- records to be inserted
    sql_string := 'INSERT INTO emp_a' || Outercounter || ' (id, col_a, col_b, col_c, col_d, col_e, col_f, col_g, col_h, col_j)
    VALUES ('
    || InnerCounter || ', to_char( ''col_a''' || innercounter || '),'
    || InnerCounter || ', to_char( ''col_d''' || innercounter || '),'
    || ', to_char( ''col_e''' || innercounter || '),'
    || ', to_char( ''col_f''' || innercounter || '),'
    || ', to_char( ''col_g''' || innercounter || '),'
    || ', to_char( ''col_h''' || innercounter || '),'
    || ', to_char( ''col_j''' || innercounter || '), SYSDATE)';
    EXECUTE IMMEDIATE sql_string;
    END LOOP;
    END LOOP;
    END;
    DECLARE
    ERROR at line 1:
    ORA-00907: missing right parenthesis
    ORA-06512: at line 17
    please check the procedure and write the correct one...

  • Help in inserting rows into a table

    I have a table called acct_fact,
    I need to insert rows in the table using a script but the problem is there's a column called seq_nbr which has random seq nbr of 14character length like 'ZWX98MGD9MVAD6J','ZWX98MG67RVAD6J' etc.,
    While inserting rows I need to generate such seq_nbr for those columns and insert rows into the table, can I use any such mechanism in my insert query to insert such random nbr's while inserting rows into a table.
    If so please suggest me

    Hi Peter,
    Thankyou for the quick reply:)
    can you suggest me how to implement it here in my script snippet:
    while read var_acct_nbr
    do
    echo "update acct_attr set acct_attr_exp_dt ='$ExpDate' where Acct_Attr_Value_Text='15' and acct_attr_exp_dt is null and person_id='LDCarrBillAgrm' and acct_nbr='$var_acct_nbr' ;" >> ./$DirectoryName/SQLQuery_$TimeStamp.sql
    echo "insert into acct_fact values ('$var_acct_nbr','$ExpDate','$ExpTime','*seq_nbr*','N','ProjTereza','Remoção de acordo d; data de expiração: $ExpDate',null,'1','LDE',null);" >> ./$DirectoryName/SQLQuery_$TimeStamp.sql
    done < ./$DirectoryName/ExpireAccts_$TimeStamp.LOG
    the script takes each acct_nbr nbr form a input file and fires an insert statement.
    The one in bold is the column where such sequence need to be inserted.can you help me in implementing the way you suggested in my script i.e., insert statement
    Thanks in Advance:)
    Edited by: rkrish on Jun 27, 2012 3:04 AM

  • Insert multiple rows into a same table from a single record

    Hi All,
    I need to insert multiple rows into a same table from a single record. Here is what I am trying to do and I need your expertise. I am using Oracle 11g
    DataFile
    1,"1001,2001,3001,4001"
    2,"1002,2002,3002,4002"
    The data needs to be loaded as
    Field1      Field2
    1               1001
    1               2001
    1               3001
    1               4001
    2               1002
    2               2002
    2               3002
    2               4002
    Thanks

    You could use SQL*Loader to load the data into a staging table with a varray column, then use a SQL insert statement to distribute it to the destination table, as demonstrated below.
    SCOTT@orcl> host type test.dat
    1,"1001,2001,3001,4001"
    2,"1002,2002,3002,4002"
    SCOTT@orcl> host type test.ctl
    load data
    infile test.dat
    into table staging
    fields terminated by ','
    ( field1
    , numbers varray enclosed by '"' and '"' (x))
    SCOTT@orcl> create table staging
      2    (field1  number,
      3     numbers sys.odcinumberlist)
      4  /
    Table created.
    SCOTT@orcl> host sqlldr scott/tiger control=test.ctl log=test.log
    SQL*Loader: Release 11.2.0.1.0 - Production on Wed Dec 18 21:48:09 2013
    Copyright (c) 1982, 2009, Oracle and/or its affiliates.  All rights reserved.
    Commit point reached - logical record count 2
    SCOTT@orcl> column numbers format a60
    SCOTT@orcl> select * from staging
      2  /
        FIELD1 NUMBERS
             1 ODCINUMBERLIST(1001, 2001, 3001, 4001)
             2 ODCINUMBERLIST(1002, 2002, 3002, 4002)
    2 rows selected.
    SCOTT@orcl> create table destination
      2    (field1  number,
      3     field2  number)
      4  /
    Table created.
    SCOTT@orcl> insert into destination
      2  select s.field1, t.column_value
      3  from   staging s, table (s.numbers) t
      4  /
    8 rows created.
    SCOTT@orcl> select * from destination
      2  /
        FIELD1     FIELD2
             1       1001
             1       2001
             1       3001
             1       4001
             2       1002
             2       2002
             2       3002
             2       4002
    8 rows selected.

  • Inserting rows into table Based on Column Values.

    Hi,
    I am trying to inserting rows into a table based on Column.
    Id      Name        
    Data
    N 105.1.1
    http://www.example.com/New/105.1.1
    U 105.1.2               http://www.example.com/Used/105.1.2
    S 105.1.3               http://www.example.com/Sold/105.1.3
    I want a table like this. 
    I want to insert Data column value based on Id and Name Columns
    If Id = N and Name = 105.1.1 then Data value should be insert as   http://www.example.com/New/105.1.1
    If Id = U and Name = 105.1.2 then Data value should be  insert as  http://www.example.com/Used/105.1.2
    If Id = S and Name = 105.1.3 then Data value should be insert as   http://www.example.com/Sold/105.1.3
    Can you please help us to write query to insert above Data Column values based on Id and Name Columns.

    INSERT INTO tbl (col) SELECT
    CASE WHEN id='N' and Name='105.1.1 ' THEN 'http://www.example.com/New/105.1.1'
              WHEN id='U' and Name='105.1.2 ' THEN 'http://www.example.com/New/105.1.2'
    END col
    FROM anothertbl
    Sorry , cannot test it right now.
    Best Regards,Uri Dimant SQL Server MVP,
    http://sqlblog.com/blogs/uri_dimant/
    MS SQL optimization: MS SQL Development and Optimization
    MS SQL Consulting:
    Large scale of database and data cleansing
    Remote DBA Services:
    Improves MS SQL Database Performance
    SQL Server Integration Services:
    Business Intelligence

  • Create Global Temporay Table

    I am converting some Informix ESQL/C programs to Oracle Pro*C.
    However the "Create Global Temporay Table" statemnt does not seem to be recognized by Pro*C precompiler.
    Does anyone know why?
    Thank You

    Note that DDL commands such as create table cannot be directly used in PLSQL you may be able to use execute immediate, the workbench uses a wrapper function for DBMS_SQL
    so showing the error:
    SQL> begin
    2 CREATE GLOBAL TEMPORARY table fred ( myint integer) ON COMMIT PRESERVE ROW
    3 end;
    4 /
    CREATE GLOBAL TEMPORARY table fred ( myint integer) ON COMMIT PRESERVE ROWS;
    ERROR at line 2:
    ORA-06550: line 2, column 1:
    PLS-00103: Encountered the symbol "CREATE" when expecting one of the following:
    begin declare exit for goto if loop mod null pragma raise
    return select update while <an identifier>
    <a double-quoted delimited-identifier> <a bind variable> <<
    close current delete fetch lock insert open rollback
    savepoint set sql execute commit forall
    <a single-quoted SQL string>
    showing our procedure wrapper:
    CREATE OR REPLACE PROCEDURE DDL_Manager(in_statement varchar)
    AUTHID CURRENT_USER AS
    PRAGMA AUTONOMOUS_TRANSACTION;
    v_CursorID INTEGER;
    v_RowsReturned INTEGER;
    BEGIN
    v_CursorID := DBMS_SQL.OPEN_CURSOR;
    DBMS_SQL.PARSE(v_CursorID, in_Statement, DBMS_SQL.NATIVE);
    v_RowsReturned := DBMS_SQL.EXECUTE(v_CursorID);
    DBMS_SQL.CLOSE_CURSOR(v_CursorID);
    EXCEPTION
    WHEN OTHERS THEN
    DBMS_SQL.CLOSE_CURSOR(v_CursorID);
    RAISE;
    END DDL_Manager;
    showing wrapper being used:
    begin
    DDL_Manager('CREATE GLOBAL TEMPORARY table fred ( myint integer) ON COMMIT PRESERVE ROWS');
    end
    In the oracle migration workbench the create table statements are done before any stored procedures are executed, so that stored procedures will not be invalid due to tables not being created yet. If all tables were referenced through dynamic sql the tables could be created on the fly.
    Turloch
    Oracle Migration Workbench Team

  • How many records are  fetched my query into my internal tables

    Hi
    I am in the debugger, would like to know, how many records are  fetched my query into my internal tables. How do I  find out ?
    thanks
    siva

    Hi,
    Do the following,
    Step 1:
      Fill your internal table with select query.
    Step 2 : Use DESCRIBE statement with LINE addition to get the no of rows in the internal table.
    for further informations, check
    http://help.sap.com/saphelp_nw70/helpdata/en/fc/eb3798358411d1829f0000e829fbfe/content.htm
    Regards,
    Anirban

  • How to insert data into the mysql table by giving as a text file

    Hi,
    Any one know's how to insert data into the mysql table by giving as a text file as the input in JSP.Please respond ASAP.
    Thanks:)

    At least you can try StringTokenizer to parse your text files. Or download a text JDBC driver to parse your files, for instance, HXTT Text(www.hxtt.net) or StelsCSV(www.csv-jdbc.com).

  • Files moving to NFS error folder - Could not insert message into duplicate check table

    Hi Friends
    Have anyone faced this error, could suggest me why.
    The CSV Files failed on Sender Channel and moves to NFS error path  and in the log it says as below.
    Error: com.sap.engine.interfaces.messaging.api.exception.MessagingException: Could not insert message  into duplicate check table. Reason: com.ibm.db2.jcc.am.SqlTransactionRollbackException: DB2 SQL Error

    Hi Uma - is that a duplicate file? have you enabled duplicate file check in sender channel?
    please check if the below note is applicable
    1979353 - Recurring TxRollbackException with MODE_STORE_ON_ERROR stage configuration

  • Urgent help needed in inserting data into a custom table in oracle WorkFlow

    Hi
    I am trying to get data from the WF and insert into a custom table..
    I read that workflow procedures WONT allow commits...
    Oracle Workflow will not support autonomous commits in any procedure it calls directly. If you need to perform commits, then embed your SQL in a subprocedure and declare it as an autonomous block. This subprocedure must be capable of being rerun. Additionally, note that Oracle Workflow handles errors by rolling back the entire procedure and setting its status to ERROR. Database updates performed by autonomous commits cannot be rolled back, so you will need to write your own compensatory logic for error handling
    Have anyone did this..
    Please give me some idea ...It is urgent
    I am getting data using getitemattribute..and try to insert it into a custom table
    thanks
    kp

    Pl do not post duplicate threads - insert dont work in Workflow
    Srini

  • GLOBAL TEMPORAY TABLE  with the ability for a user to see others's entries

    I have a list of record available for many user at the same time for them to work on.
    Every one has the tendency to chosse the first record...
    I want each of the users to be able to chose a different record once the preceding record has been selected by another user.
    It would have been somthing like a GLOBAL TEMPORAY TABLE but with the ability for every user to see others records
    so I would do and anti-join...
    Does anyon have an idea on how I could implement such a behavior?

    You will not be able to use a global temporary table for such a requirement.
    It sounds like you will need a permanent table, which you will need to lock certain records (prohibiting users from taking those already in use), you can look at using the DBMS_LOCK package for this.
    That's just speculation based on what you've typed, if you provide more details you make get more specific help.

  • Insert data into msql thru table trigger

    Hi,
    I installed dg4msql 11.2 and I would like to insert some data into our msql thru one of our Oracle table after insert table,
    I used the syntax below
    insert into test@dg4msql ("PN") values (:new.PN)
    but the below error problem if I insert data into my Oracle table,
    ORA-02054: transaction 4.17.15898 in-doubt
    ORA-028500, connect from ORACLE to a non-Oracle system returned this message
    [Oracle][ODBC SQL Server Driver][SQL Server] Invalid object name "RECOVER.HS_TRANSACTION_LOG
    Do I have to create the HS_TRANSACTION_LOG table in my msql server to resolve this problem?
    Thanks
    Vincent

    Vincent,
    If the gateway insert is being done as part of a trigger then it will almost certainly be part of a distributed transaction so yes, you will have to create the transaction log table and the recovery user in the SQL*Server database.
    As well as the gateway documentation have a look at this note in My Oracle Support -
    Note.227011.1 How to Setup DG4MSQL to Use Distributed Transactions          (Doc ID 227011.1)
    Regards,
    Mike

  • Drop, create and insert data into few intermediate tables

    Hi All,
    I need to schedule a process to drop, create and insert data into few intermediate tables on a weekly basis. Here is what i need to do in the stored procedure, which can be scheduled weekly.
    DROP TABLE TABLE_NAME1;
    DROP TABLE TABLE_NAME2;
    DROP TABLE TABLE_NAME3;
    CREATE TABLE TABLE_NAME1
    CREATE TABLE TABLE_NAME2
    CREATE TABLE TABLE_NAME3
    INSERT INTO TABLE_NAME1 SELECT ....;
    INSERT INTO TABLE_NAME2 SELECT ....;
    INSERT INTO TABLE_NAME3 SELECT ....;
    Any suggestions, examples or code on how to accomplish this task would be very helpful. Any question pls let me know.
    Thanks in advance.

    I am using the intermediate tables in an extract process. The idea was that the table would be created prior to calling the extract procedure and once the data written to the intermediate table had been processed the table would be dropped. This would be repeated each time the extract process is run. From a DBA's point or view, would it be better to just leave the table on the database and truncate it after each run or is removing it entirely best?

Maybe you are looking for