Cursor Expression Select Statement

SELECT DNAME, CURSOR(SELECT SAL,COMM FROM EMP E WHERE E.DEPTNO = D.DEPTNO) FROM DEPT D
DNAME CURSOR(SELECTSAL,COM
ACCOUNTING CURSOR STATEMENT : 2
CURSOR STATEMENT : 2
SAL COMM
2450
5000
1300
RESEARCH CURSOR STATEMENT : 2
CURSOR STATEMENT : 2
SAL COMM
800
2975
3000
1100
3000
SALES CURSOR STATEMENT : 2
CURSOR STATEMENT : 2
SAL COMM
1600 300
1250 500
1250 1400
2850
1500 0
950
6 rows selected.
OPERATIONS CURSOR STATEMENT : 2
CURSOR STATEMENT : 2
no rows selected
Can we get rid of "CURSOR STATEMENT : 2"(Also I don't know what is its
significance there) from the output so that the output is in a readable format.
Thanks,
Neeraj Goel

I want Data in the Format Like
deptno cnt1
cnt2
cntN
deptno cnt1
cntM
when you have a parent table (dept) and you sometimes
want to see the child rows (emp) but "on demand". For example, you fetch
"select deptno, cursor( from emp ) from dept" and the user says "hmmm, I'd like
to see the emps in deptno 20" -- great, you just fetch that cursor and display
them -- you didn't have to bring everyone else back with that query.
Also useful when you have more then 1:M relationship in a query
Now you can get a result set like:
deptno Sal Comm
Sal Comm
Sal
3 Sal, 2 Comm items and one deptno -- in one "row"
Thats why i used cursor Expression.
Thanks
Neeraj Goel

Similar Messages

  • Cursor Vs Select statement

    Hi,
    Please let me know what is the difference between using OPEN CURSOR and SELECT statement WITH PACAKGE additon.
    Praneeth

    Hi,
    http://wiki.sdn.sap.com/wiki/display/ABAP/SELECTStatementsandCURSORstatement-Performance+Analysis.
    and also check
    http://help.sap.com/saphelp_46c/helpdata/en/fc/eb3b23358411d1829f0000e829fbfe/content.htm
    cheers,
    Bhavana

  • Using Cursor in Select statements? How to do this?

    I am getting an error whilt passing a cursor to a select clause:
    SELECT dbms_xmlquery.getXML('select deptno, dname, '||
    'cursor(select empno, ename, sal from emp e where e.deptno = d.deptno) employees '||
    'from dept d where d.deptno in (10, 20)')
    FROM dual;
    DBMS_XMLQUERY.GETXML('SELECTDEPTNO'||'CURSORIS(SELECTEMPNOFROMEMPEWHEREE.DEPT=D.
    <?xml version = '1.0'?>
    <ERROR>oracle.xml.sql.OracleXMLSQLException: ORA-00923
    : FROM keyword not found where expected
    </ERROR
    THIS IS DUE TO THE CURSOR AND ITS FROM STATEMENT?
    CAN ANY ONE PLEASE GUIDE AS TO HOW TO USE CURSORS IN A SELECT STATEMENT PLEASE?

    Another duplicate thread. See my response Select CLAUSE error using CURSORS & XSU.Please SEE..
    Cheers, APC

  • Cursor - dynamic select statement?

    Hello!
    I am passing a string into a function, and my string is a sql select statement. Is it possible to create a cursor using the string that is passed to the function?
    Ex:
    create or replace function "GETCOUNT"
    (sqlstring in VARCHAR2)
    return NUMBER
    is
    rec_count NUMBER; --I eventually want to return a count of the rows returned from my sqlstring
    cursor c1 is sqlstring <--this is what I'd like to do...but how?
    begin
    ... -- I haven't gotten to this point yet
    end;
    The error I get says 'Encountered the symbol "sqlstring" when expecting on of the following: ( select '
    Thanks in advance!!

    Something like this ->
    create or replace procedure gen_cur(str in varchar2,rc in out sys_refcursor)
    is
    str1 varchar2(500);
    begin
    str1 := 'select empno,ename,hiredate,sal
    from emp
    where '||str;
    open rc for str1;
    exception
    when others then
    dbms_output.put_line(sqlerrm);
    end;
    variable b refcursor
    declare
    src varchar2(300);
    begin
    src:= 'sal between 2000 and 7000';
    gen_cur(src,:b);
    exception
    when others then
    dbms_output.put_line(sqlerrm);
    end;
    print bRegards.
    Satyaki De.

  • Ref cursor in select statement

    I am not sure this is the right forum, but I got no reply from the OTN Sample.
    How do I call a function from a select
    statement ?
    The function returns a ref cursor.
    Here is the function definition:
    FUNCTION get_priority_code_list
    (p_language_id IN VARCHAR2)
    RETURN RefCur
    IS
    rcur RefCur;
    BEGIN
    OPEN rcur FOR SELECT
    priority_code "priority_code_menu",
    priority_code_desc "priority_code_menu_desc"
    FROM
    prio_desc
    WHERE
    language_id = p_language_id;
    RETURN rcur;
    END;
    I would like to wrap a SELECT statement
    around the function and have the same result
    as running the SELECT statement directly.
    Of course it does not seem logical here to
    use a function, but in fact I want the
    function to do more than just a select....
    I tried the syntax
    SELECT package.function(param) FROM dual;
    but that does not work...
    or
    SELECT * FROM package.function(param);
    but that does not work either....
    any idea ?
    Tks.
    null

    I have a stored function that returns a ref cursor and I want to use it in select
    I tried the following code
    for example if the ref cursor returned rows of varchar2
    I will define an object
    CREATE TYPE myType as OBJECT( doc_type VARCHAR2(256));
    created a type as a table of myType
    create or replace type myTable as table of myType;
    If I write a select statement like below
    select * from TABLE(CAST(function_name() AS myTable))
    ERROR at line 1:
    ORA-00932: inconsistent datatypes: expected - got CURSER
    I get this error
    Can anyone give any clue why this does throw an error or if there is a second way to do this,

  • CURSOR Inside select statement

    Hi,
    I am trying to generate reports in XML Publisher using PL/SQL Procedure. Following is my procedure:
    PROCEDURE XXDL_PO_VEN_SUMMARY_BY_VEN (ERR_BUFF VARCHAR2,
                        RETCODE VARCHAR2,
                   in_vendor_number IN VARCHAR2,
                        in_start_date IN VARCHAR2,
                   in_end_date IN VARCHAR2
    IS
              SQL_STMT VARCHAR2(15000);
              result CLOB;
              in_vendor_summary_from_date DATE;
              in_vendor_summary_to_date DATE;
              CURSOR_WHERE_CLAUSE VARCHAR2(1000);
    BEGIN
              in_vendor_summary_from_date := TO_DATE(in_start_date,'YYYY/MM/DD HH24:MI:SS');
              in_vendor_summary_to_date := TO_DATE(in_end_date,'YYYY/MM/DD HH24:MI:SS');
              --DBMS_OUTPUT.PUT_LINe('start date object'||in_vendor_summary_from_date);
              --DBMS_OUTPUT.PUT_LINe('end date object'||in_vendor_summary_to_date);
    IF in_start_date IS NOT NULL AND LENGTH(in_start_date) > 0 AND in_end_date IS NOT NULL AND LENGTH(in_end_date) > 0 THEN
         CURSOR_WHERE_CLAUSE := 'AND TO_DATE(CONCAT(CONCAT(SUMMARY_MONTH, ''/''), SUMMARY_YEAR),''MM/YYYY'') BETWEEN'
         ||' '''||in_vendor_summary_from_date||''' AND '''||in_vendor_summary_to_date||'''';
    END IF;
    IF in_vendor_number IS NOT NULL AND LENGTH(in_vendor_number)>0 THEN
              CURSOR_WHERE_CLAUSE := CURSOR_WHERE_CLAUSE || ' AND UPPER(VENDOR_NUM) LIKE UPPER('''||in_vendor_number||'%'')';
    END IF;
    --DBMS_OUTPUT.PUT_LINe('cursor where clause : '||CURSOR_WHERE_CLAUSE );
    SQL_STMT := 'SELECT VS.VENDOR_NUM,' ||
              ' VS.VENDOR_NAME,' ||
              ' VS.SUMMARY_MONTH,' ||
              ' VS.SUMMARY_YEAR,' ||
              ' VS.ORD_TOTAL_CNT,' ||
              ' VS.ORD_DOLLAR_AMT,'||
              ' VS.RPO_TOTAL_CNT,' ||
              ' VS.RPO_DOLLAR_AMT,'||
              ' VS.CWA_TOTAL_CNT,' ||
              ' VS.CWA_DOLLAR_AMT,'||
              ' VS.STD_TOTAL_CNT,' ||
              ' VS.STD_DOLLAR_AMT,'||
              ' VS.BLM_TOTAL_CNT,' ||
              ' VS.BLM_DOLLAR_AMT,'||
              ' VS.INVOICE_TOTAL_CNT,' ||
              ' VS.INVOICE_DOLLAR_AMT, ' ||
              ' CURSOR (SELECT nvl(SUM( nvl(VS1.ORD_TOTAL_CNT,0)),0) FROM STANPRS.VENDOR_SUMMARY VS1 '||
              ' ) TOTAL_ORDERS, ' ||
              ' CURSOR(SELECT nvl(SUM(nvl(VS2.ORD_DOLLAR_AMT,0)),0) FROM STANPRS.VENDOR_SUMMARY VS2 '||
              ' ) TOTAL_DOLLARS,' ||
              ' CURSOR(SELECT nvl(SUM(nvl(VS3.RPO_TOTAL_CNT,0)),0) FROM STANPRS.VENDOR_SUMMARY VS3 '||
              ' ) RPO_ORDERS,' ||
              ' CURSOR(SELECT nvl(SUM(nvl(VS4.RPO_DOLLAR_AMT,0)),0) FROM STANPRS.VENDOR_SUMMARY VS4 '||
              ' ) RPO_DOLLARS,' ||
              ' CURSOR(SELECT nvl(SUM(nvl(VS5.CWA_TOTAL_CNT,0)),0) FROM STANPRS.VENDOR_SUMMARY VS5 '||
              ' ) CWA_ORDERS,' ||
              ' CURSOR(SELECT nvl(SUM(nvl(VS6.CWA_DOLLAR_AMT,0)),0) FROM STANPRS.VENDOR_SUMMARY VS6 '||
              ' ) CWA_DOLLARS,' ||
              ' CURSOR(SELECT nvl(SUM(nvl(VS7.STD_TOTAL_CNT,0)),0) FROM STANPRS.VENDOR_SUMMARY VS7 '||
              ' ) STD_ORDERS,' ||
              ' CURSOR(SELECT nvl(SUM(nvl(VS8.STD_DOLLAR_AMT,0)),0) FROM STANPRS.VENDOR_SUMMARY VS8 '||
              ' ) STD_DOLLARS,' ||
              ' CURSOR(SELECT nvl(SUM(nvl(VS9.BLM_TOTAL_CNT,0)),0) FROM STANPRS.VENDOR_SUMMARY VS9 '||
              ' ) BLM_ORDERS,' ||
              ' CURSOR(SELECT nvl(SUM(nvl(VS10.BLM_DOLLAR_AMT,0)),0) FROM STANPRS.VENDOR_SUMMARY VS10 '||
              ' ) BLM_DOLLARS,' ||
              ' CURSOR(SELECT nvl(SUM(nvl(VS11.INVOICE_TOTAL_CNT,0)),0) FROM STANPRS.VENDOR_SUMMARY VS11 '||
              ' ) TOTAL_INVOICES,' ||
              ' CURSOR(SELECT nvl(SUM(nvl(VS12.INVOICE_DOLLAR_AMT,0)),0) FROM STANPRS.VENDOR_SUMMARY VS12 '||
              ' ) INVOICE_DOLLARS, ' ||
    ' CURSOR(SELECT nvl(SUM(nvl(V1.RPO_TOTAL_CNT,0)),0)*100 / decode(SUM(nvl(V1.RPO_TOTAL_CNT,0)),0,1,NULL,1,SUM(nvl(V1.RPO_TOTAL_CNT,0))) FROM '|| ' STANPRS.VENDOR_SUMMARY V1 ) RPO_ORDERS_PER,' ||
    ' CURSOR(SELECT NVL(SUM(nvl(V2.RPO_DOLLAR_AMT,0)),0)*100 / decode(SUM(nvl(V2.RPO_DOLLAR_AMT,0)),0,1,NULL,1,SUM(nvl(V2.RPO_DOLLAR_AMT,0))) '|| 'FROM STANPRS.VENDOR_SUMMARY V2 ) RPO_DOLLARS_PER,' ||
    ' CURSOR(SELECT NVL(SUM(nvl(V3.CWA_TOTAL_CNT,0)),0)*100 / decode(SUM(nvl(V3.CWA_TOTAL_CNT,0)),0,1,NULL,1,SUM(nvl(V3.CWA_TOTAL_CNT,0))) FROM '|| 'STANPRS.VENDOR_SUMMARY V3 ) CWA_ORDERS_PER,' ||
    ' CURSOR(SELECT NVL(SUM(nvl(V4.CWA_DOLLAR_AMT,0)),0)*100 / decode(SUM(nvl(V4.CWA_DOLLAR_AMT,0)),0,1,NULL,1,SUM(nvl(V4.CWA_DOLLAR_AMT,0))) '||
    'FROM STANPRS.VENDOR_SUMMARY V4 ) CWA_DOLLARS_PER,' ||
    ' CURSOR(SELECT NVL(SUM(nvl(V5.STD_TOTAL_CNT,0)),0)*100 / decode(SUM(nvl(V5.STD_TOTAL_CNT,0)),0,1,NULL,1,SUM(nvl(V5.STD_TOTAL_CNT,0))) FROM '|| 'STANPRS.VENDOR_SUMMARY V5 ) STD_ORDERS_PER,' ||
    ' CURSOR(SELECT NVL(SUM(nvl(V6.STD_DOLLAR_AMT,0)),0)*100 / decode(SUM(nvl(V6.STD_DOLLAR_AMT,0)),0,1,NULL,1,SUM(nvl(V6.STD_DOLLAR_AMT,0))) '||
    'FROM STANPRS.VENDOR_SUMMARY V6 ) STD_DOLLARS_PER,' ||
    ' CURSOR(SELECT NVL(SUM(nvl(V7.BLM_TOTAL_CNT,0)),0)*100 / decode(SUM(nvl(V7.BLM_TOTAL_CNT,0)),0,1,NULL,1,SUM(nvl(V7.BLM_TOTAL_CNT,0))) '||
    'FROM STANPRS.VENDOR_SUMMARY V7 ) BLM_ORDERS_PER,' ||
    ' CURSOR(SELECT NVL(SUM(nvl(V8.BLM_DOLLAR_AMT,0)),0)*100 / decode(SUM(nvl(V8.BLM_DOLLAR_AMT,0)),0,1,NULL,1,SUM(nvl(V8.BLM_DOLLAR_AMT,0))) '||
    'FROM STANPRS.VENDOR_SUMMARY V8 ) BLM_DOLLARS_PER,' ||
    'CURSOR(SELECT NVL(SUM(nvl(V9.INVOICE_TOTAL_CNT,0)),0)*100 / '||
    'decode(SUM(nvl(V9.INVOICE_TOTAL_CNT,0)),0,1,NULL,1,SUM(nvl(V9.INVOICE_TOTAL_CNT,0))) FROM '||
    'STANPRS.VENDOR_SUMMARY V9 ) TOTAL_INVOICES_PER, ' ||
    'CURSOR(SELECT NVL(SUM(nvl(V10.INVOICE_DOLLAR_AMT,0)),0)*100/ decode(SUM(nvl(V10.INVOICE_DOLLAR_AMT,0)),0,1,NULL,1,SUM(nvl(V10.INVOICE_DOLLAR_AMT,0))) FROM STANPRS.VENDOR_SUMMARY V10 ) INVOICE_DOLLARS_PER' ||
    ' FROM STANPRS.VENDOR_SUMMARY VS' ||
    ' WHERE';
    --DBMS_OUTPUT.PUT_LINE('STANPRS.VENDOR_SUMMARY V10  WHERE V10.VENDOR_NUM = VS.VENDOR_NUM ') INVOICE_DOLLARS_PER');
    IF in_start_date IS NOT NULL AND LENGTH(in_start_date) > 0 AND in_end_date IS NOT NULL AND LENGTH(in_end_date) > 0 THEN
         SQL_STMT := SQL_STMT || ' TO_DATE(CONCAT(CONCAT(VS.SUMMARY_MONTH, ''/''), VS.SUMMARY_YEAR),''MM/YYYY'') BETWEEN '
         ||' '''||in_vendor_summary_from_date||''' AND '''||in_vendor_summary_to_date||'''';
    END IF;
    IF in_vendor_number IS NOT NULL AND LENGTH(in_vendor_number)>0 THEN
         IF SUBSTR(SQL_STMT, LENGTH(SQL_STMT)-4) = 'WHERE' THEN
              SQL_STMT := SQL_STMT || ' UPPER(VS.VENDOR_NUM) LIKE UPPER('''||in_vendor_number||'%'')';
         ELSE
              SQL_STMT := SQL_STMT || ' AND UPPER(VS.VENDOR_NUM) LIKE UPPER('''||in_vendor_number||'%'')';
         END IF;
    END IF;
    IF in_vendor_name IS NOT NULL AND LENGTH(in_vendor_name)>0 THEN
         IF SUBSTR(SQL_STMT, LENGTH(SQL_STMT)-4) = 'WHERE' THEN
              SQL_STMT := SQL_STMT || ' UPPER(VS.VENDOR_NAME) LIKE UPPER('''||in_vendor_name||'%'')';
         ELSE
              SQL_STMT := SQL_STMT || ' AND UPPER(VS.VENDOR_NAME) LIKE UPPER('''||in_vendor_name||'%'')';
         END IF;
    END IF;
    IF SUBSTR(SQL_STMT, LENGTH(SQL_STMT)-4) = 'WHERE' THEN
         SQL_STMT := SQL_STMT || ' VS.VENDOR_NUM = '''||in_vendor_number||'''';
    END IF; */
    SQL_STMT := SQL_STMT || ' ORDER BY VS.VENDOR_NUM, VS.VENDOR_NAME ASC';
    --insert into XX_ERROR VALUES('Spriha',SQL_STMT);
    SELECT DBMS_XMLGEN.GETXML(SQL_STMT) INTO result FROM DUAL;
    FND_FILE.PUT_LINE(FND_FILE.OUTPUT, result);
    EXCEPTION
         WHEN OTHERS THEN
              FND_FILE.PUT_LINE(FND_FILE.OUTPUT, SQLERRM);
    END XXDL_PO_VEN_SUMMARY_BY_VEN ;
    Now u can see in this Procedure that i have created so many cursors. i.e. for one function i have created one cursor. at present thesecursors are working fine. but the time i will put the where clause inside the cursors this will throw an ERROR ' ORA-01460 Unimplemented or Unreasonable Conversion Request'. Also these cursors are working with where clause but they are not working together. i.e when i will remove some cursors then i am not getting any error but at present i.e. without any where clause they all r working fine. Seriously i am not getting anything that what is happening. Can anyone help me? waiting for the reply....
    Thanks
    Spriha

    This is pretty much a poster for ugly code.
    Simplify this down to the bare minimum and debug it. Then add back the rest of the functionality.
    But I am at a loss, looking at this code, as to why any cursor is either desirable or required.

  • Execute the expression in select statement

    CREATE TABLE TEST1
      OFFICE_PRODUCTS     NUMBER,
      OFFICE_ELECTRONICS  NUMBER
    Insert into TEST1 (OFFICE_PRODUCTS, OFFICE_ELECTRONICS) Values(1, 0);
    COMMIT;
    CREATE TABLE TEST2
      EXPORT_FIELD_NAME         VARCHAR2(100 BYTE),
      EXPORT_COLUMN_EXPRESSION  VARCHAR2(100 BYTE)
    Insert into TEST2
       (EXPORT_FIELD_NAME, EXPORT_COLUMN_EXPRESSION)
    Values ('A1', 'least(OFFICE_PRODUCTS, OFFICE_ELECTRONICS)');
    COMMIT; I want to be execute the expression should run in select statement how to do?
    and tried as like below,it's not working.
    select (select EXPORT_COLUMN_EXPRESSION from test2 where EXPORT_FIELD_NAME='A1') FROM TEST1;

    968892 wrote:
    CREATE TABLE TEST1
    OFFICE_PRODUCTS     NUMBER,
    OFFICE_ELECTRONICS  NUMBER
    Insert into TEST1 (OFFICE_PRODUCTS, OFFICE_ELECTRONICS) Values(1, 0);
    COMMIT;
    CREATE TABLE TEST2
    EXPORT_FIELD_NAME         VARCHAR2(100 BYTE),
    EXPORT_COLUMN_EXPRESSION  VARCHAR2(100 BYTE)
    Insert into TEST2
    (EXPORT_FIELD_NAME, EXPORT_COLUMN_EXPRESSION)
    Values ('A1', 'least(OFFICE_PRODUCTS, OFFICE_ELECTRONICS)');
    COMMIT; I want to be execute the expression should run in select statement how to do?
    and tried as like below,it's not working.
    select (select EXPORT_COLUMN_EXPRESSION from test2 where EXPORT_FIELD_NAME='A1') FROM TEST1;
    Your problems are many...
    a) it's very poor design to be storing expressions or sql statements or any 'executable' style code as data in the database.
    b) what you're storing is a string of characters. Oracle isn't going to miraculously know that that is some expression that has to be evaluated, so why should it decide to treat it as such?
    c) this poor design can lead to security issues especially around SQL injection.
    d) to actually perform what you want would require you to build a dynamic SQL statement and then execute that using EXECUTE IMMEDIATE or DBMS_SQL (or for a 3rd party client, a Ref Cursor), but then there are numerous issues around doing dynamic SQL, aside from SQL injection, in that you are producing code that is not validated at compile time and can thus lead to bugs showing only at run-time and sometime only under certain conditions; the code is harder to maintain; the code can potentially be avoiding the use of bind variables, impacting on resources and performance on the database; the final query can be difficult to know just from reading the code, making further development or debugging a pain in the posterior. Essentially, dynamic SQL is considered very poor design and is 99.9% of the time used for the wrong reasons.
    So, why are you trying to do this? What is the business requirement you are trying to solve?

  • Select statement from "v_filename_txt" inside cursor

    Hi,
    I've got a store procedure procedure that create parametric tables to upload from csv files. I have collect into v_filename_txt variable the parametric table name where I would like to make some select.
    I have write the following code but I can't use a variable into a select statement :
    select field
    FROM TABLE '''||v_filename_txt||''' ;
    Does anybody know how to include the precedent statement into a cursor to retrive recods ?
    Paolo.

    You could rcreate an external table. Then select the values from this external table.
    You can switch the source of the external table to a new file with the
    alter table myExtTable location ('mySecondCsvFIle.csv');
    command.
    Of cause this works only when all the files have the same structure. But the Idea is: mimic the structure of the csv file sin the database with a external table command. Then you can read the values and move them to some other table.
    Insert into myParameterTable select * from myExtTable;

  • Number of records in cursor select statement

    hi all,
    with the cursor i am selecting set of select statement and writing into the .txt file.
    its working file, but it should not open the file if the cursor returns nothing.
    how to check for the number of records return by the select statement in the cursor.
    pls help me.
    Thanks..

    Hi,
    You can use a Cursor for loop (you'll not enter in the loop if the SELECT is retrieving no rows):
    begin
    for recs in(select * from whatever) loop
    null; -- Produce your file
    end loop;
    end;You can also use a bulk collect and check "yourcollection.count" attribute ... (but don't use it if you are processing a lot of records!)

  • Using ref cursor in "in clause" in select statement

    Hi,
    Is there any way can we use the ref cursor in the in condition of a select statement.
    Regards,
    Venkat.
    Edited by: ramanamadhav on Aug 23, 2011 11:14 AM

    ramanamadhav wrote:
    I'm sorry if I post in confusing way. I will give an example. Just see the psudo code here.
    declare
    rf_cur sys_refcursor;
    begin
    pr_test(empno,rf_cur);
    -- rf_cur returning emp names.
    select * from emp
    where empname in (ref_cusor results);-- here i want to consume my ref cursor result in the in conditions.
    end;
    Thanks &Regards,
    Venkat.No you can't do that. A ref cursor is not a set of results as you believe.
    Take a read of this article...
    {thread:id=886365}

  • Use of cursors insted of select statements

    could any one please explain what is the advantage of using cursors instead of simple select statements
    thanks
    siby

    A benefit to using an explicit cursor rather than a select statement, is for the NO_DATA_FOUND exception. Its kind of like a free IF statment. IF no data is found, then stop.
    if you write a select statement, and no data is returned, you SHOULD code for the NO_DATA_FOUND exception. Often people say, "i'll ALWAYS get a row returned". but you should always cover your code "just in case". so you must code an exception...
    declare
    v_var varchar2(1);
    procedure do_something(p_parm varchar2) is
    begin
    null;
    end do_something;
    procedure log_error is
    begin
    null;
    end log_error;
    begin <<main>>
    do_something('x');
    begin <<selectblock>>
    select dummy
    into v_var
    from dual
    where dummy = 'a';
    do_something(v_var);
    exception
    when no_data_found then
    log_error;
    end selectblock;
    do_something (v_var||'abc');
    end main;
    if you use an explicit cursor instead, you don't need to code for the NO_DATA_FOUND. If an explicit cursor opens and finds no rows, there are simply no rows. of course, you don't need a loop if you expect only 1 row returned under normal circumstances.
    BTW, don' forget that SQL%ROWCOUNT and your_cursor%ROWCOUNT are not initialized. There is a null, until a row is successfully fetched. therefore if no rows are returned at all, %ROWCOUNT is NULL.
    declare
    v_var varchar2(1);
    cursor my_cur is
    select dummy
    from dual
    where dummy = 'a';
    procedure do_something(p_parm varchar2) is
    begin
    null;
    end do_something;
    procedure log_error is
    begin
    null;
    end log_error;
    begin << main>>
    for cur_rec in my_cur loop
    dbms_output.put_line('inside');
    begin <<loop_block>>
    if nvl(my_cur%rowcount,0) > 1 then
    do_something(cur_rec.dummy);
    else
    log_error;
    end if;
    end loop_block;
    end loop;
    end main;
    /

  • Insert select statement or insert in cursor

    hi all,
    i need a performance compare for: insert operation in cursor one by one and select statement insert,
    for example:
    1. insert into TableA (ColumA,ColumnB) select A, B from TableB;
    2. cursor my_cur is select A, B from TableB
    for my_rec in my_cur loop
    insert into TableA (ColumA,ColumnB) values (my_rec.A, my_rec.B);
    end loop;
    also "bulk collect into" can be used.
    Which one has a better performance?
    Thanks for your help,
    kadriye

    What's stopping you from making 100,000 rows of test data and trying it for yourself?
    Edit: I was bored enough to do it myself.
    Starting insert as select 22-JUL-08 11.43.19.544000000 +01:00
    finished insert as select. 22-JUL-08 11.43.19.825000000 +01:00
    starting cursor loop 22-JUL-08 11.43.21.497000000 +01:00
    finished cursor loop 22-JUL-08 11.43.35.185000000 +01:00
    The two second gap between the two is for the delete.
    Message was edited by:
    Dave Hemming

  • Function returning cursor to be used in select statement

    I have written a function which returns a cursor. I want to use this in a select statement to display rows. How to do this.
    Eg : function - test_fn return sys_refcursor. (returns empno, empname, deptname)
    select test_fn from dual
    This should return
    empno empname deptno
    1 name1 dept1
    2 name 2 dept2
    ....

    Try This::
    SCOTT@xe_144>variable c  refcursor
    SCOTT@xe_144>CREATE OR REPLACE FUNCTION get_nm
      2     RETURN sys_refcursor
      3  AS
      4    
      5     c sys_refcursor;
      6  BEGIN
      7    
      8     open c for 'SELECT ENAME , SAL FROM EMP';
      9      
    10     RETURN c;
    11  END;
    12  /
    Function created.
    SCOTT@xe_144>select get_nm into :c from dual;
    GET_NM
    CURSOR STATEMENT : 1
    CURSOR STATEMENT : 1
    ENAME             SAL
    SMITH             800
    ALLEN            1600
    WARD             1250
    JONES            2975
    BLAKE            2850
    CLARK            2450
    KING             3000
    AKASH2           5000
    TURNER           1500
    ADAMS            1100
    JAMES             950
    MILLER           1300
    ABC              2000
    13 rows selected.Let me know if its works for you.

  • PL/SQL cursors vs. SQL*plus Select statement

    Hi folks, hope you're doing well,
    Here is a question that kept me wondering:
    Why would I use cursors when i can achieve the same thing with a SQL+ Select statement which is much easier to formulate than a cursor (e.g. you need no declaration, loops etc)?.
    Thanks so much,
    -a

    There is no such thing as a SQL*Plus SELECT statement. The SELECT command is part of the SQL Language - not part of the SQL*Plus (very limited small vocabulary) macro language.
    All SQL SELECTs (from client languages) winds up in the SQL Engine as SQL cursors. A SQL Cursor is basically the:
    - SQL source code
    - SQL "compiled" code (instructions on how to fetch the rows)
    On the client side, client cursors (not to be confused with SQL cursors) are used. A client cursor is created in the client language when it makes SQL calls via the database client driver (called the OCI/Oracle Call Interface for Oracle clients).
    Typically this is what a client does. It makes a connection to the database and gets a database handler in return. The database handler is the "communication channel" from the client to the db. In Oracle, the database handler in the client refers to the Oracle session (for that client) on db server.
    A SQL statement (source code) is used by the client. This can be a SELECT statement you type in at the SQL*Plus command line. It can be a SELECT statement for the PL/SQL cursor command in a stored procedure.
    The client creates a SQL handle for this SQL statement, by calling the Oracle client driver. Note that this SQL handle is a client handle - a client cursor for that SQL statement.
    E.g.
    a) sqlHandle = CreateSQL( databaseHandle, 'SELECT ... FROM ...')
    b) sqlHandle.Parse
    c) sqlHandle.Execute
    After the SQL handle (client cursor) has been executed, the client can fetch rows from it.
    This is what SQL*Plus does automatically for you, without you having to write the code to do it. SQL*Plus CONNECT command create a database connection handle. You enter a SELECT statement and SQL*Plus creates a SQL handle (client cursor), executes it, fetches from it, displays the rows, and closes the SQL handle when done.
    The same applies to PL/SQL. You can use a SELECT statement just like that in PL/SQL. E.g.
    declare
      i integer;
    begin
      select count(*) into i from emp where deptid = 123;
    end;This is called an implicit cursor. PL/SQL creates (just like SQL*Plus) an implicit client cursor. It creates and disposes of that client SQL handle for you - you do not need to do it.
    Or, you can create an explicit cursor. E.g. declare
      cursor c is select count(*) from emp where deptid = 123;
      i integer;
    begin
      open c;
      fetch c into i;
      close c;
    end;The question as to when to use implicit (client) cursors versus explicit (client) cursors depends on your requirements. Do you need to cycle through the results of the SQL? Etc.
    And keep in mind that in either case, the SQL Engine creates a SQL cursor anyway on its side.

  • Re: Select statement fails under Express, but works underForte when usi

    Hi there,
    I have logged this with Forte and it as been recognized as a bug ( #46554 ).
    The Express methods you need to look at if you want to modify this behavior
    are :
    1 - BusinessQuery.BuildQuery() starting at line 217
    2 - SqlQuery.GetWhereText() starting at line 60
    We have gotten around the problem by modifying what Express generates by
    changing it from ( col1, col2 ) in ( select col1, col2 ... ) to ( col1 +
    col2 ) in ( select col1 + col2 ... )
    Just one thing i'm not sure that the '+' is standard on every DBMS for
    concatenation.
    We are using SQL Server 6.5
    Hope this helps.
    Christian Boult ([email protected])
    Programmeur - Analyste
    Influatec inc.
    -----Original Message-----
    From: Metcalf, Roger <[email protected]>
    To: '[email protected]' <[email protected]>
    Date: Thursday, May 06, 1999 1:55 PM
    Subject: Select statement fails under Express, but works under Forte when
    using DB2
    Express sometimes (e.g. with nested windows) generates SQL with a select
    subquery, e.g.
    select x1, x2 from t1 where (x1,x2) in (select x1,x2 from t2.....)
    This fails on DB2 with a message that the comma in the where clause (x1,x2)
    is not allowed.
    This works on Oracle.
    Does anyone have a workaround or other suggestion?
    To unsubscribe, email '[email protected]' with
    'unsubscribe forte-users' as the body of the message.
    Searchable thread archive <URL:http://pinehurst.sageit.com/listarchive/>
    To unsubscribe, email '[email protected]' with
    'unsubscribe forte-users' as the body of the message.
    Searchable thread archive <URL:http://pinehurst.sageit.com/listarchive/>

    joe.meszaros wrote:
    (SELECT ? AS Id, ? AS UserBytes FROM dual)I suggest that you test to see if that statement and ONLY that statement works at all.
    There are rules for bind variables and that statement certainly looks suspicious.
    If you can't get that to work then the rest of the attempt is pointless.

Maybe you are looking for

  • Sound problem thru minidisplayport to HDMI on AVR

    Hi! When i connect my iMac '27 (mid 2010) to my AVReciever (Harman Kardon AVR 255) i hear the sound, but on every few seconds the sound dissapear for a 2 seconds and comes back. Its really frustrating...The thing is, that on Mountain Lion was everyth

  • Which webpage size is the "most normal" to use?

    Hey everyone out there. I'm creating a webpage in Muse. In the Plan-part of the program the page is shown just as I want it, but when I upload it on my browser it looks all wrong. It's like my page is too small for the browser, because big white gaps

  • Print command makes the programm to close

    I have recently bought a black macbook and when I use the print command (either from the menu or by pressing command and P) the programm (pages, preview, stickies, text, ms word) dissapears and a message that the programm quited unexpectedly appears.

  • Master Password won't change

    I recently upgraded to OSX 10.4.11. When I try to set my master password to a new one, it will not recognize the change, it remains my old password. What can I do to remedy this?

  • EJB3 Dependency Injection

    hi, I am using ejb3 with adf managed bean. when i inject ejb3 from managed bean , i get only null in the ejb object. i am using Jdeveloper 11.111. my ejb project and my web project are in separate application. i add ejb jar into my web project from l