Difference of fetching data, into a cursor variable and into a record?

whats the difference of fetching data via cursor, into a cursor variable and into a record.
i feel infact fetching data into cursor variable is more better, and plus lesser lines to code??

Can you explain what you mean, preferably with an example? Generally, you fetch data from a cursor, not into one. I'm assuming by "fetching into a record" you mean inserting into a table?
Justin

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.

  • Can I put a SQL query into a bind variable and then use it to output report

    Hi,
    Can I put a SQL query into a bind variable and then use it to output report?
    I want to create a report and an item "text area" (say P1_TEXT) which can let user to input a SQL query(they are all technical users and knows SQL very well). Then, I use a bind variable (that text area) to store the SQL statement. Then, I add a submit button and I want to use the following to output the report:
    select * from (:P1_TEXT);
    Do you think it is possible to do that? Any known limitations for APEX in this area?
    Thanks a lot,
    Angela

    You can, but make sure it's what you really want to do. Make sure you are VERY familiar with SQL Injection. Most people who know what it is, go out of their way to prevent SQL Injection. You're going out of your way to allow it.
    You can try using &P1_TEXT. instead of bind variable syntax. Bind variables are one of the best ways to prevent SQL Injection, which is why it's not working for you.
    Once again, I strongly urge you to consider the implications of your app, but this suggestion should get it working.
    Tyler

  • HT1144 I have stopped getting my e-mail in my mail folder on my mac pro. it goes into my I-pad and into my S4 phone  it was working yesterday morning and stopped suddenly

    I have stopped getting my e-mails in my mail and they only to to my safari
    it is coming into my I-pad and into my S4 phone
    could I get a suggestion to fix this this
    Mel

    http://support.apple.com/kb/TS3276
    sounds like a corrupt database or index
    the glitch is your home folder and mail folder, esp look at syncservices
    I'd post in the mail or Lion forum where you will find similar issues being discussed.
    http://www.apple.com/support/mail

  • Cursor variable and for update

    hi
    can anyone explain me why can't we use "for update " with a Cursor Variable.
    Thanks in advance.

    user10314274 wrote:
    exmple : I read it in one book(Oracle press)And how difficult is it to test this?
    declare
        v_cur sys_refcursor;
        v_ename varchar2(20);
    begin
        open v_cur for select ename from emp for update;
        loop
          fetch v_cur into v_ename;
          exit when v_cur%notfound;
          dbms_output.put_line(v_ename);
        end loop;
        close v_cur;
        dbms_output.put_line('----------------------');
        open v_cur for 'select ename from emp for update';
        loop
          fetch v_cur into v_ename;
          exit when v_cur%notfound;
          dbms_output.put_line(v_ename);
        end loop;
        close v_cur;
    end;
    SMITH
    ALLEN
    WARD
    JONES
    MARTIN
    BLAKE
    CLARK
    SCOTT
    KING
    TURNER
    ADAMS
    JAMES
    FORD
    MILLER
    SMITH
    ALLEN
    WARD
    JONES
    MARTIN
    BLAKE
    CLARK
    SCOTT
    KING
    TURNER
    ADAMS
    JAMES
    FORD
    MILLER
    PL/SQL procedure successfully completed.
    SQL> SY.

  • How to Fetch Data From Standard Table MARA and Display using BOPF ?

    Hello All,
    In BOPF creation of Quey to a node fetches data from the Data Base Table attached to that Node,
    But in my requirement I have to fetch data Present in a Standard table and Display it in the FPM List Using FBI.
    **  Can we Fetch the data From Standard Table and fill the Node in BOPF, Is this possible as the standard Table do not contain KEY field which BOPF uses for Data Fetching ?
    Kindly share your Idea's .
    Thanks in Adv.

    Hi Dhivya,
    Thanks For your Response.
    In my Requirement I want to make ROOT Node as Transient Node.
    When I create a Sub Node to a Root Node, I am able to get this option to make this sub node as a Transient Node .
    By selecting   'Standard<-->Extended' option in the Menu item 'GoTo' I am able to get this Transient Node check box field for the Sub Nodes.
    I want to make a ROOT Node as a Transient Node.
    (Which Version you are using, and which transaction you are using to create BO . we are using BOBX Transaction, Version Ehp 6 )
    Kindly Guide me .
    Thanks,
    Kranthi Kumar.

  • Find the difference between two dates for the specific month and year

    Hi,
    I have two dates, start date is 30/12/2012 and end date is 04/01/2013. Using datediff I found the difference of days between two dates. But I find the no of days in January 2013. ie output is 4 instead of 6. I input month and year to find the no of days
    for that date. In this case I input Jan 2013. How can I sql this ?

    I don't understand how most of the answers provided here not analytically solving the problem with many cases possible.
    First let me understand you:
    You have 2 dates range and you want to calculate day range for specific month and year between the original date range.
    declare @for_month int = 1 --January
    declare @for_year int = 2013
    declare @StartDate date = '2012-12-20'
    declare @EndDate date = '2013-01-04'
    SELECT
    CASE
    WHEN (DATEPART(MONTH, @StartDate) = @for_month and DATEPART(MONTH, @EndDate) = @for_month) and ((DATEPART(YEAR, @StartDate) = @for_year or DATEPART(YEAR, @EndDate) = @for_year)) THEN
    DATEDIFF(DAY, @StartDate,@EndDate)
    WHEN (@StartDate < cast(CONVERT(varchar(4), @for_year) + '-' + CONVERT(varchar(2), @for_month) + '-01' as date)) and (@EndDate between (cast(CONVERT(varchar(4), @for_year) + '-' + CONVERT(varchar(2), @for_month) + '-01' as date)) and (cast(DATEADD(d, -1, DATEADD(m, DATEDIFF(m, 0, cast( CONVERT(varchar(4), @for_year) + '-' + CONVERT(varchar(2), @for_month) + '-01' as date)) + 1, 0)) as date))) THEN
    DATEDIFF(DAY, DATEADD(MONTH, DATEDIFF(MONTH, -1, @EndDate)-1, 0),@EndDate)
    WHEN (@EndDate > cast(DATEADD(d, -1, DATEADD(m, DATEDIFF(m, 0, cast( CONVERT(varchar(4), @for_year) + '-' + CONVERT(varchar(2), @for_month) + '-01' as date)) + 1, 0)) as date)) and (@StartDate between (cast(CONVERT(varchar(4), @for_year) + '-' + CONVERT(varchar(2), @for_month) + '-01' as date)) and (cast(DATEADD(d, -1, DATEADD(m, DATEDIFF(m, 0, cast( CONVERT(varchar(4), @for_year) + '-' + CONVERT(varchar(2), @for_month) + '-01' as date)) + 1, 0)) as date))) THEN
    DATEDIFF(DAY, @StartDate,DATEADD(d, -1, DATEADD(m, DATEDIFF(m, 0, @StartDate) + 1, 0))) + 1
    WHEN ((DATEDIFF(DAY, @StartDate, cast(DATEADD(d, -1, DATEADD(m, DATEDIFF(m, 0, cast( CONVERT(varchar(4), @for_year) + '-' + CONVERT(varchar(2), @for_month) + '-01' as date)) + 1, 0)) as date)) >= 0) and (DATEDIFF(DAY, cast(CONVERT(varchar(4), @for_year) + '-' + CONVERT(varchar(2), @for_month) + '-01' as date), @EndDate) >= 0)) THEN
    DATEDIFF(DAY, cast( CONVERT(varchar(4), @for_year) + '-' + CONVERT(varchar(2), @for_month) + '-01' as datetime), DATEADD(d, -1, DATEADD(m, DATEDIFF(m, 0, cast( CONVERT(varchar(4), @for_year) + '-' + CONVERT(varchar(2), @for_month) + '-01' as datetime)) + 1, 0))) + 1
    ELSE
    0
    END as [DD]
    I don't know how you calculate day range between 01/01/2013 and 04/01/2013
    is 4, it is actually is 3 but if that is the case, you can add 1 from the condition.

  • Fetching data from one internal table and populating into other table

    hi
    i am working on a report program.
    here,i have my data in a internal table(having only one field) i.e. only one column in the table.
    now i want to populate this data into other internal internal table that has 13fields i.e. 13 columns.
    so could anyone let me know how can i work on this.
    i am looping the internal table with one field into a work area from there i am not able to proceed ahead.
    thanks in advance

    hi
    i would like to clear one point here i.e. my internal table into which i want to populate data is a dynamic internal table....so i cant give the fieldnames here..
    here i am pasting the code..so that u may get some idea...
    "generating dynamic internal table :
    ref_descr ?= cl_abap_typedescr=>describe_by_data( it_itab ).
    it_details[] = ref_descr->components[].
    loop at it_details into wa_comp.
    cnt = cnt + 1.
    wa_fcat-col_pos = cnt.
    wa_fcat-fieldname = wa_comp-name.
    append wa_fcat to t_fcat.
    endloop.
    CALL METHOD CL_ALV_TABLE_CREATE=>CREATE_DYNAMIC_TABLE
      EXPORTING
        I_STYLE_TABLE             =
        IT_FIELDCATALOG           = t_fcat
        I_LENGTH_IN_BYTE          =
      IMPORTING
       EP_TABLE                  = lo_table
        E_STYLE_FNAME             =
      EXCEPTIONS
        GENERATE_SUBPOOL_DIR_FULL = 1
        others                    = 2
    so lo_table is my dynamic internal table.
    ASSIGN lo_table->* TO <f_tab>.
    using the GUI_UPLOAD i have uploaded the data file(separated by semicolon)  from presentation server and its in the table itab.
    with the following logic i am capturing the data in between the semicolon's.
    LOOP AT itab INTO wa_itab.
    find all occurrences of c_semicolon in wsg_string results result_tab.
    loop at result_tab into wa_reslt.
    if sy-tabix = 1.
    str1 = wsg_string+0(wa_reslt-offset).
    wa_prev_offset  = wa_reslt-offset.
    wa_prev_offset = wa_prev_offset + 1.
    clear str1.
      else.
       count = wa_reslt-offset - wa_prev_offset.
       count = count.
    str2 = wsg_string+wa_prev_offset(count).
    clear str2.
    wa_prev_offset  = wa_reslt-offset.
    wa_prev_offset = wa_prev_offset + 1.
    enddo.
    endif.
    endloop.
    data is there in the fields str1 and str2.now i want to populate this data into dynamic internal table row-wise.
    could anyone suggest me how can i do this

  • How to select data from one nested table and into another nested table

    create or replace
    TYPE ctxt_code_rec as object
    ctxt_header varchar2(10),
    header_description varchar2(300),
    status varchar2(30),
    adjacent_code varchar2(300),
    adjacent_desc Varchar2(400),
    adjacent_flag varchar2(4000),
    adjacent_text_href varchar2(4000)
    create or replace
    type ctxt_code_table as table of CTXT_CODE_REC
    d_table ctxt_code_table ;
    v_tab ctxt_code_table ;
    Iam trying to select data from d_table to v_tab
    using and bulk collect into
    select m.*
    bulk collect into p_code_result
    from table(l_loop_diag_code_table1)m
    order by 1;
    Receiving error:
    ora 94007 : not enoughvalues
    Could you please let me know how to solve it?
    Thanks,
    in advance

    >
    create or replace
    TYPE ctxt_code_rec as object
    ctxt_header varchar2(10),
    header_description varchar2(300),
    status varchar2(30),
    adjacent_code varchar2(300),
    adjacent_desc Varchar2(400),
    adjacent_flag varchar2(4000),
    adjacent_text_href varchar2(4000)
    create or replace
    type ctxt_code_table as table of CTXT_CODE_REC
    d_table ctxt_code_table ;
    v_tab ctxt_code_table ;
    Iam trying to select data from d_table to v_tab
    using and bulk collect into
    select m.*
    bulk collect into p_code_result
    from table(l_loop_diag_code_table1)m
    order by 1;
    Receiving error:
    ora 94007 : not enoughvalues
    Could you please let me know how to solve it?
    >
    Not unless you provide the code you are actually using.
    There is no definition of 'p_code_result' in your post and you say you 'trying to select data from d_table' but there is no code that loads 'd_table' in what you posted.
    And the SELECT query you posted actuall selects from an object named 'l_loop_idag_code_table1' which isn't mentioned in your code.
    Post the actual code you are using and all of the structures being used.
    Also explain why you even need to use nested tables and PL/SQL for whatever it is you are really doing.

  • Fetch data from a R3 Table and send to XI

    Hello XI Experts,
                     I am involved in a scenario where i need to read some data from a Transparent table in R3 send to xi and post a CSV File.
    I have worked on look ups at the XI abap stack but i want to know which is the best solution to do this.
    What's the best way to read a table from R/3 and send to xi server. Is there any other ways besides Look ups? 
    Regards,
    Anirban.

    Hi,
    >>>1. When is look ups advised .
    a lookup is when somewhere in your
    interface flow you have to lookup some data
    for example you send data from one system to another but you have to change material numbers before
    sending the data to the target system
    you know that material numbers are inside some other server so you have to call this sever (very often in a sync way) to lookup the material numbers
    so this would be difficult with IDOC for instance
    >>> 2. Which is a better solution, if i have very few data to fetch with WAS >= 6.20 . Abap Proxy Or a 'Z' Idoc.
    you'll be able to do it quicker with ABAP proxy
    if you use a scheduled report that will send the data from r3 then use ABAP proxies
    Regards,
    michal

  • HOW TO  WRITE DATA INTO A CLOB VARIABLE IN PL SQL-URGENT!!!!!!!

    Hi,
    In a PLSQL procedure, i need to store all error messages into a CLOB variable and finally RETURN the Clob.
    While using DBMS_LOB.wirteappend, i am getting Error like
    "INVALID LOB LOCATOR SPECIFIED".
    Here is the code sample i used,
    CREATE OR REPLACE procedure ErrorAppend
    is
    clobObj CLOB;
    amt binary_integer:=32767;
    errormsg varchar2(2000);
    BEGIN
    for j in 1..3
    loop
    BEGIN
    insert into errortest values(j);
    EXCEPTION
    WHEN others THEN
    errormsg:='ERROR OCCURED '||SQLERRM;
    dbms_lob.writeappend(clobObj,amt,errormsg);
    END;
    commit;
    end loop;
    dbms_output.put_line(clobObj);
    END ErrorAppend;
    Here i am not allowed to get empty_clob/clob from the table.
    Any help will be apprciated.
    rgds,
    senthil.

    InputStream inputStream = new ByteArrayInputStream(mss.getBytes());Use this to push into the file>
    Ummm.... no. InputStreams are for reading, not for writing.
    Anyway, OP, you're making a mistake. You're trying to open a stream to a file called "This is for dedo". Use FileOutputStream(File file) constructor or FileOutputStream(String pathToFile). You can then wrap that stream with a PrintWriter or whatever you need.

  • How to (in Pro*C) pass a cursor variable as a pointer between functions

    I am opening a cursor in a called function that accepts as one argument a pointer to a cursor variable, and a second argument for the sql string defining the cursor select. That works fine, and in that same function can successfully fetch and access the records from the cursor. However, my objective is to fetch the records in another function that calls the first function. I want to pass back to the calling function the pointer to the cursor variable from the first function. In the second (calling) function, is where I want to fetch the records. What I am getting is SQLCODE = -1002 (fetch out of sequence).
    Here is the relevent code in the first called function that calls a PL/SQL package procedure to open the cursor, followed by the code in the second calling procedure where I am attempting to fetch the records:
    /******Called function code starts here ******/
    EXEC SQL INCLUDE SQLCA;
    long db_makeCursor(cursorID, SQLstr)
    EXEC SQL BEGIN DECLARE SECTION;
    sql_cursor cursorID;      / a pointer to a cursor variable */
    char *SQLstr;
    EXEC SQL END DECLARE SECTION;
    long SQLCODE;
    EXEC SQL BEGIN DECLARE SECTION;
    sql_cursor dbCursor; /* a cursor variable */
    EXEC SQL END DECLARE SECTION;
    EXEC SQL WHENEVER SQLERROR DO sql_error();
    dbCursor = *cursorID;
    EXEC SQL EXECUTE
    BEGIN
    db_util_ref_cursor_pkg.open_dbNameCursor( :dbCursor, :SQLstr );
    END;
    END-EXEC;
    return;
    /******Calling function code starts here ******/
    EXEC SQL INCLUDE SQLCA;
    EXEC SQL BEGIN DECLARE SECTION;
    static PROCLOG PROC_Rec;
    EXEC SQL END DECLARE SECTION;
    long retrieveProcesses( _proclog, sqlForProcLog )
    EXEC SQL BEGIN DECLARE SECTION;
    PROCLOG _proclog;
    char *sqlForProcLog
    EXEC SQL END DECLARE SECTION;
    long rc;
    long SQLCODE;
    EXEC SQL BEGIN DECLARE SECTION;
    sql_cursor dbCursor; /* a cursor variable */
    sql_cursor cursorID;      / a pointer to a cursor variable */
    PROCLOG proclog;
    short end_ts_ind;
    short proc_name_ind;
    short comments_ind;
    EXEC SQL END DECLARE SECTION;
    cursorID = &dbCursorName; /* assign a value to the pointer */
    EXEC SQL ALLOCATE :dbCursor;
    rc = dbUtilities_makeCursor( cursorID, sqlForProcLog);
    if (rc == TRUE)
    while (SQLCODE == 0)
    EXEC SQL FETCH :dbCursorName INTO
    :proclog.PROC_ID,
    :proclog.START_TS,
    :proclog.END_TS:end_ts_ind,
    :proclog.PROC_NAME:proc_name_ind,
    :proclog.COMMENTS:comments_ind,
    if (SQLCODE == 0)
    printf("PROC_ID: %d; COMMENTS: %s\n",proclog.PROC_ID, proclog.COMMENTS);
    } /* end retrieveProcesses */

    You need to include a loop...
    for i=0;i<sqlca.sqlerrd[2];i++)
    printf("name %s\n", struct.name)
    This allows you to step through your cursor records!!!

  • Cursor Variable Arguments

    Migration Workbench Ver 1.2.2 has migrated an SQL Server 6.5
    strored procedure as a package containing only the Cursor
    variable and a procedure with INOUT parameter with packaged
    cusorvariable as one of the parameters to Oracle 8.0.5.
    How do you execute this procedure from SQL + and from another
    PL/SQL block where you have to retrive the data elements of the
    cursor.(ie.How do you input the cursor variable parameter in the
    EXECUTE stored procedure command.)An example of the type is
    appreciated.
    null

    Surendra,
    Using refcursors between procedures is covered in the 'Wrong
    number or types of argument in call to stored proc' 4 jun thread,
    with reference to the manuals.
    Using refcursor bind variables is covered in the sqlplus user
    guide and reference reproduced below (from the 8.1.5 version,
    also in 8.0.5) available on line on OTN.
    Hope that helps,
    Turloch
    Oracle Migration Workbench Team
    Using REFCURSOR Bind Variables
    SQL*Plus REFCURSOR bind variables allow SQL*Plus to fetch and
    format the results of a SELECT statement contained in a PL/SQL
    block.
    REFCURSOR bind variables can also be used to reference PL/SQL
    cursor variables in stored procedures. This allows you to store
    SELECT statements in the database and reference them from
    SQL*Plus.
    A REFCURSOR bind variable can also be returned from a stored
    function.
    Note:
    You must have Oracle7, Release 7.3 or above to assign
    the return value of a stored function to a
    REFCURSOR variable.
    Example 3-18 Creating, Referencing, and Displaying REFCURSOR Bind
    Variables
    To create, reference and display a REFCURSOR bind variable, first
    declare a local bind variable of the REFCURSOR datatype
    SQL> VARIABLE dept_sel REFCURSOR
    Next, enter a PL/SQL block that uses the bind variable in an OPEN
    ... FOR SELECT statement. This statement opens a cursor variable
    and executes a query. See the PL/SQL User's Guide and Reference
    for information on the OPEN command and cursor variables.
    In this example we are binding the SQL*Plus dept_sel bind
    variable to the cursor variable.
    SQL> BEGIN
    2 OPEN :dept_sel FOR SELECT * FROM DEPT;
    3 END;
    4 /
    PL/SQL procedure successfully completed.
    The results from the SELECT statement can now be displayed in
    SQL*Plus with the PRINT command.
    SQL> PRINT dept_sel
    DEPTNO DNAME LOC
    10 ACCOUNTING NEW YORK
    20 RESEARCH DALLAS
    30 SALES CHICAGO
    40 OPERATIONS BOSTON
    The PRINT statement also closes the cursor. To reprint the
    results, the PL/SQL block must be executed again before using
    PRINT.
    Example 3-19 Using REFCURSOR Variables in Stored Procedures
    A REFCURSOR bind variable is passed as a parameter to a
    procedure. The parameter has a REF CURSOR type. First, define the
    type.
    SQL> CREATE OR REPLACE PACKAGE cv_types AS
    2 TYPE DeptCurTyp is REF CURSOR RETURN dept%ROWTYPE;
    3 END cv_types;
    4 /
    Package created.
    Next, create the stored procedure containing an OPEN ... FOR
    SELECT statement.
    SQL> CREATE OR REPLACE PROCEDURE dept_rpt
    2 (dept_cv IN OUT cv_types.DeptCurTyp) AS
    3 BEGIN
    4 OPEN dept_cv FOR SELECT * FROM DEPT;
    5 END;
    6 /
    Procedure successfully completed.
    Execute the procedure with a SQL*Plus bind variable as the
    parameter.
    SQL> VARIABLE odcv REFCURSOR
    SQL> EXECUTE dept_rpt(:odcv)
    PL/SQL procedure successfully completed.
    Now print the bind variable.
    SQL> PRINT odcv
    DEPTNO DNAME LOC
    10 ACCOUNTING NEW YORK
    20 RESEARCH DALLAS
    30 SALES CHICAGO
    40 OPERATIONS BOSTON
    The procedure can be executed multiple times using the same or a
    different REFCURSOR bind variable.
    SQL> VARIABLE pcv REFCURSOR
    SQL> EXECUTE dept_rpt(:pcv)
    PL/SQL procedure successfully completed.
    SQL> PRINT pcv
    DEPTNO DNAME LOC
    10 ACCOUNTING NEW YORK
    20 RESEARCH DALLAS
    30 SALES CHICAGO
    40 OPERATIONS BOSTON
    Example 3-20 Using REFCURSOR Variables in Stored Functions
    Create a stored function containing an OPEN ... FOR SELECT
    statement:
    SQL> CREATE OR REPLACE FUNCTION dept_fn RETURN -
    cv_types.DeptCurTyp IS2 resultset cv_types.DeptCurTyp;
    3 BEGIN
    4 OPEN resultset FOR SELECT * FROM DEPT;
    5 RETURN(resultset);
    6 END;
    7 /
    Function created.
    Execute the function.
    SQL> VARIABLE rc REFCURSOR
    SQL> EXECUTE :rc := dept_fn
    PL/SQL procedure successfully completed.
    Now print the bind variable.
    SQL> PRINT rc
    DEPTNO DNAME LOC
    10 ACCOUNTING NEW YORK
    20 RESEARCH DALLAS
    30 SALES CHICAGO
    40 OPERATIONS BOSTON
    4 rows selected
    The function can be executed multiple times using the same or a
    different REFCURSOR bind variable.
    SQL> EXECUTE :rc := dept_fn
    PL/SQL procedure successfully completed.
    SQL> PRINT rc
    DEPTNO DNAME LOC
    10 ACCOUNTING NEW YORK
    20 RESEARCH DALLAS
    30 SALES CHICAGO
    40 OPERATIONS BOSTON
    Surendra Kumar (guest) wrote:
    : Migration Workbench Ver 1.2.2 has migrated an SQL Server 6.5
    : strored procedure as a package containing only the Cursor
    : variable and a procedure with INOUT parameter with packaged
    : cusorvariable as one of the parameters to Oracle 8.0.5.
    : How do you execute this procedure from SQL + and from
    another
    : PL/SQL block where you have to retrive the data elements of the
    : cursor.(ie.How do you input the cursor variable parameter in
    the
    : EXECUTE stored procedure command.)An example of the type is
    : appreciated.
    Oracle Technology Network
    http://technet.oracle.com
    null

  • Cursor Variable in Nested Block

    Dear all,
    I have a package that has procedures that open cursor variables and print the queries of sample schema HR. There's one procedure that opens the cursor with an input integer to choose which query that wants to be fetched. The other prints the query by fetching the cursor in a nested block with exceptions. The following package runs as intended, it prints all the three options without any problems:
    CREATE OR REPLACE PACKAGE admin_data AS
    TYPE gencurtyp IS REF CURSOR;
    PROCEDURE open_cv (generic_cv IN OUT gencurtyp, choice INT);
    procedure print_cv (generic_cv gencurtyp);
    END admin_data;
    CREATE OR REPLACE PACKAGE BODY admin_data AS
    PROCEDURE open_cv (generic_cv IN OUT gencurtyp, choice INT) IS
    BEGIN
    IF choice = 1 THEN
    OPEN generic_cv FOR SELECT * FROM jobs where job_id='ST_MAN';
    ELSIF choice = 2 THEN
    OPEN generic_cv FOR SELECT * FROM departments where department_id=270;
    ELSIF choice = 3 THEN
    OPEN generic_cv FOR SELECT * FROM employees where employee_id=206;
    END IF;
    END;
    procedure print_cv (generic_cv gencurtyp)is
    employees_rec employees%rowtype;
    departments_rec departments%rowtype;
    jobs_rec jobs%rowtype;
    begin
    fetch generic_cv into jobs_rec;
    dbms_output.put_line(jobs_rec.job_title);
    exception
    when ROWTYPE_MISMATCH then
      begin
      fetch generic_cv into departments_rec;
      dbms_output.put_line(departments_rec.department_name);
      exception
      when ROWTYPE_MISMATCH then
        dbms_output.put_line('row mismatch');
        fetch generic_cv into employees_rec;
        dbms_output.put_line(employees_rec.first_name);
      when OTHERS then
        dbms_output.put_line('others');
        fetch generic_cv into employees_rec;
        dbms_output.put_line(employees_rec.first_name);
      end;
    end print_cv;
    END admin_data;
    declare
    some_cur admin_data.gencurtyp;
    begin
    admin_data.open_cv(some_cur,1);
    admin_data.print_cv(some_cur);
    admin_data.open_cv(some_cur,2);
    admin_data.print_cv(some_cur);
    admin_data.open_cv(some_cur,3);
    admin_data.print_cv(some_cur);
    admin_data.open_cv(some_cur,3);
    admin_data.print_cv(some_cur);
    admin_data.open_cv(some_cur,1);
    admin_data.print_cv(some_cur);
    admin_data.open_cv(some_cur,2);
    admin_data.print_cv(some_cur);
    end;
    17  /
    Stock Manager
    Payroll
    row mismatch
    William
    row mismatch
    William
    Stock Manager
    Payroll
    PL/SQL procedure successfully completed.The innermost block executes 'rowtype mismatch' exception block, which fetches
    SELECT * FROM employees where employee_id=206 query.
    This time, I switch the query fetch so that
    SELECT * FROM employees where employee_id=206query is in the outermost block and
    SELECT * FROM jobs where job_id='ST_MAN' is in the innermost block. The package body looks like this:
    CREATE OR REPLACE PACKAGE BODY admin_data AS
    PROCEDURE open_cv (generic_cv IN OUT gencurtyp, choice INT) IS
    BEGIN
    IF choice = 1 THEN
    OPEN generic_cv FOR SELECT * FROM jobs where job_id='ST_MAN';
    ELSIF choice = 2 THEN
    OPEN generic_cv FOR SELECT * FROM departments where department_id=270;
    ELSIF choice = 3 THEN
    OPEN generic_cv FOR SELECT * FROM employees where employee_id=206;
    END IF;
    END;
    procedure print_cv (generic_cv gencurtyp)is
    employees_rec employees%rowtype;
    departments_rec departments%rowtype;
    jobs_rec jobs%rowtype;
    begin
    fetch generic_cv into employees_rec;
    dbms_output.put_line(employees_rec.first_name);
    exception
    when ROWTYPE_MISMATCH then
      begin
      fetch generic_cv into departments_rec;
      dbms_output.put_line(departments_rec.department_name);
      exception
      when ROWTYPE_MISMATCH then
        dbms_output.put_line('row mismatch');
        fetch generic_cv into jobs_rec;
        dbms_output.put_line(jobs_rec.job_title);
      when OTHERS then
        dbms_output.put_line('others');
        fetch generic_cv into jobs_rec;
        dbms_output.put_line(jobs_rec.job_title);
      end;
    end print_cv;
    END admin_data;
    then I run the same anonymous block, I get:declare
    some_cur admin_data.gencurtyp;
    begin
    admin_data.open_cv(some_cur,1);
    admin_data.print_cv(some_cur);
    admin_data.open_cv(some_cur,2);
    admin_data.print_cv(some_cur);
    admin_data.open_cv(some_cur,3);
    admin_data.print_cv(some_cur);
    admin_data.open_cv(some_cur,3);
    admin_data.print_cv(some_cur);
    admin_data.open_cv(some_cur,1);
    admin_data.print_cv(some_cur);
    admin_data.open_cv(some_cur,2);
    admin_data.print_cv(some_cur);
    end;
    17 /
    others
    Payroll
    William
    William
    others
    Payroll
    PL/SQL procedure successfully completed.
    The strangest thing is the innermost block execute OTHERS exception block instead of ROWTYPE MISMATCH and the the record doesn't fetch anything. What happen? How come the result is different when I only switch the query?
    Best regards,
    Val

    Hi Sy,
    thanks for the reply, yes I agree that the code is cumbersome, I'm studying to prepare OCP PL/SQL certification, so I'm playing around with cursor variable in order to grasp the whole concept. I'm observing the behaviour of weak cursor variable when getting passed into a function and fetched couple of times and exploring exception propagation in the same time. This why the code looks not relevant in the real world.
    Anyway, I just curious how it behaves like that. Here's my instance info:
    SQL> select * from v$version
      2  ;
    BANNER
    Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
    PL/SQL Release 11.2.0.1.0 - Production
    CORE     11.2.0.1.0     Production
    TNS for Linux: Version 11.2.0.1.0 - Production
    NLSRTL Version 11.2.0.1.0 - Production

  • How to fetch data from bseg based  on 2 internal tables in 1 select query?

    hi,
    i have bukrs and belnr in one internal table and bukrs and vbeln in another internal table..now if i select from bseg twice using for all entries for fields bukrs vbeln and bukrs belnr twice in 2 select statements its fine..but since bseg is a huge table i want to use select only once..but the problem is that none of the belnrs in the one internal table will have vbeln in another internal table....ie if vbeln is selected from bseg based on belnr in 1 internal table non eof those vbeln will be equal to vbeln on another internal tables..in this scenario even if i take all data from two internal tables into 1 single internal table and then use for all entries on vbeln and belnr of that table ..but i think this will not work because and AND operator will not work because no record will satisfy both vbeln and belnr conditions..even if i use OR operator if the 1st condition satisfies the select clause will not enter into the second clause and all the records will not be fetched...Please help how should i tackle this..

    Hi vijaya,
    no simple select statement solution so far.
    As BSEG is a huge table you should try to get the full key values into another internal table first.
    SAP avoids direct selections from BSEG.
    Pleas consider the use of one of the secondary index tables first:
          BSAD : Accounting: Secondary Index for Customers (Cleared Items)
          BSAK : Accounting: Secondary Index for Vendors (Cleared Items)
          BSAS : Accounting: Secondary Index for G/L Accounts (Cleared Item
          BSEC : One-Time Account Data Document Segment
          BSEG : Accounting Document Segment
          BSID : Accounting: Secondary Index for Customers
          BSIK : Accounting: Secondary Index for Vendors
          BSIS : Accounting: Secondary Index for G/L Accounts
    you may include
          BKPF : Accounting Document Header
    for restrictions about time and document type.
    The last step is a FOR ALL ENTRIES selection from BSEG providing the full key values.
    Although you may have even more selects, the overall performance will be much better.
    Regards,
    Clemens

Maybe you are looking for