Is it possible to identify records in ref cursor without actually fetching

CREATE OR REPLACE PROCEDURE test_miles (p_ref_cursor OUT SYS_REFCURSOR)
IS
BEGIN
OPEN p_ref_cursor FOR
select 5168 mem_uid, 16353 bac_uid, '2013-JAN-19' dte,3 no_of_pax,'AnoopM' username,NULL reward_id from dual
union select 4702 mem_uid , 16344 bac_uid, '2013-JAN-29' dte, 2 no_of_pax,'RAZO' username, NULL reward_id from dual;
END;
Hi all,
I have having a Procedure with out parameter as a REF CURSOR.
This ref cursor will be returned to the calling service.
Is there a way in oracle by which we can identify whether the Ref cursor holds data without actually fetching it.
Since if i choose to fetch the data, i will lose one row when i return the ref cursor back to the calling service. Or else is there way i can retrieve the row i lose during fetch.
Other alternative what have been suggested is create and object type ,fetch the ref cursor values in object type. Then i can use the ref cursor to return the data by table casting.
one more solution is
OPEN
FETCH
CLOSE
OPEN (AGAIN)
In reality the select statement will have is huge lines of code therefore want a suggestion whether there is an alternative to the above solution
Please suggest.

CREATE OR REPLACE PROCEDURE test_miles (p_ref_cursor OUT SYS_REFCURSOR)
IS
BEGIN
OPEN p_ref_cursor for SELECT * from DUAL;
OPEN p_ref_cursor FOR
select 5168 mem_uid, 16353 bac_uid, '2013-JAN-19' dte,3 no_of_pax,'AnoopM' username,NULL reward_id from dual
union select 4702 mem_uid , 16344 bac_uid, '2013-JAN-29' dte, 2 no_of_pax,'RAZO' username, NULL reward_id from dual;
END;
If the second cursor doesnt fetch any data then an empty dataset will be stored

