Need a test for fetch into from a cursor or an alternative.

I've got a small oddity.
Ultimately it's two cursors that are exactly the same except a part of the WHERE clause. Depending on how many records I am going through (based on a counter) I need to use one cursor or the other as the outer loop, to then process an inner loop.
My problem is I don't quite know how to exit out of the current loop when the fetch reaches the end of the cursor. Meaning, in each of these loops, there's an IF THEN to see which cursor I'm using, and then fetch from the correct one. If I use a FOR X IN CURSOR loop, I'll have to double up the code, one for each outer cursor loop.
What's the test in Oracle for no record found or when it hits the end of a cursor with a fetch? Or am I stuck and I can only use the exception block when the fetch fails?
Thansk!

You can check whether your fetch returned any data with %notfound.
DECLARE
  l_cur   sys_refcursor;
  l_var   VARCHAR2 (30);
BEGIN
  IF TO_CHAR (SYSDATE, 'Day') = 'Monday'
  THEN
    OPEN l_cur FOR
      SELECT dummy
        FROM DUAL;
  ELSE
    OPEN l_cur FOR
      SELECT dummy
        FROM DUAL
       WHERE 1 = 2;
  END IF;
  LOOP
    FETCH l_cur
     INTO l_var;
    EXIT WHEN l_cur%NOTFOUND;
    DBMS_OUTPUT.put_line (l_var);
  END LOOP;
END;
/

