Create cursor from another cursor

Hello,
How do I create a cursor from the values of another cursor ?
For Example,
cursor c1
select x, y from table 1;
cursor c2
select c1.x, c1.y, table 2.z
from table 2
where y := c1.y
I get errors when I do this way. Can somebody help me out on this.
Thanks
Raj

In this particular example, it's probably easiest to declare a single cursor that contains a join, i.e.
cursor c1
SELECT a.x, a.y, b.z
  FROM table1 a,
       table2 b
WHERE a.y = b.yIf you really want to create the two cursors, though, you have to make sure that the first cursor is visible to the creation statement of the second. For example,
for c1 in (select x, y from table1)
loop
  for c2 in (select c1.x, c1.y, b.z from table2 b where b.y = c1.y)
  loop
  end loop;
end loop;If you need more help, quoting the exact error an providing a snippet of the code you're using would be greatly helpful.
Justin

Similar Messages

  • Using a Cursor in another cursor

    Hi Guys,
    I have declared a cursor in one of my program unit in forms 6i form, like this:
    Declare
    Cursor C1 is select employee_id, emp_name from employees;
    Begin
    End;
    Now i have to declare an other cursor that will be using the employee_id of the cursor C1.
    for example :
    Declare
    Cursor C1 is select employee_id, emp_name from employees;
    Cursor C2 is select C1.Employee_id, Employees.Age
    from C1, employees;
    Begin
    End;
    I dont think we can do this.... as it gives error in forms. What is the alternate how can i reference a cursor in another cursor??
    Kindly Help Pliz, Imran Baig

    Imran,
    you can do this. The way you try to read from a cursor is not correct because you treat a cursor like a table which it isn't. You have to loop through the parent cursor and then within this loop, loop the second cursor. Please see the PLSQL documentation on OTN on how to do this (its not that big of a deal).
    Frank

  • Problem with creating site from another computer!

    Hi
    I have WebDB 2.2 and Oracle8i instaled on the same computer. I have a problem to create site from another computer. There is not any process when I try to create site. There is always 0%.
    But I have no problem with creating site on the origin computer.
    Does anybody know something about it?

    I am just not clear what to do once I have made changes and have published the new information.
    You shouldn't have to do anything. iWeb will save the information in the Domain.sites file. You can keep this file in a folder, or even on your desktop and iWeb will launch from a double-click on the file.

  • Populate a REF CURSOR from regular cursor...

    Hi all,
    I apologize if the answer to this is somewhere...I've been looking on the web for sometime and can't find an answer to the following problem. I have a Significant Events database that contains network based issues and problems. As problems are detected on the network an SE is issued and published. As the SE records are updated, NEW records are entered into the table and "linked" back to the original. Each update results in a new row. Thus, an SE with two updates would have a total of 3 lines. When the SE gets closed (set the column CLOSED to SYSDATE), only the "original" SE is closed, any updates are left open...aka, the CLOSED column is left null.
    That said, I need a way to get the original and/or latest updated SE rows from the table. Thus, I am trying to use a PL/SQL package. The PL/SQL "must" return a REF CURSOR as the results are being passed to a client process.
    My initial approach was within a PL/SQL procedure, I have an SQL statement that returns the SE originals. Once in that cursor I need to do the following:
    - Attempt to fetch any linked SE rows.
    - if no rows then
    - add the original to the REF CURSOR.
    - else
    - find latest SE update
    - add latest SE update to REF CURSOR.
    - end if
    My Question is : How do I manually "add" a row to a REF CURSOR?
    If this is not possible, is there a way to populate a REF CURSOR from maybe another construct like:
    TYPE ian_se_record is RECORD (
    se_id number
    ,linked_se_id number
    ,submitted date
    ,updated date
    ,closed date
    ,segroup varchar2(150)
    ,incident_start_time varchar2(150)
    ,business_units_affected varchar2(150)
    ,officenum varchar2(1500)
    ,sedetails varchar2(4000)
    TYPE ian_se_table is table of ian_se_record index by binary_integer;
    With the above construct I could:
    - Fill ian_se_table with the process described above.
    - And finally select off ian_se_table into the REF CURSOR?
    Any help would be greatly appreciated,
    adym

    Hi michaels,
    I've put your solution in place, but can't seem to get it to run. The two types were moved out of the package and into real types as you said. Here's the function, for brevity, I've remove some of the less important code:
    function ian_se_fetch return sys_refcursor
    is
        p_csr_events            sys_refcursor;
        cursor csr_items is
            select
                 se_id
        ...removed for brevity...
        /* END : csr_items  */
        ian_se_row     ian_se_record;
        ian_se_tbl     ian_se_table;
        l_lng_index    number;
        l_lng_linked   number;
        l_lng_id       number;
    begin
         * OPEN : Open the main cursor of originals...
        for the_item in csr_items loop
             * CHECK : Check for any updates to the original...
            l_lng_linked := 0;
            select count(*)
            into l_lng_linked
            from sig_se_t src
            where src.linked_se_id = the_item.se_id;
            l_lng_id := 0;    /* reset the se-id    */
            /* SE original...no linked records yet.    */
            if ( l_lng_linked = 0 ) then
                l_lng_id := the_item.se_id;
            /* SE updates...one or more updates are present.    */
            else
                begin
                    select
                         se_id
                    into l_lng_id
                    from sig_se_t src
                    where src.linked_se_id = the_item.se_id
                      and rownum = 1
                    order by updated desc; /* latest update    */
                exception
                    when too_many_rows then
                        l_lng_id := the_item.se_id;
                    when others then
                        l_lng_id := 0;
                end;
            end if;
            if ( l_lng_id != 0 ) then
                select
                     se_id
                    ,linked_se_id
                    ,submitted
                    ,updated
                    ,closed
                    ,segroup
                    ,incident_start_time
                    ,business_units_affected
                    ,officenum || decode( nvl(impact,'1')
                                      ,'1',''
                                      ,decode(impact
                                          ,'NA', ''
                                          ,':' || impact
                                  )                           impact
                    ,sedetails
                into ian_se_row.se_id
                    ,ian_se_row.linked_se_id
                    ,ian_se_row.submitted
                    ,ian_se_row.updated
                    ,ian_se_row.closed
                    ,ian_se_row.segroup
                    ,ian_se_row.incident_start_time
                    ,ian_se_row.business_units_affected
                    ,ian_se_row.officenum
                    ,ian_se_row.sedetails
                from sig_se_t src
                where src.se_id = l_lng_id;
                l_lng_index := nvl(ian_se_tbl.last,0)+1;
                ian_se_tbl(l_lng_index).se_id                   := ian_se_row.se_id;
                ian_se_tbl(l_lng_index).linked_se_id            := ian_se_row.linked_se_id;
                ian_se_tbl(l_lng_index).submitted               := ian_se_row.submitted;
                ian_se_tbl(l_lng_index).updated                 := ian_se_row.updated;
                ian_se_tbl(l_lng_index).closed                  := ian_se_row.closed;
                ian_se_tbl(l_lng_index).segroup                 := ian_se_row.segroup;
                ian_se_tbl(l_lng_index).incident_start_time     := ian_se_row.incident_start_time;
                ian_se_tbl(l_lng_index).business_units_affected := ian_se_row.business_units_affected;
                ian_se_tbl(l_lng_index).officenum               := ian_se_row.officenum;
                ian_se_tbl(l_lng_index).sedetails               := ian_se_row.sedetails;
            end if;
        end loop;
         * REF CURSOR : Open the ref cursor on the dataset...
        if ( nvl(ian_se_tbl.last,0) = 0 ) then
            p_csr_events := null;
        else
            open p_csr_events for
            select *
            from table (cast ( ian_se_tbl as ian_se_table ));
        end if;
        return p_csr_events;
    end;Here's the test. I keep getting the same error ORA-06530:
    SQL> variable v refcursor;
    SQL> exec :v:=pkg_ian.ian_se_fetch;
    BEGIN :v:=pkg_ian.ian_se_fetch; END;
    ERROR at line 1:
    ORA-06530: Reference to uninitialized composite
    ORA-06512: at "N0002501.PKG_IAN", line 131
    ORA-06512: at line 1
    SQL> print v
    ERROR:
    ORA-24338: statement handle not executedOther things I tried:
    - The ian_se_fetch() function was a procedure using an in out parameter...same error.
    - Wrote a small anonymous block and tried to LOOP/FETCH. Same ORA-06530 error.
    P.S. Line 131 of pkg_ian is the SELECT ... INTO ian_se_row.se_id, ...
    Any help would be greatly appreciated,
    tia,
    adym
    Message was edited by:
    alink

  • I can't create table from another table?

    Hi everyone!
    I have a problem that I don't known the reason why?
    I'm using Oracle version 8i and I want to create a table from another table, such as:
         CREATE TABLE a_backup as SELECT * FROM a => It's OK, table a_backup is created.
    But there is only a table that I can't created like that, such as:
         CREATE TABLE b_backup AS SELECT * FROM b;
    When I run over command, SQL Plus is not responding... and clients are can't access to DB or Executing forever
    This is the first time I met this problem.
    Can Anyone help me to resolved it?
    Thanks in advance!

    xi`tin wrote:
    Hi everyone!
    I have a problem that I don't known the reason why?
    I'm using Oracle version 8i and I want to create a table from another table, such as:You realize, of course, that 8i is completely out of support .... Is your OS and hardware just as old as your rdbms software, or is it only the rdbms that your company refuses to upgrade?
         CREATE TABLE a_backup as SELECT * FROM a => It's OK, table a_backup is created.
    But there is only a table that I can't created like that, such as:
         CREATE TABLE b_backup AS SELECT * FROM b;
    When I run over command, SQL Plus is not responding... and clients are can't access to DB or Executing forever
    This is the first time I met this problem.
    Can Anyone help me to resolved it?
    Thanks in advance!

  • Create table from another table including constraints

    Hi,
    Is there a way to create a table from another table including constraints.
    CREATE TABLE COPY_EMP
    as
    SELECT *
    FROM EMP
    WHERE 1 =2 ;
    This creates the table, but the constraints are not copied over.
    I was reading about DBMS_REDEFINITION - can that be used for this scenario ?
    Thanks!
    Anand

    >
    I tried that, but the constraint names are posing a problem. And unfortunately our constraints are not named in a standard, so am finding it difficult to replace them.
    Was just wondering if there were any simpler approach to this.
    >
    No - there isn't.
    You will have to use new names for the constraints. That usually means extracting the DDL and manually changing the constraint names.

  • How to create table from another in pl/sql

    Hi I need to create a table from another in pl/sql
    How can I do this

    The proper way to do this, is not to do it in PL/SQL. But do it in SQL, something like:
    create table tbl
    as
    select *
      from other_tbl;Doing it in PL/SQL is really slow compared to SQL.
    Yes, if you really want to create a table using PL/SQL then you will need to use DBMS_SQL or EXECUTE IMMEDIATE (Native Dynamic SQL) to do this.

  • Creating playlist from another playlist

    So, I have all the latest software etc. Can I tell you what ***** about itunes? Remember when making a playlist you could access other playlists and pluck songs to add to your new playlist? You can't do that anymore. WHY?! Itunes Master, please bring this option back!

    I'm assuming you're referring to Music in iOS, not iTunes on Mac, right?  I'm extremely upset that this feature was removed.  I have 25,000 songs, so I use playlists in iOS to sort them a bit.  No longer having the ability to pull from those playlists to create or modify another has just taken me back 5 years.  I have to create and modify everything from my Mac now, as I did long ago.

  • Creating Contract from another contract

    hi
    is it possible to create a contract from another contract as a reference? Can i use the expired contracts again after changing its validty dates?
    thanks
    maniraj

    Hi
    It is possible to create a contract from another contract in MM.
    Please follow the steps
    1) Go to t code ME31K
    2) Initial screen press Copy document button
    3) Give the reference document no and mention the reference items ( from & to)
    4) Press enter
    Now you can see the items copied from the old contract.
    Hoep it helps.
    Thanks /Karthik
    Edited by: Karthik on Aug 13, 2009 9:27 AM

  • Return package cursor from another store procedure

    Hello
    I’m new in Oracle, and I have the following problem:
    I have an cursor in a package defined like that:
    create or replace
    PACKAGE "TestPACKAGE" AS
    cursor DOWNLOAD_CURSOR is select A.* from Table1 A, Table2 B
    END TestPACKAGE;
    In reality there is more than one package with different cursors. I want to use that DOWNLOAD_CURSOR in a store procedure, function or another package, to return it to the client as a ref cursor.
    Something likes that:
    create or replace
    procedure aa_test (retCURSOR OUT sys_refcursor) is
    begin
    retCURSOR := TestPACKAGE.DOWNLOAD_CURSOR;
    end;
    or to replace that procedure with another method to return data, and use them in a .NET application.

    961449 wrote:
    Hello
    I’m new in Oracle, and I have the following problem:
    I have an cursor in a package defined like that:
    create or replace
    PACKAGE "TestPACKAGE" AS
    cursor DOWNLOAD_CURSOR is select A.* from Table1 A, Table2 B
    END TestPACKAGE;
    In reality there is more than one package with different cursors. I want to use that DOWNLOAD_CURSOR in a store procedure, function or another package, to return it to the client as a ref cursor.
    Something likes that:
    create or replace
    procedure aa_test (retCURSOR OUT sys_refcursor) is
    begin
    retCURSOR := TestPACKAGE.DOWNLOAD_CURSOR;
    end;
    or to replace that procedure with another method to return data, and use them in a .NET application.Static PL/SQL cursor declarations and Ref Cursors are not interchangable. You cannot return the static cursor definition as a ref cursor.
    See...
    SQL> ed
    Wrote file afiedt.buf
      1  declare
      2    v_rc sys_refcursor;
      3    cursor cur_emp is select * from emp;
      4  begin
      5    open v_rc for cur_emp;
      6* end;
    SQL> /
      open v_rc for cur_emp;
    ERROR at line 5:
    ORA-06550: line 5, column 17:
    PLS-00320: the declaration of the type of this expression is incomplete or malformed
    ORA-06550: line 5, column 3:
    PL/SQL: Statement ignored

  • How to create table from ref cursor

    I have a proc that returns a ref cursor, whats the simplest way to create a table based on the return of the ref cursor?
    declare
    type rc is ref cursor;
    p_data rc;
    begin
    call_my_proc(p_data);
    :result := p_data; -- If I run this in TOAD I can see the data here but I want to create a table based on it rather than outputting it)
    end;
    thanks.
    edit: sorry. typed this wrong first time, should be right now

    961469 wrote:
    I have a proc that returns a ref cursor, whats the simplest way to create a table based on the return of the ref cursor?Not to do it...
    A cursor is not a result set. A cursor is not a result set. Worth repeating several times as this is a common misconception
    A cursor is essentially a program. Executable code that was compiled from a SQL source code program.
    A SELECT cursor is "read" program. Each fetch instruction runs this program (from its current "paused state"), and outputs data.
    An INSERT cursor is a "write" program. You pass data to it (process called binding, via bind variables). You then execute the program. It writes the data it received (can be bulk data via a bulk bind) to table.
    Now your question is: How do I write the output of a cursor program back to the database?
    The answer is that a "write" cursor program is needed. Your code needs to execute (fetch output from) the ref (read/select) cursor. Then bind that data to the "write" cursor and execute it.
    In other words, you have a read cursor and a write cursor, and you need to pass data from one to the other.
    HOWEVER.. This is slow. This does not scale. This is also known as slow-by-slow row by row processing.
    The correct approach is to create a single program. One that reads the data, and then writes the data. No to send data via a detour through your code between the read part and write part.
    The cursor to create is an INSERT..SELECT cursor. This can do fast direct path inserts. This can be executed in parallel - i.e. the database executing several copies of this read-and-write program at the same time.

  • Create table from another db

    Basically, I need to create a table from a query that runs against another db on a separate box. Is this possible?
    Thanks

    If you're able to create a database link to the other server you could then use the query(modifying the tables in the from clause to use the dblink) in a create table statement.

  • Question on Creating table from another table and copying the partition str

    Dear All,
    I want to know whether is there any way where we can create a table using another table and have the partitions of the new table to be exactly like the used table.
    Like
    CREATE TABLE TEST AS SELECT * FROM TEMP;
    The table TEMP is having range and hash partitions.
    Is there any way when we use the above command, we get the table partitions of TEMP to be copied to the new table TEST.
    Appreciate your suggestions on this one.
    Thanks,
    Madhu K.

    may this answer your question...
    http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:595568200346856483
    Ravi Kumar

  • Cursor in another cursor. how?

    i have a program like this
    DECLARE
    V_VARIABLE1 VARCHAR2(10);
    CURSOR_ABC IS select...............;
    BEGIN
    OPEN CURSOR_ABC;
    LOOP
    FETCH CURSOR_ABC INTO V_VARIABLE1;
    EXIT WHEN CURSOR_ABC%NOTFOUND;
    WHAT IF I WANT TO INSERT ANOTHER SAME KINDA CURSOR LOOP HERE LIKE*
    CURSOR_XYZ IS select...............;
    BEGIN
    OPEN CURSOR_XYZ;
    LOOP
    FETCH CURSOR_XYZ INTO V_VARIABLE2;
    EXIT WHEN CURSOR_XYZ%NOTFOUND;
    END LOOP;
    CLOSE CURSOR_XYZ;
    END LOOP;
    CLOSE CURSOR_ABC;

    Gul wrote:
    i have a program like this
    DECLARE
    V_VARIABLE1 VARCHAR2(10);
    CURSOR_ABC IS select...............;
    BEGIN
    OPEN CURSOR_ABC;
    LOOP
    FETCH CURSOR_ABC INTO V_VARIABLE1;
    EXIT WHEN CURSOR_ABC%NOTFOUND;
    WHAT IF I WANT TO INSERT ANOTHER SAME KINDA CURSOR LOOP HERE LIKE*
    CURSOR_XYZ IS select...............;
    BEGIN
    OPEN CURSOR_XYZ;
    LOOP
    FETCH CURSOR_XYZ INTO V_VARIABLE2;
    EXIT WHEN CURSOR_XYZ%NOTFOUND;
    END LOOP;
    CLOSE CURSOR_XYZ;
    END LOOP;
    CLOSE CURSOR_ABC;It is your code & you are free to implement what ever you desire.
    CURSOR LOOPS are row by row & SLOW by SLOW!
    How do I ask a question on the forums?
    SQL and PL/SQL FAQ

  • Create script from another script using PROMPT

    Oracle 11
    Instead of having to link to another database I want to create 2 scripts that are executed from a shell script and run them separately. Script1 run in Database1 would create Script 2 by using PROMPT to create the second script while grabbing a date from database1 to pass into database2. I almost have it but the problem is ... Script2 uses COPY FROM and I have to put a "-" at the end of each line which the PROMPT takes as a literal instead of as part of the output.
    SCRIPT1:
    set pagesize 0
    set trimspool on
    set linesize 2000
    set echo off
    set verify off
    set feedback off
    set sqlblanklines on
    spool /home/michelle/sql_scripts/sl_dtc_copy2.sql
    PROMPT whenever sqlerror exit sql.sqlcode rollback
    PROMPT SET ARRAYSIZE 5000
    PROMPT SET COPYCOMMIT 20
    PROMPT set linesize 4000
    PROMPT
    PROMPT COPY FROM {username}/{pswd}@{DB2}  TO {username}/{pswd}@{DB2}
    PROMPT CREATE SL_DTC_TMP USING SELECT DISTINCT
    PROMPT col1, col2, col3
    PROMPT FROM dual
    PROMPT WHERE veh_data_elements_cd = 'ODO_READ'
    PROMPT and       dh.data_collected_timstm >= 
    select trunc(sysdate)-7 from dual;
    PROMPT and       substr(vdh.vin,10,1) in ('A','B','C','D')
    PROMPT /
    PROMPT spool off
    PROMPT exit
    spool off
    exitSCRIPT2 NOW:
    whenever sqlerror exit sql.sqlcode rollback
    SET ARRAYSIZE 5000
    SET COPYCOMMIT 20
    set linesize 4000
    COPY FROM {username}/{pswd}@{DB2}  TO {username}/{pswd}@{DB2}
    CREATE SL_DTC_TMP USING SELECT DISTINCT
    col1, col2, col3
    FROM dual
    WHERE veh_data_elements_cd = 'ODO_READ'
    and       dh.data_collected_timstm >=
    12-AUG-11
    and       substr(vdh.vin,10,1) in ('A','B','C','D')
    spool off
    exitSCRIPT2 HOW IT NEEDS TO BE:
    --- A [SPACE] AND A [-] at the end of each line
    --- THE DATE NEEDS TO HAVE SINGLE QUOTES AND PREFERABLY AT THE END OF THE PREVIOUS LINE
    whenever sqlerror exit sql.sqlcode rollback
    SET ARRAYSIZE 5000
    SET COPYCOMMIT 20
    set linesize 4000
    COPY FROM {username}/{pswd}@{DB2}  TO {username}/{pswd}@{DB2} -
    CREATE SL_DTC_TMP USING SELECT DISTINCT -
    col1, col2, col3 -
    FROM dual -
    WHERE veh_data_elements_cd = 'ODO_READ' -
    and       dh.data_collected_timstm >= '12-AUG-11' -
    and       substr(vdh.vin,10,1) in ('A','B','C','D')
    spool off
    exitTHANK YOU

    Try
    select 'whenever sqlerror exit sql.sqlcode rollback'                    ||chr(10)||
           'SET ARRAYSIZE 5000'                                             ||chr(10)||
           'SET COPYCOMMIT 20'                                              ||chr(10)||
           'set linesize 4000'                                              ||chr(10)||
           ' '                                                              ||chr(10)||
           'COPY FROM {username}/{pswd}@{DB2}  TO {username}/{pswd}@{DB2} -'||chr(10)||
           'CREATE SL_DTC_TMP USING SELECT DISTINCT -'                      ||chr(10)||
           'col1, col2, col3 -'                                             ||chr(10)||
           'FROM dual -'                                                    ||chr(10)||
           'WHERE veh_data_elements_cd = ''ODO_READ'' -'                    ||chr(10)||
           'and       dh.data_collected_timstm >= ''12-AUG-11'' -'          ||chr(10)||
           'and       substr(vdh.vin,10,1) in (''A'',''B'',''C'',''D'')'
           as Script
    from dual

Maybe you are looking for