Similar Messages

  • Number of total rows returned by a ref cursor without using FETCH

    Hi. How can we get the total number of rows returned by a ref cursor without doing the FETCH? I mean, if you use %ROWCOUNT, it only returns the current row number being returned in every fetch. This is not what I want. My purpose is to determine if my query using ref cursor returns greater than zero total rows without using fetch. Thanks.

    As John pointed out in the thread you linked to, the only way to know how many rows a query will return is to actually fetch all the rows. Oracle doesn't know how many rows a query is going to return until it actually fetches the last row.
    Plus, assuming the default transaction isolation level, if you run the same query multiple times in succession, there is no guarantee that the results will be the same.
    If you just want to know whether a query will return a nonzero number of rows, why not just write the code to assume that it returns at least 1 row and handle the zero row result as an exception.
    Justin

  • Ref Cursor - How to append records into ref cursor?

    Hi,
    Is it possible to append ref cursor?
    Iam having a procedure which accepts 1 string as input
    parameter. That string will have list of ID delimited by comma.
    I want to extract & match every ID with some tables.
    My problem is for first ID i would get 10 records
    and for 2nd ID i 'l get other 20 records. But while returning
    i need to send the same(10 + 20 records) as ref cursor(OUT parameter).
    But in below given code i could send only last 20 records. first
    10 records are not append/updated into ref cursor.
    How to append 2nd 20 records with 1st 10 records? so that i can
    send all the 30 records.
    Here goes my code...
    CREATE OR REPLACE PROCEDURE getCRMGroupsAndRollups_PRC
    in_groupId IN VARCHAR2,
    out_getCRMGroups OUT TYPES.DATASET
    IS
    v_temp VARCHAR2(500) := in_groupId ||',';
    v_temp_split VARCHAR2(500);
    v_pos1 NUMBER := 0;
    v_pos2 NUMBER := 1;
    v_pos3 NUMBER := 0;
    v_extract_char VARCHAR(1) := NULL;
    v_comma_cnt NUMBER := 0;
    BEGIN
    -- check in for null input parameters
    IF ( in_groupId IS NOT NULL ) THEN
    -- loop to count no of in_groupId
    FOR j IN 1..LENGTH(v_temp)
    LOOP
         v_extract_char := SUBSTR(v_temp,j,1);
         IF (v_extract_char = ',') THEN
              v_comma_cnt := v_comma_cnt + 1;
         END IF;     
    END LOOP;
    -- loop to extract in_group Id
    FOR i IN 1..v_comma_cnt
    LOOP
         v_pos1 := instr(v_temp,',',(v_pos1 + 1));
         v_pos3 := ((v_pos1-1) - v_pos2 )+ 1;
         v_temp_split := SUBSTR(v_temp,v_pos2,v_pos3);
         v_pos2 := v_pos1 + 1;
    -- query to return dataset filled BY list of all the current
    -- CRM groups and the associated rollup groups
    OPEN out_getCRMGroups FOR
    SELECT
    DISTINCT
    gcs.crm_st_id_cd,
    gcs.lgcy_roll_up_grp_num,
    gcs.lgcy_roll_up_grp_name,
    gcs.grp_xwalk_complt_dt,
    gcs.crm_grp_num,
    gcs.facets_gnat_id,
    gcs.crm_grp_name
    FROM
    grp_convsn_stat gcs
    --lgcy_xref_elem lxe
    WHERE
    ( gcs.mbrshp_convsn_aprvl_dt = NULL )
    OR ( gcs.mbrshp_convsn_aprvl_dt < (SYSDATE - 7 ) )
    AND ( gcs.facets_grp_stat_actv_ind = 'Y' )
    AND ( gcs.lgcy_roll_up_grp_num = v_temp_split );
    END LOOP;
    END IF;
    EXCEPTION
    WHEN OTHERS THEN
    DBMS_OUTPUT.PUT_LINE('INTERNAL ERROR');
    END getCRMGroupsAndRollups_PRC;
    in this v_temp_split will have extracted id & iam opening
    ref cursor for each & every ID extracted from list.
    2) How to handle no_data_found exception for this ref cursor?
    Please help me....
    -thiyagarajan.

    http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:110612348061
    http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:210612357425
    Message was edited by:
    Kamal Kishore

  • Procedure to fetch table records using ref cursor

    Hi
    i need to fetch all the records in the table using ref cursor.we need to pass table
    name and the out paramater should be ref cursor.
    CREATE OR REPLACE PROCEDURE gettable(p_table_name IN VARCHAR2,
    p_ref_cursor OUT dept_pack.ref_cursor1)
    IS
    BEGIN
    OPEN p_ref_cursor FOR SELECT * FROM p_table_name;
    END gettable;
    is that a start ? then after this i have to execute this procedure to fetch the data from table. i am getting error that table doesnot exist but my idea was to pass p_table_name as IN parameter.
    Thnks in Advance

    here is the example
    SQL> CREATE OR REPLACE PROCEDURE TEST( t_name IN VARCHAR2
      2                                  , p_cursor OUT SYS_REFCURSOR)
      3  IS
      4  BEGIN
      5    OPEN p_cursor FOR
      6    'SELECT * FROM '|| t_name ;
      7  END TEST;
      8  /
    Procedure created.
    Elapsed: 00:00:00.02
    SQL>  var o refcursor;
    SQL> var tname varchar2(10);
    SQL> execute test('EMP',:o);
    PL/SQL procedure successfully completed.
    Elapsed: 00:00:00.00
    SQL> print :o;
         EMPNO ENAME      JOB              MGR HIREDATE         SAL       COMM        DNO
          7369 SMITH      CLERK           7902 17-DEC-80      800.2                    20
          7499 ALLEN      SALESMAN        7698 20-FEB-81       1600        300         30
          7521 WARD       SALESMAN        7698 22-FEB-81       1250        500         30
          7566 JONES      MANAGER         7839 02-APR-81       2975                    20
          7654 MARTIN     SALESMAN        7698 28-SEP-81       1250       1400         30
          7698 BLAKE      MANAGER         7839 01-MAY-81       2850                    30
          7782 CLARK      MANAGER         7839 09-JUN-81       2450                    10
          7788 SCOTT      ANALYST         7566 19-APR-87       3000                    20
          7839 KING       PRESIDENT            17-NOV-81       5000                    10
          7844 TURNER     SALESMAN        7698 08-SEP-81       1500          0         30
          7876 ADAMS      CLERK           7788 23-MAY-87       1100                    20
          7900 JAMES      CLERK           7698 03-DEC-81        950                    30
          7902 FORD       ANALYST         7566 03-DEC-81       3000                    20
          7934 MILLER     CLERK           7782 23-JAN-82       1300                    10
    14 rows selected.
    Elapsed: 00:00:00.01
    SQL>  execute test('DEPT',:o);
    PL/SQL procedure successfully completed.
    Elapsed: 00:00:00.02
    SQL>  print :o;
        DEPTNO DNAME          LOC
            90 LOGISTIC
            10 ACCOUNTING     NEW YORK
            20 RESEARCH       DALLAS
            30 SALES          CHICAGO
            40 OPERATIONS     BOSTON
    Elapsed: 00:00:00.01

  • Dynamic REF Cursor with Dynamic Fetch - Urgent

    i have a pl/sql package with generates dynamic SQL statments. my problem is i want to open this SQL statment dynamically and fetch data in dynamic variable.
    declare
    type type_temp is REF CURSOR;
    cur_temp type_temp;
    mv_sql varchar2(4000);
    begin
    -- this will be dunamically generated and
    -- hence could have any no. of columns.
    mv_sql := select f1, f2, f3, f4 from table_temp;
    open cur_temp for mv_sql;
    fetch cur_temp into c1, c2, c3, c4;
    close cur_temp;
    end;
    problem is my sql statment will have N no. of columns how can i fetch this N no. of columns.

    Very hard problem, because ref cursors do not (directly) support description!
    Se mine (non-ideal) solution (it may be doable, but it isn't very practical
    or easily maintainable):
    1. "Generic" package
    CREATE OR REPLACE PACKAGE dyn_fetch IS
    TYPE ref_cur_t IS REF CURSOR;
    g_query VARCHAR2 (32000);
    g_count NUMBER;
    g_desc_tab DBMS_SQL.DESC_TAB;
    varchar2_type CONSTANT PLS_INTEGER := 1;
    number_type CONSTANT PLS_INTEGER := 2;
    date_type CONSTANT PLS_INTEGER := 12;
    rowid_type CONSTANT PLS_INTEGER := 11;
    char_type CONSTANT PLS_INTEGER := 96;
    long_type CONSTANT PLS_INTEGER := 8;
    raw_type CONSTANT PLS_INTEGER := 23;
    mlslabel_type CONSTANT PLS_INTEGER := 106;
    clob_type CONSTANT PLS_INTEGER := 112;
    blob_type CONSTANT PLS_INTEGER := 113;
    bfile_type CONSTANT PLS_INTEGER := 114;
    PROCEDURE describe_columns;
    FUNCTION record_def RETURN VARCHAR2;
    END;
    CREATE OR REPLACE PACKAGE BODY dyn_fetch IS
    PROCEDURE describe_columns IS
    l_cur INTEGER;
    BEGIN
    l_cur := DBMS_SQL.OPEN_CURSOR;
    DBMS_SQL.PARSE (l_cur, g_query, DBMS_SQL.NATIVE);
    DBMS_SQL.DESCRIBE_COLUMNS (l_cur, g_count, g_desc_tab);
    DBMS_SQL.CLOSE_CURSOR (l_cur);
    EXCEPTION
    WHEN OTHERS THEN
    IF DBMS_SQL.IS_OPEN (l_cur) THEN
    DBMS_SQL.CLOSE_CURSOR (l_cur);
    END IF;
    RAISE;
    END;
    FUNCTION record_def RETURN VARCHAR2 IS
    l_record_def VARCHAR2 (32000);
    l_type VARCHAR2 (100);
    l_col_type PLS_INTEGER;
    l_col_max_len PLS_INTEGER;
    l_col_precision PLS_INTEGER;
    l_col_scale PLS_INTEGER;
    BEGIN
    FOR i IN 1..g_count LOOP
    l_col_type := g_desc_tab(i).col_type;
    l_col_max_len := g_desc_tab(i).col_max_len;
    l_col_precision := g_desc_tab(i).col_precision;
    l_col_scale := g_desc_tab(i).col_scale;
    IF l_col_type = varchar2_type THEN
    l_type := 'VARCHAR2(' || l_col_max_len || ')';
    ELSIF l_col_type = number_type THEN
    l_type := 'NUMBER(' || l_col_precision || ',' || l_col_scale || ')';
    ELSIF l_col_type = date_type THEN
    l_type := 'DATE';
    ELSIF l_col_type = rowid_type THEN
    l_type := 'ROWID';
    ELSIF l_col_type = char_type THEN
    l_type := 'CHAR(' || l_col_max_len || ')';
    -- ELSIF l_col_type = ...
    -- long_type, raw_type ...
    END IF;
    l_record_def := l_record_def || ' col_' || i || ' ' || l_type || ',';
    END LOOP;
    l_record_def := RTRIM (l_record_def, ',');
    RETURN l_record_def;
    END;
    END;
    Note that procedure "record_def" creates columns names as col_1 (col_2 ...)
    because SELECT clause in your query can be without aliases, for example
    "SELECT deptno || dname FROM dept".
    2. Your package which returns query nad ref cursor
    CREATE OR REPLACE PACKAGE test IS
    PROCEDURE set_query (p_query VARCHAR2 := NULL);
    FUNCTION ref_cur RETURN dyn_fetch.ref_cur_t;
    END;
    CREATE OR REPLACE PACKAGE BODY test IS
    PROCEDURE set_query (p_query VARCHAR2 := NULL) IS
    l_query VARCHAR2 (32000) :=
    ' SELECT e.empno, e.ename,' ||
    ' e.deptno, d.dname' ||
    ' FROM emp e,' ||
    ' dept d' ||
    ' WHERE e.deptno = d.deptno';
    BEGIN
    IF p_query IS NULL THEN
    dyn_fetch.g_query := l_query;
    ELSE
    dyn_fetch.g_query := p_query;
    END IF;
    END;
    FUNCTION ref_cur RETURN dyn_fetch.ref_cur_t IS
    l_ref_cur dyn_fetch.ref_cur_t;
    BEGIN
    OPEN l_ref_cur FOR dyn_fetch.g_query;
    RETURN l_ref_cur;
    END;
    END;
    Why we need two separate procedures (functions) in your package ?
    a) Receiving program must use dynamic SQL, but in dynamic block we can access
    only PL/SQL code elements that have global scope (standalone functions and procedures,
    and elements defined in the specification of a package).
    Unfortunately, cursor variables cannot be defined in the specification of a package
    (cannot be global variables).
    b) Receiving program must get the column list before ref cursor.
    So, we have two options: call (in receiving program) the same function two times
    (once to get the column list and once to return a ref cursor)
    or use one procedure (or function) for returning query (to get the column list)
    and second function for returning a ref cursor.
    3. Your receiving program
    CREATE OR REPLACE PROCEDURE test_fetch_ref_cur (p_query VARCHAR2 := NULL) IS
    l_statement VARCHAR2 (32000);
    FUNCTION process_def RETURN VARCHAR2 IS
    l_process_def VARCHAR2 (32000);
    BEGIN
    l_process_def := 'DBMS_OUTPUT.PUT_LINE (';
    FOR i IN 1 .. dyn_fetch.g_count LOOP
    l_process_def := l_process_def || ' l_record.col_' || i || ' || ''>>'' || ';
    END LOOP;
    l_process_def := RTRIM (l_process_def, ' || ''>>'' || ') || ');';
    RETURN l_process_def;
    END;
    BEGIN
    test.set_query (p_query);
    dyn_fetch.describe_columns;
    l_statement :=
    ' DECLARE' ||
    ' TYPE record_t IS RECORD (' ||
    dyn_fetch.record_def || ');' ||
    ' l_record record_t;' ||
    ' l_ref_cur dyn_fetch.ref_cur_t;' ||
    ' BEGIN' ||
    ' l_ref_cur := test.ref_cur;' ||
    ' LOOP' ||
    ' FETCH l_ref_cur INTO l_record;' ||
    ' EXIT WHEN l_ref_cur%NOTFOUND;' ||
    process_def ||
    ' END LOOP;' ||
    ' CLOSE l_ref_cur;' ||
    ' END;';
    EXECUTE IMMEDIATE l_statement;
    END;
    You can test this with:
    SET SERVEROUTPUT ON;
    EXECUTE test_fetch_ref_cur;
    Note that we can try to use more generic solution:
    CREATE OR REPLACE PACKAGE dyn_fetch IS
    -- SAME AS BEFORE, PLUS:
    PROCEDURE fetch_ref_cur (
    p_function_ref_cur VARCHAR2,
    p_process_def VARCHAR2);
    END;
    CREATE OR REPLACE PACKAGE BODY dyn_fetch IS
    -- SAME AS BEFORE, PLUS:
    PROCEDURE fetch_ref_cur (
    p_function_ref_cur VARCHAR2,
    p_process_def VARCHAR2)
    IS
    l_statement VARCHAR2 (32000);
    BEGIN
    l_statement :=
    ' DECLARE' ||
    ' TYPE record_t IS RECORD (' ||
    record_def || ');' ||
    ' l_record record_t;' ||
    ' l_ref_cur dyn_fetch.ref_cur_t;' ||
    ' BEGIN' ||
    ' l_ref_cur := ' ||
    p_function_ref_cur || ';' ||
    ' LOOP' ||
    ' FETCH l_ref_cur INTO l_record;' ||
    ' EXIT WHEN l_ref_cur%NOTFOUND;' ||
    p_process_def ||
    ' END LOOP;' ||
    ' CLOSE l_ref_cur;' ||
    ' END;';
    EXECUTE IMMEDIATE l_statement;
    END;
    END;
    CREATE OR REPLACE PROCEDURE test_fetch_ref_cur (p_query VARCHAR2 := NULL) IS
    FUNCTION process_def RETURN VARCHAR2 IS
    -- SAME AS BEFORE
    END;
    BEGIN
    test.set_query (p_query);
    dyn_fetch.describe_columns;
    dyn_fetch.fetch_ref_cur (
    p_function_ref_cur => 'test.ref_cur',
    p_process_def => process_def);
    END;
    Regards,
    Zlatko Sirotic

  • Dynamic Ref Cursor with Dynamic Fetch

    Hi,
    I'm using dynamic sql (DBMS_SQL) to define columns of ref cursor.
    It works Ok but the problem is when i'm using PL/SQL CURSOR in the REF CURSOR. Then,
    I'm getting :
    Error at line 3
    ORA-00932: inconsistent datatypes: expected NUMBER got CURSER
    ORA-06512: at "SYS.DBMS_SQL", line 1830
    ORA-06512: at "TW.PRINT_REF_CURSOR", line 28
    ORA-06512: at line 9
    Here is my code:
    set serveroutput on
    exec DBMS_OUTPUT.ENABLE(1000000);
    declare
    l_cursor sys_refcursor;
    begin
    OPEN l_cursor FOR
    SELECT SERVICE_TABLE.SERVICE, SERVICE_TABLE.SERVICE_GROUP, SERVICE_TABLE.SERVICE_DESC,
    CURSOR(SELECT SERVICE_TABLE.SERVICE_CD FROM SERVICE_TABLE) SERVICE_CD_CURSOR
    FROM SERVICE_TABLE ;
    print_ref_cursor( l_cursor );
    end;
    =========================
    CREATE OR REPLACE procedure print_ref_cursor
    ( p_query in out sys_refcursor,
    p_date_fmt in varchar2 default 'dd-mon-yyyy hh24:mi:ss' )
    is
    l_theCursor integer;
    l_columnValue varchar2(4000);
    l_descTbl dbms_sql.desc_tab2;
    l_colCnt number;
    l_date date;
    l_cursor SYS_REFCURSOR;
    begin
    l_theCursor := dbms_sql.to_cursor_number( p_query );
    dbms_sql.describe_columns2
    ( l_theCursor, l_colCnt, l_descTbl );
    -- define all columns to be cast to varchar2's, we
    -- are just printing them out
    for i in 1 .. l_colCnt loop
    if ( l_descTbl(i).col_type in ( 12, 178, 179, 180, 181, 231 ) )
    then
    dbms_sql.define_column
    (l_theCursor, i, l_date );
    else
    dbms_sql.define_column
    (l_theCursor, i, l_columnValue, 4000);
    end if;
    end loop;
    while ( dbms_sql.fetch_rows(l_theCursor) > 0 )
    loop
    for i in 1 .. l_colCnt loop
    if ( l_descTbl(i).col_type in ( 12, 178, 179, 180, 181, 231 ) )
    then
    dbms_sql.column_value( l_theCursor, i, l_date );
    l_columnValue := to_char( l_date, p_date_fmt );
    else
    dbms_sql.column_value( l_theCursor, i, l_columnValue );
    end if;
    dbms_output.put_line
    ( rpad( l_descTbl(i).col_schema_name || '.' ||
    l_descTbl(i).col_name, 30 ) || ': ' || l_columnValue );
    end loop;
    dbms_output.put_line( '-----------------' );
    end loop;
    dbms_sql.close_cursor( l_theCursor );
    end;
    Is there a solution or bypass?
    Regards,
    Tamir Geva

    No. The problem is that one cannot use DBMS_SQL.define_column() to define that column in the SQL projection as a cursor, and then use DBMS_SQL.column_value() to read it into a ref cursor variable.
    You can however detect the cursor column - the DBMS_SQL.describe_columns3() call will return a col_type value of 102. In which case you can treat it as an exception (i.e. not process that column in the projection).
    As a general issue - a cursor as a SQL column projection does not make sense to me. I have never used this in any production code. Nor do I see any reasons why.
    If you want that column in the projection to contain a "list" of sorts (the results of the cursor), then a nested table type can be used as projected type and the MultiSet() function used to execute the in-line SQL and provide that SQL cursor's result as an array/nested table.
    But even this approach raises the question why a standard relational join is not used?

  • Is it possible to save an image as PDF without actually converting it ?

    According to the Convert To PDF setting, it seems that no matter what format the source image is, it is always converted (into either lossless heavy format or a lossy lighweight format) when saved as a PDF.
    I want to use GIF because it's the best quality/weight for newspaper type of images, but when saved as PDF the text is always bold and blury. How can I avoid this ?

    No, one needs to convert the image to the PDF format by use of a program that can read the image and then create the data streams used by the PDF.
    I would not use a photo editing program to add text to an image, you might use a page layout program like InDesign. PDF does store text and images in different objects and data streams. This should provide a much better result.

  • Weak REF CURSOR: discarding records?

    Hi,
    I'd like to discard the first N records from a weak REF CURSOR without having to specify the structure of the cursor. Is this possible?
    For example:
    LOOP
    FETCH results INTO garbagecan
    EXIT WHEN results%NOTFOUND;
    END LOOP;
    It seems that FETCH statement requires INTO to be specified with the correct structure.
    -Jerome
    null

    it is not possible to fetch into some variable, which doesn't match the structure of the cursor variable.....
    There is one way.....you can discard the first n records while assigning the query to cursor variable....
    Frame ur query that ur going to assocaite to the cursor in the following way....
    select * from (select rownum a, test.* from test) where a > N
    where 'N' is the no of records that u don't want to be in the cursor.....
    if u have doubt in this approach get back to me...

  • Still not possible (4.0 EA3) to copy displayed column headings from ref cursor output.

    Hi,
    I've created an enhancement request to allow displayed column headings from ref_cursor output to be copied.
    This is still not possible (4.0 EA3)
    The ref cursor data can be copied, but not the headings..
    See July 2012 discussion of problem in comments at
    http://www.thatjeffsmith.com/archive/2011/12/sql-developer-tip-viewing-refcursor-output/

    Hi,
    I think you're out of luck... except if you're on 11g where you can use DBMS_SQL.TO_CURSOR_NUMBER to convert the REF CURSOR to a DBMS_SQL cursor, and then benefit from the DBMS_SQL package to get column details.
    http://download.oracle.com/docs/cd/B28359_01/appdev.111/b28419/d_sql.htm#CHDJDGDG

  • Fetch Ref Cursor Multiple Times

    create or replace
    PROCEDURE refcursor1
    AS
    TYPE r_cursor IS REF CURSOR;
    rcv_emp r_cursor;
    TYPE rec_emp IS record
    empno NUMBER,
    ename VARCHAR2(20 CHAR),
    deptno number
    recv_emp rec_emp;
    recv_emp2 rec_emp;
    PROCEDURE printemployeedetails AS
    BEGIN
      loop
      fetch rcv_emp INTO recv_emp;
      exit WHEN rcv_emp%notfound;
        dbms_output.put_line(recv_emp.empno||'-'||recv_emp.ename||'-'||recv_emp.deptno);
      END loop;
    END;
    PROCEDURE printemployeedetails2(p_emp r_cursor) IS
    BEGIN
      loop
      fetch p_emp INTO recv_emp2;
      exit WHEN p_emp%notfound;
        dbms_output.put_line(recv_emp2.empno||'-'||recv_emp2.ename||'-'||recv_emp2.deptno);
      end loop;
    END;
    BEGIN
      FOR i IN (SELECT deptno FROM dept order by deptno)
      loop
        OPEN rcv_emp FOR SELECT empno,ename,deptno FROM emp WHERE deptno=i.deptno;
        dbms_output.put_line(i.deptno);
        dbms_output.put_line('--------------------');
        dbms_output.put_line('calling printemployeedetails');
        printemployeedetails;
        dbms_output.put_line('                    ');
        dbms_output.put_line('calling printemployeedetails2');
        dbms_output.put_line('                    ');
        printemployeedetails2(rcv_emp);
        CLOSE rcv_emp;
      END loop;
    end;
    Output:
    10
    calling printemployeedetails
    7839-KING-10
    7782-CLARK-10
    7934-MILLER-10
    calling printemployeedetails2
    20
    calling printemployeedetails
    7566-JONES-20
    7788-SCOTT-20
    7902-FORD-20
    7369-SMITH-20
    7876-ADAMS-20
    calling printemployeedetails2
    30
    calling printemployeedetails
    7698-BLAKE-30
    7499-ALLEN-30
    7521-WARD-30
    7654-MARTIN-30
    7844-TURNER-30
    7900-JAMES-30
    calling printemployeedetails2
    40
    calling printemployeedetails
    calling printemployeedetails2
    Hello All,
    If i open a cursor once can i fetch the elements of a cursor n times like above? i see only either one of those procedures are printing the details but not both.
    Wonder why as i am passing the same ref cursor to a second procedure.
    It's neither throwing me an error saying the elements of ref cursor are already fetched.
    Thank you.

    >
    If i open a cursor once can i fetch the elements of a cursor n times like above? i see only either one of those procedures are printing the details but not both.
    Wonder why as i am passing the same ref cursor to a second procedure.
    It's neither throwing me an error saying the elements of ref cursor are already fetched.
    >
    You can't see any such thing. That code above won't even compile let alone run.
    The 'ref cursor' you are talking about is defined in a standalone procedure 'refcursor1' which does NOTHING but declare some variables that are then NEVER USED.
    The other procedures try to use variables that DO NOT EXIST since they are not declared in the procedure that is trying to use them. So those procedures won't compile and even if they did compile and run each execution only does ONE fetch so the anonymous block can't possibly produce multiple rows of output.  
    I don't know what you claim to be seeing but it certainly isn't anything produced by the code you posted.

  • Ref cursor and dynamic sql

    Hi..
    I'm using a ref cursor query to fetch data for a report and works just fine. However i need to use dynamic sql in the query because the columns used in the where condition and for some calculations may change dynamically according to user input from the form that launches the report..
    Ideally the query should look like this:
    select
    a,b,c
    from table
    where :x = something
    and :y = something
    and (abs(:x/:y........)
    The user should be able to switch between :x and :y
    Is there a way to embed dynamic sql in a ref cursor query in Reports 6i?
    Reports 6i
    Forms 6i
    Windows 2000 PRO

    Hello Nicola,
    You can parameterize your ref cursor by putting the query's select statement in a procedure/function (defined in your report, or in the database), and populating it based on arguments accepted by the procedure.
    For example, the following procedure accepts a strongly typed ref cursor and populates it with emp table data based on the value of the 'mydept' input parameter:
    Procedure emp_refcursor(emp_data IN OUT emp_rc, mydept number) as
    Begin
    -- Open emp_data for select all columns from emp where deptno = mydept;
    Open emp_data for select * from emp where deptno = mydept;
    End;
    This procedure/function can then be called from the ref cursor query program unit defined in your report's data model, to return the filled ref cursor to Reports.
    Thanks,
    The Oracle Reports Team.

  • How to count no of rows affected by a ref cursor ?

    Hi,
    I have taken a ref cursor inside the function since I want to return resultset to the calling application. I also need the NO. OF ROWS AFFECTED by the ref cursor. When I used refcur%rowcount, it returned me 0. Whereas actual no. of rows inside the table is 11.
    As a workaround I created a integer variable v_rcount and again executed the cursor's query using count() to store the no. of rows affected by the cursor query. See definition 1 and 2 :
    How can I get no. of rows affected by the ref cursor without taking additional variable ??? Any other means available ???
    DEFINITION ONE:
    create or replace function f_ref()
    RETURN curpkg.ref_cursor as
    stock_cursor curpkg.ref_cursor;
    v_rcount INTEGER;
    BEGIN
    OPEN stock_cursor FOR SELECT * FROM FIRSTTABLE;
    select count(*) into v_rcount from firsttable;
    dbms_output.put_line('['|| v_rcount||']');
    RETURN stock_cursor;
    END;
    OUTPUT:
    [11]
    DEFINITION TWO:
    create or replace function f_ref()
    RETURN curpkg.ref_cursor as
    stock_cursor curpkg.ref_cursor;
    v_rcount INTEGER;
    BEGIN
    OPEN stock_cursor FOR SELECT * FROM FIRSTTABLE;
    v_rcount := stock_cursor%rowcount;
    dbms_output.put_line('['|| v_rcount||']');
    RETURN stock_cursor;
    END;
    OUTPUT:
    [0]

    michaels>  DECLARE
       emp_row   emp%ROWTYPE;
       cur       sys_refcursor;
       FUNCTION f_ref
          RETURN sys_refcursor
       AS
          stock_cursor   sys_refcursor;
       BEGIN
          OPEN stock_cursor FOR
             SELECT *
               FROM emp;
          RETURN stock_cursor;
       END f_ref;
    BEGIN
       cur := f_ref;
       LOOP
          FETCH cur
           INTO emp_row;
          EXIT WHEN cur%NOTFOUND;
       END LOOP;
       DBMS_OUTPUT.put_line ('Fetched rows: ' || cur%ROWCOUNT);
       CLOSE cur;
    END;
    Fetched rows: 14

  • In SAP is it possible to identify duplicate BP master record?

    hi,
    In SAP is it possible to identify duplicate BP master record?
    Regards,
    babu

    Hi,
    You can identify the BP dupliate check. See the link
    http://help.sap.com/saphelp_crm50/helpdata/en/9a/6f9a3d13ce0450e10000000a114084/frameset.htm
    Regards
    Srinu

  • Create record variable that refers dynamic query assigned to a ref cursor?

    Hi ,
    Just explaining what I am trying to achieve:
    1) i have a hr.departments table that was loaded in hr schema on 1st oct 2012 with 4 columns(department_id, department_name, manager_id, location_id)
    2) now I have a new schema by my name 'rahul' and I have loaded departments table but now an additional column has come into picture,ie created_date, this table got loaded on 1st-Nov-2012
    3) Now going forward my columns could be dropped from the departments table (it can be a case), for example might be my departments table in my schema 'rahul' one day could comprise of only 3 columns(department_id,department_name,manager_id)
    4) Now in the next step, I have managed to extract common column names(in a single line where columns are delimited using a comma) from both the tables(hr.departments and rahul.departments) which are (department_id, department_name, manager_id, location_id) using all_tab_cols table and I have written a function for it which i will be pasting below.
    5) now going forward, using the above column names line with column names delimited using comma, I have used a ref cursor and assigned a query to it using the line of columns that I have extracted from the above point
    6) Now I want to create a record variable which refers to my ref cursor, something like we do when we create a record variable by reffering to an explicit cursor defination that we give in the declaration block.
    PS:
    1) I have been out of touch with plsql for a long time so I have lost a lot of mmeory regarding plsql.
    2) basically I need to compare data in hr.departments table with rahul.departments table for only columns that are common to both the tables, rest new or discarded columns information will go in one of the log tables that I have created(this is done already)
    Please help me, I did try searching on google for the same but it really confused me very badly, any kind of help is appreciated, please find my code below:
    Regards
    Rahul
    Code :
    ===================================================================================================
    create or replace procedure p_compare_data(fp_old_table_name in varchar2, fp_new_table_name in varchar2)
    is
    type ref_cursor_old_table is ref cursor;
    v_ref_cursor_old_table ref_cursor_old_table;
    --record_v_ref_cursor_old_table v_ref_cursor_old_table;
    --record_ref_cursor_old_table v_ref_cursor_old_table%ROWTYPE;
    Lv_all_column_names varchar2(2000);
    function f_fetch_common_column_names(fp_old_table_name in varchar2, fp_new_table_name in varchar2)
    return varchar2
    is
              Lv_all_column_names varchar2(2000);
    begin
              execute immediate 'select ltrim(all_column_names,'','') all_column_names_in_line from (select * from (select sys_connect_by_path(hr_e_column_name,'','') all_column_names from (select hr_e_column_name, rownum curr, rownum - 1 prev from (select hr_e.* , rahul_e.* , case when (hr_e_column_name = rahul_e_column_name) then 1 when (rahul_e_column_name is NULL) then 2 when (hr_e_column_name is NULL) then 3 end decision from (select column_name hr_e_column_name from all_tab_cols where owner ='''||substr(fp_old_table_name,1,instr(fp_old_table_name,'.',1,1)-1)||''' and table_name = '''||substr(fp_old_table_name,instr(fp_old_table_name,'.',1,1)+1)||''') hr_e full outer join (select column_name rahul_e_column_name from all_tab_cols where owner = '''||substr(fp_new_table_name,1,instr(fp_new_table_name,'.',1,1)-1)||''' and table_name = '''||substr(fp_new_table_name,instr(fp_new_table_name,'.',1,1)+1)||''') rahul_e on hr_e.hr_e_column_name = rahul_e.rahul_e_column_name) decision_table where decision = 1) a start with a.curr = 1 connect by prior curr = prev) b order by length(b.all_column_names) desc) all_column_names_in_line where rownum = 1' into Lv_all_column_names;
              return (Lv_all_column_names);
    end;
    begin
         Lv_all_column_names := f_fetch_common_column_names(fp_old_table_name, fp_new_table_name);
         --dbms_output.put_line(Lv_all_column_names);
         open v_ref_cursor_old_table for ('select '||Lv_all_column_names||' from '||fp_old_table_name);
    end;
    =====================================================================================================

    >
    6) Now I want to create a record variable which refers to my ref cursor, something like we do when we create a record variable by reffering to an explicit cursor defination that we give in the declaration block.
    >
    This thread is basically nothing more than a continuation of your original thread except now you are finally explaining what you are really trying to do.
    Re: passing table name to a procedure and then need to open a cursor ..
    In that original thread you said you found the solution
    >
    Well Mate thanks for your suggestion but I got it working through ref cusror, thanks for your time.
    >
    So I ask you to post your 'solution' and when you finally did it was clear that you hadn't solved anything at all. Your solution used a ref cursor all right but the code relied on a record ('record_employees') that was based on a table name ('employees') that was declared within the procedure. There isn't much point in passing in a table name if you have to hard-code the table name in the procedure.
    create or replace procedure p_ref_cursor(fp_old_table in varchar2)
    is
    type ref_cursor is REF CURSOR;
    v_ref_cursor ref_cursor;
    record_employees hr.employees%ROWTYPE;
    begin
    open v_ref_cursor for 'select * from '||fp_old_table;
    loop
    fetch v_ref_cursor into record_employees;
    exit when v_ref_cursor%NOTFOUND;
    dbms_output.put_line(record_employees.employee_id);
    end loop;
    end;Then sb92075 ask you the question that illustrates what I just said
    >
    what happens when you pass in "HR.DEPARTMENTS" ; besides throwing errors?
    >
    And you blew him off with this
    >
    Mate, departments never came in my context, in my prior message I explained what I was trying to achieve ... so I dont know what problem you are understanding reading my posts.
    >
    And now here you are asking how to get this to work for the departments table.
    It is very difficult to help someone that won't tell us what it is they are really trying to do so we can try to suggest some better ways of doing it. Hopefully, in the future, you wil start by explaining your problems instead of focusing on the solution you think you should use.
    Back to the issue -
    The first thing you should do is finish defining the requirements. Assuming the above actually works to identify columns that have different data what are you going to do with that information?
    1. Do you need to save that different data from TABLE1 somewhere?
    2. If you don't save it how will anyone look at it to decide which table has the correct data?
    3. If you do save it how will you save it 'generically' since other tables will have different columns and datatypes?
    4. What about the data from the same record in TABLE2; do you need to save that data somewhere?
    5. Will these two tables have primary keys? Are they on the same columns in each table? If not what if TABLE1 has one record but there are TWO records in TABLE2 that are identical. Is that a match? Or is that a problem because TABLE2 has an extra record even though the record is identical?
    In short detecting the differences is just one small part of the entire problem. You also need to save those differences somewhere so someone can examine the data and decide what action to take. That is the more difficult part of trying to implement a 'generic' solution.
    But now that we know what you are really trying to do take a read through this thread from 6 years ago. It has three different ways to pass a query to a procedure and get different output. You may want to save a copy of the thread since it has some very advanced techniques in it.
    How to pipeline a function with a dynamic number of columns?
    See ascheffer's reply Posted: May 9, 2006 4:53 AM for using data cartridge functionality with a pipelined function.
    See Kamal's reply Posted: May 10, 2006 4:49 AM - it shows how to get XML output.
    See BluShadow's reply Posted: Mar 27, 2009 1:50 AM - for using dynamic sql

  • DB proc - do you need to create a table to pass a ref cursor record type?

    I want to pass a limited selection of columns from a large table through a DB procedure using a REF CURSOR, returning a table rowtype:
    CREATE OR REPLACE package XXVDF_XPOS_DS021_ITEMS AS
         TYPE XXVDF_XPOS_DS021_ITEM_ARRAY
         IS REF CURSOR
         return XXVDF_XPOS_DS021_ITEM_TABLE%ROWTYPE;
    Do I need to create this dummy table?
    I can't get a TYPE to work, where the type is an OBJECT with the desired columns in it.
    So a dummy empty table will sit in the database...
    Is there another way?
    thanks!

    You can use RECORD type declaration:
    SQL> declare
      2   type rec_type is record (
      3    ename emp.ename%type,
      4    sal emp.sal%type
      5   );
      6   type rc is ref cursor return rec_type;
      7   rc1 rc;
      8   rec1 rec_type;
      9  begin
    10   open rc1 for select ename, sal from emp;
    11   loop
    12    fetch rc1 into rec1;
    13    exit when rc1%notfound;
    14    dbms_output.put_line(rec1.ename || ' ' || rec1.sal);
    15   end loop;
    16   close rc1;
    17  end;
    18  /
    SMITH 800
    ALLEN 1600
    WARD 1250
    JONES 2975
    MARTIN 1250
    BLAKE 2850
    CLARK 2450
    SCOTT 3000
    KING 5000
    TURNER 1500
    ADAMS 1100
    JAMES 950
    FORD 3000
    MILLER 1300or use, for example, VIEW to declare rowtype:
    SQL> create view dummy_view as select ename, sal from emp;
    View created.
    SQL> declare
      2   type rc is ref cursor return dummy_view%rowtype;
      3   rc1 rc;
      4   rec1 dummy_view%rowtype;
      5  begin
      6   open rc1 for select ename, sal from emp;
      7   loop
      8    fetch rc1 into rec1;
      9    exit when rc1%notfound;
    10    dbms_output.put_line(rec1.ename || ' ' || rec1.sal);
    11   end loop;
    12   close rc1;
    13  end;
    14  /
    SMITH 800
    ALLEN 1600
    WARD 1250
    JONES 2975
    MARTIN 1250
    BLAKE 2850
    CLARK 2450
    SCOTT 3000
    KING 5000
    TURNER 1500
    ADAMS 1100
    JAMES 950
    FORD 3000
    MILLER 1300 Rgds.

Maybe you are looking for

  • Anyone have an answer?

    When I insert an external drive (like a USB or a CD) it is not popping up onto my desktop like I thought it was supposed to, instead I have to go into my computer and open it through here.. Is there a way to fix or change this? or was I wrong and it

  • Corrupt shadows in LR 2.2 and ACR

    Hi there! I have an issue with both LR 2.2 and ACR for Windows XP. I calibrate my monitor with Colovision Spyder (first version) which then creates an ICC profile and a LUT for the graphics adapter. I calibrate for gamma 2.2 and color temperature 650

  • Report to view Appraisal Rating maintained in Infotype 147-Appraisals

    Hi All, Is there any standard report to view the ratings maintained for an employee in infotype 147-Appraisals? Please suggest. Thank you. Regards, Nishy

  • How to make a new row as selected in adf table

    Hi, I am adding a new row to my table as below. DCBindingContainer bindingContainer = (DCBindingContainer)ADFUtil.evaluateEL("#{bindings}"); DCIteratorBinding iter = bindingContainer.findIteratorBinding("IfwSystemBrandView1Iterator"); ViewObject vo =

  • QM Inspection Points

    Gurus I require that inspection points should be created automatically for inspection lots when i view QA32 To do so i have done settings in SPRO > QM > Quality Planning > Inspection Planning > General > Define identifier. Here i kept 2 under field T