Similar Messages

  • Wanted to fetch data from ref cursor to nested pl/sql table getting an erro

    create or replace type "DEPT12" as object(dno number(2),dname varchar2(30),loc varchar2(50));
    create or replace type dept_tab as table of "DEPT12"
    create or replace type "LOC12" as object(locno number,loc_name varchar2(100))
    create or replace type loc_tab as table of "LOC12"
    create or replace type dept_loc_rec1 as object (dept_dt dept_tab,eno number,loc_dt loc_tab);
    create type dept_loc_tb as table of dept_loc_rec1
    create table dept_loc_tb_bk1(dept_dt dept_tab,eno number,loc_dt loc_tab)
    NESTED TABLE dept_dt
    STORE AS dept_tab12,
    NESTED TABLE loc_dt
    STORE AS loc_tab12
    insert into dept_loc_tb_bk1 values(dept_tab(dept12(3,'ABD','LOC')
    ,dept12(4,'ABD','LOC')
    ,dept12(5,'ABD','LOC')),3,loc_tab(loc12(21,'AAB'),
    loc12(22,'AAB'),
    loc12(23,'AAB')));
    when I am trying to fetch data from ref cursor to pl/sql table which i am getting an error ora-06504: pl/sql : Return types of result set variables or query do not match.
    I have created a nested table of same as the nested pl/sql object table dept_loc_tb and i have declared the lv_dept_loc_tb of same dept_loc_tb but getting an above error when trying to fetch into that variable.
    Please any one who can solve my problem.
    declare
    type cr is ref cursor;
    cr_obj cr;
    lv_dept_loc_tb dept_loc_tb;
    begin
    open cr_obj for select dept_dt,eno,loc_dt from dept_loc_tb_bk1;
    fetch cr_obj bulk collect into lv_dept_loc_tb;
    close cr_obj;
    end;

    Your query selects 3 separate columns therefore requires 3 collections of corresponding types. You want to treat those 3 columns as an object of DEPT_LOC_REC1 type:
    SQL> declare
      2  type cr is ref cursor;
      3  cr_obj cr;
      4 
      5  lv_dept_loc_tb dept_loc_tb;
      6 
      7  begin
      8  open cr_obj for select dept_dt,eno,loc_dt from dept_loc_tb_bk1;
      9  fetch cr_obj bulk collect into lv_dept_loc_tb;
    10  close cr_obj;
    11  end;
    12  /
    declare
    ERROR at line 1:
    ORA-06504: PL/SQL: Return types of Result Set variables or query do not match
    ORA-06512: at line 9
    SQL> declare
      2  type cr is ref cursor;
      3  cr_obj cr;
      4 
      5  lv_dept_loc_tb dept_loc_tb;
      6 
      7  begin
      8  open cr_obj for select DEPT_LOC_REC1(dept_dt,eno,loc_dt) from dept_loc_tb_bk1;
      9  fetch cr_obj bulk collect into lv_dept_loc_tb;
    10  close cr_obj;
    11  end;
    12  /
    PL/SQL procedure successfully completed.
    SQL> SY.
    P.S. Discover sys_refcursor.

  • Need SQL code for this logic - -  From Serial and To serial no.

    In Forms 4.5 Screen I have two text boxes where i will be entering From Serial No
    and to serial no.
    I want the code which will check whether any number in between these from and to serial exists in a table or not ?
    For Example
    Select * from t; -- contains
    serial number
    321-456-789
    123-456-654
    321-569-986
    321-569-987
    When I enter from Serial no 321-456-789 and to serial no as 321-456-789.. I should get a message sayinig that 321-456-789 exists.
    When I enter from Serial no 321-569-986 and to serial no as 321-569-988.
    I should get a message sayinig that 321-456-986 exists.
    I should get a message sayinig that 321-456-987 exists.
    I should get a message sayinig that 321-456-988 not exists.
    is it possible with a SQL query or do we need to go for procedure or temp table ?
    If anybody ahving similar code please post it here.
    Thanks in Advance
    Devender

    Hi Devender,
    Try this :
    SQL> select * from t;
    SN
    321-456-789
    123-456-654
    321-569-986
    321-569-987
    SQL> var sn1 varchar2(11)
    SQL> exec :sn1 := '321-569-986'
    Procédure PL/SQL terminée avec succès.
    SQL> var sn2 varchar2(11)
    SQL> exec :sn2 := '321-569-988'
    Procédure PL/SQL terminée avec succès.
    SQL>1  select a.sn, decode(t.sn,null,'does not exists','already exists')
      2  from
      3  (select substr(replace(:sn1,'-','')+rownum-1,1,3)||'-'
      4        ||substr(replace(:sn1,'-','')+rownum-1,4,3)||'-'
      5        ||substr(replace(:sn1,'-','')+rownum-1,7,3) as sn
      6  from (select level
      7        from dual
      8        connect by level <= (replace(:sn2,'-','')-replace(:sn1,'-',''))+1
      9       )
    10  )a,
    11  t
    12* where a.sn=t.sn(+)
    SQL> /
    SN                            DECODE(T.SN,NUL
    321-569-986                   already exists
    321-569-987                   already exists
    321-569-988                   does not exists
    SQL> exec :sn1 := '321-456-789'
    Procédure PL/SQL terminée avec succès.
    SQL> exec :sn2 := '321-456-789'
    Procédure PL/SQL terminée avec succès.
    SQL> l
      1  select a.sn, decode(t.sn,null,'does not exists','already exists')
      2  from
      3  (select substr(replace(:sn1,'-','')+rownum-1,1,3)||'-'
      4        ||substr(replace(:sn1,'-','')+rownum-1,4,3)||'-'
      5        ||substr(replace(:sn1,'-','')+rownum-1,7,3) as sn
      6  from (select level
      7        from dual
      8        connect by level <= (replace(:sn2,'-','')-replace(:sn1,'-',''))+1
      9       )
    10  )a,
    11  t
    12* where a.sn=t.sn(+)
    SQL> /
    SN                            DECODE(T.SN,NUL
    321-456-789                   already exists
    SQL> Nicolas.

  • Need Select statement for fetch the details

    Hi
      i want to fetch particular customer wise sales orderds and that sales order No  and  date of sales order was created..  and  that sales order related PO No  and Materials ..
    for this..   how can i write an executable programm.. is it needed to  define seperate  internal tables or single internal table is enough  ?
    what is the select statement for fetching  all these details..
    any help will be appriciated
    Thanks
    Bbau

    custmer master table is KNA1  knb1  knbk
    sales data tables r  VBAK VBAP VBEP
    Material data  MARA, MARAC, MARD
    PO RELATED ekko  ekpo  eket
    <REMOVED BY MODERATOR>
    Edited by: Alvaro Tejada Galindo on Feb 13, 2008 3:04 PM

  • Add null rows in WDDOINIT  for fetching data from dynamically added rows..

    Hi,,
    I have to fetch data from a dynamically added rows of a table.
    I have followed / gone through many forums but they ddnot mention how to add null rows in the initialization method..
    I am using WD Abap..
    Can anyone help how to bind null rows in WDDOINIT method..?
    Thanks,
    Surya

    Discussed @ SDN lot of time before. Have a look at any of below threads:-
    Dynamic table in interactive form...
    Make dynamic tables in Adobe Interactive forms
    Adding Rows dynamically upon clicking the button
    How to get values to WebDynpro ABAP from dynamic table?
    Chintan

  • Need to test for a Hashtable key in JSTL

    What I am trying to accomplish is to test a Hashtable for a specific key and if it is there run some HTML.
    This is my code but it obviously wrong since JSTL yells at me when I try to execute it.
    <c:set var="hash" scope="page" value="${UpdateStatusData}"/>
         <c:if test="${hash.containsKey('success')}">
              success
         </c:if>UpdateStatusData is a Hashtable. How can I modify this "code" in oder to test for a key?
    Thanks

    EL != java
    Luckily EL handles Maps quite well:
    <c:set var="hash" scope="page" value="${UpdateStatusData}"/>
    // one way
    <c:if test="${not empty hash.success}">
         success
    </c:if>
    // an alternative syntax
    <c:if test="${not empty hash['success']}">
         success
    </c:if>Cheers,
    evnafets

  • How to use BULK INSERT for a data from a cursor?

    Oracle 10G enterprise edition.
    I tried to Bulk insert datas returning from a cursor, its returning error.
    PLS-00302: component 'LAST' must be declared
    I need some help to use the Bulk INSERT here.Can any one help me to specify what error i have made?
    CREATE OR REPLACE PROCEDURE HOT_ADMIN.get_search_keyword_stats_prc
    IS
    CURSOR c_get_scenarios
    IS
    SELECT a.*,ROWNUM rnum
    FROM (
    SELECT TRUNC(r.search_date) sdate,
    r.search_hits hits,
    r.search_type stype,
    r.search_qualification qual,
    r.search_location loc,
    r.search_town stown,
    r.search_postcode pcode,
    r.search_college college,
    r.search_colname colname,
    r.search_text text,
    r.affiliate_id affiliate,
    r.search_study_mode smode,
    r.location_hint hint,
    r.search_posttown ptown,
    COUNT(1) cnt
    FROM w_search_headers r
    WHERE search_text IS NOT NULL
    AND NVL(search_type,' ') <> 'C'
    AND TRUNC(search_date)= TO_DATE(TO_CHAR(SYSDATE-1,'DD-MON-RRRR'))
    GROUP BY TRUNC(r.search_date),
    r.search_hits,
    r.search_type,
    r.search_qualification,
    r.search_location,
    r.search_town,
    r.search_postcode,
    r.search_college,
    r.search_colname,
    r.search_text,
    r.affiliate_id,
    r.search_study_mode,
    r.location_hint,
    r.search_posttown
    ORDER BY cnt desc
    ) a
    WHERE ROWNUM <=1000;
    lc_get_data c_get_scenarios%ROWTYPE;
    BEGIN
    OPEN c_get_scenarios;
    FETCH c_get_scenarios into lc_get_data;
    CLOSE c_get_scenarios;
    FORALL i IN 1..lc_get_data.last
    INSERT INTO W_SEARCH_SCENARIO_STATS VALUES ( i.sdate,
    i.hits,
    i.stype,
    i.qual,
    i.loc,
    i.stown,
    i.pcode,
    i.college,
    i.colname,
    i.text,
    i.affiliate,
    i.smode,
    i.hint,
    i.ptown,
    i.cnt
    COMMIT;
    END;

    This isn't what you asked, but I've generally found it helpful to list the columns in an INSERT statement before the values. It is of course optional, but useful for reference when looking at the statement later

  • Urgent: Help required for fetching CHILDNODE from an XML schema.

    Hi All,
    Good Morning to all, i have problem in extracting the child node name. I have created a xml schema say "emrxmlsample3.xsd" based on this schema i have created a xmltype table and inserted a value to the table. I have a problem in fetching the "CHILDNODE" name. Kindly help me in solving this problem. Here by i am posting all the workings i have done...
    I am using the following client:
    SQL*Plus: Release 10.2.0.1.0 - Production on Mon Jan 31 11:44:59 2011
    Copyright (c) 1982, 2005, Oracle. All rights reserved.
    Connected to:
    Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production
    With the Partitioning, OLAP and Data Mining options
    ////////////////////////////////// XML Schema ///////////////////////
    begin
    dbms_xmlschema.registerSchema(
    'http://www.oradev.com/emrxmlsample3.xsd',
    '<schema xmlns="http://www.w3.org/2001/XMLSchema"
    targetNamespace="http://www.oradev.com/emrxmlsample3.xsd"
    xmlns:samp="http://www.oradev.com/emrxmlsample3.xsd"
    version="1.0">
         <element name="HISTORY">
              <complexType>
                   <sequence>
                        <element name="HPI">
                             <complexType>
                                  <sequence>
                                       <element name = "PAIN">
                                            <complexType>
                                                 <sequence>
                                                      <element name = "SET">
                                                           <complexType>
                                                                <sequence>
                                                                     <element name = "LOCATION">
                                                                          <complexType>
                                                                               <sequence>
                                                                                    <element name = "HEAD" type = "string"/>
                                                                                    <element name = "NECK" type = "string"/>
                                                                                    <element name = "BACK" type = "string"/>                                                       
                                                                                    <element name = "CHEST" type = "string"/>                                                       
                                                                                    <element name = "ABDOMEN" type = "string"/>     
                                                                                    <element name = "JOINTS" type = "string"/>     
                                                                                    <element name = "OTHERS" type = "string"/>
                                                                               </sequence>
                                                                          </complexType>
                                                                     </element>
                                                                     <element name = "SEVERITY">
                                                                          <complexType>
                                                                               <sequence>
                                                                                    <element name = "MILD" type = "string"/>
                                                                                    <element name = "MODERATE" type = "string"/>
                                                                                    <element name = "SEVERE" type = "string"/>                                                       
                                                                                    <element name = "VERYSEVERE" type = "string"/>                                                       
                                                                               </sequence>
                                                                          </complexType>
                                                                     </element>     
                                                                     <element name = "CHARACTER">
                                                                          <complexType>
                                                                               <sequence>
                                                                                    <element name = "DIFFUSE" type = "string"/>
                                                                                    <element name = "PRICKING" type = "string"/>
                                                                                    <element name = "CUTTING" type = "string"/>                                                       
                                                                                    <element name = "PIERCING" type = "string"/>                                                       
                                                                               </sequence>
                                                                          </complexType>
                                                                     </element>                                                                      
                                                                </sequence>
                                                           </complexType>      
                                                      </element>
                                                 </sequence>     
                                            </complexType>
                                       </element>
                                  </sequence>
                             </complexType>
                        </element>
                   </sequence>
              </complexType>
         </element>
         </schema>',
    TRUE, TRUE, FALSE, FALSE);
    end;
    ////////////////////////// Table which has multiple Column //////////////////////////
    CREATE TABLE chipsemr3 (
    id number, CHIPSEMR XmlType)
    XMLTYPE CHIPSEMR STORE AS OBJECT RELATIONAL
    XMLSCHEMA "http://www.oradev.com/emrxmlsample3.xsd"
    ELEMENT "HISTORY";
    ///////////////////////////////// Insert Query in chipsxmltable //////////////////////////
    INSERT INTO chipsemr3 VALUES(1,
    xmltype.createxml('<?xml version="1.0"?>
    <samp:HISTORY xmlns:samp="http://www.oradev.com/emrxmlsample3.xsd" >
              <HPI>
                   <PAIN>
                        <SET>
                             <LOCATION>
                                  <HEAD> YES </HEAD>
                                  <NECK> NO </NECK>
                                  <BACK> N </BACK>
                                  <CHEST> N </CHEST>
                                  <ABDOMEN> N </ABDOMEN>
                                  <JOINTS> N </JOINTS>
                                  <OTHERS> N </OTHERS>
                             </LOCATION>
                             <SEVERITY>
                                  <MILD> N </MILD>
                                  <MODERATE> N </MODERATE>
                                  <SEVERE> N </SEVERE>
                                  <VERYSEVERE> N </VERYSEVERE>
                             </SEVERITY>
                             <CHARACTER>
                                  <DIFFUSE> N </DIFFUSE>
                                  <PRICKING> N </PRICKING>
                                  <CUTTING> N </CUTTING>
                                  <PIERCING> N </PIERCING>
                             </CHARACTER>
                        </SET>
                   </PAIN>
              </HPI>
    </samp:HISTORY>'));
    To show the Root Node Name:
    1. Query
    select x.CHIPSEMR.getrootelement() CHIPSNODES from chipsemr3 x, table(xmlsequence(x.CHIPSEMR.extract('//node()'))) x
    Apologies if the description is not clear. Kindly let me know if further details are needed. Many thanks for your help.
    Very best regards,
    Godwin Jebakumar C.V.

    Hi,
    I'm not sure what you want actually.
    You can get all elements names with :
    SELECT value(x).getRootElement() chipsnodes
    FROM chipsemr3 t,
         TABLE(
           XMLSequence(
             Extract(t.chipsemr, '//*')
         ) x
    ;or, starting with 10.2 :
    SELECT x.*
    FROM chipsemr3 t,
         XMLTable(
          'for $i in //* return local-name($i)'
          passing t.chipsemr
          columns chipsnode varchar2(30) path '.'
         ) x
    ;

  • Need a Query for getting ItemCost from PO Vendor wise

    Hi,Experts We are not maintain different price list for  some particular items , We raised the PO for Multiple vendors with differnt price for the same material. We need a query to find the Item cost with Various Vendor .
    I need the query in the following format
    Po Number, Vendor Name,Item Name, Item Description,Qty, Price,Tax.
    Thanks
    Kamal

    Hi,
    Check this query which looks for the Item doctype Purchase Order and brings the data accordingly :
    select t0.docnum as 'PO Number', t0.CardName as 'Vendor name', t1.itemcode as 'Item Name',
    t1.Dscription as 'Item Description', t1.quantity as 'QTY', t1.Price as 'Price',
    t1.vatsum as 'Tax Amount'
    from OPOR t0 inner join POR1 t1 on t0.docentry = t1.docentry
    where t0.doctype ='I'
    Group by t1.itemcode, t1.Dscription, t0.docnum,
    t0.cardname, t1.quantity, t1.Price, t1.vatsum
    Order by t1.Itemcode
    Check if it helps.
    Kind Regards,
    Jitin
    SAP Business One Forum Team

  • Need some hints for evaluating data from Central Performance History

    Hi Experts,
    I am pretty new to all the BI-stuff and hopefully someone can provide some hints. I got the requirement to read data (CPU usage/peaks) from central performance history of "system a" into "bi system" to generate overviews/charts/etc
    Now I guess there might be a standard report/info cubes and stuff which will help to solve this.
    I would really appreciate if someone could throw me a few keywords about this topic.
    Thanks in advance!

    Hi David,
    Thanks, I hadn't looked at the note.
    Section 2 - history of all connected systems showed me what to do to collect the data. I had seen that in the setup but not understood the implications of it fully. The note explained it much clearly than the SAP help.
    Thanks again,
    Gareth

  • Trouble doing a database upgrade test for SCCM upgrade from SP1 to SP2

    I'm trying to do this test and am having trouble.  I'm following the instructions in
    http://technet.microsoft.com/en-us/library/bb693648.aspx.  However when I enter the command at a command prompt: setup /testdbupgrade e:\database test\sms_hlb_new I get the message: The setup command line option is not valid on this computer. 
    Any help anyone can provide to get around this problem would be appreciated.  I'm not a SQL admin and haven't done an SCCM upgrade before so any tips for a newbie would be great.  Thanks!

    The procedures are basically the same. You can even attach a copy of the database where ConfigMgr is runnig on (be careful to not /testupgrade the wrong one then). You cannot provide a command line parameter to point to a remote SQL server, but you can
    start setup.exe on any machine (so local to the SQL where the copy of the DP is)Torsten Meringer | http://www.mssccmfaq.de

  • Need Transparent background for video created from Photoshop Imageready

    I created a
    a man in imageready walking across the screen with a transparent background. I exported as an .mov like I normally do and converted to a
    n F4v. But when placed in ID, the video displays with a black BG. Can anyone help? thanks

    to get the transparent effect, you actually code it into the
    surrounding .html using "wmode=transparent" Google that and you
    should fine a tutorial. Remember that when you actually put in
    wmode and its value that you need to paste it in there twice (just
    like all the other tags that are already there).

  • Help needed writing trigger for deleting records from multipul tables

    i am trying to write a trigger which would help me delete the record from 3 different tables
    lets say i have table a , b and c
    i an trying to write a trigger which would help me delete the same record from table a and c.
    drop trigger az_zzz_trigger;
    create trigger az_zzz_trigger
    before INSERT or UPDATE or DELETE ON az_employ
    FOR EACH ROW
    BEGIN
    IF DELETING then
    delete from za_payroll
    delete from az_salary_audit
    end if;
    end;
    while executing this trigger all data of table za_payroll is delete.
    what should i do so that only the record which i delete from az_employ gets deleted from az_payroll and az_salary_audit

    872959 wrote:
    i am trying to write a trigger which would help me delete the record from 3 different tables
    lets say i have table a , b and c
    i an trying to write a trigger which would help me delete the same record from table a and c.
    drop trigger az_zzz_trigger;
    create trigger az_zzz_trigger
    before INSERT or UPDATE or DELETE ON az_employ
    FOR EACH ROW
    BEGIN
    IF DELETING then
    delete from za_payroll
    delete from az_salary_audit
    end if;
    end;
    while executing this trigger all data of table za_payroll is delete.
    what should i do so that only the record which i delete from az_employ gets deleted from az_payroll and az_salary_auditutilize appropriate WHERE clause

  • Need mbean details for connecting OIM from SOA

    Hi All,
    we have one requirement where SOA will run an interface whhich contains OIM code. OIM code will update user's end date,send mail to manager and retrun true or false to SOA. I have the code which works fine but now I need to invoke this code from SOA as a wsdl. My question is how SOA will invoke OIM API's? If I rpovide mbean details to SOA then it can run the java code on OIM DB. Please suggest if anybody has worked on this type of requirement.
    Thanks,
    Kalpana.

    Use this:
    http://www.oracle.com/webfolder/technetwork/tutorials/obe/fmw/oim/oim_11g/developing_oim_custom_approval_process_for_resource_provision/developing_oim_custom_approval_process_for_resource_provision.pdf
    They have given example. It's not WSDL but simply using OIM APIs from SOA Workflow.
    They are using Credential Store. Check code of Java Embeded.

  • Beginner question - testing for null string from shell script command

    I'm trying to test the result of a shell script command. I want to put out a message if the result is a null string. At present I get this applescript error "The command exited with a non-zero status"
    I'm sure this is a simple question ... but I can't figure out how to do it. Help!

    You can use try clause for the problem:
    set _result to ""
    try
    set _result to do shell script "/blah/andblah"
    end

Maybe you are looking for