At what point is PL/SQL cursor data retrieved?

Hi
This may sound like a silly question but since I am not entirely sure, I thought I'd ask.
In Oracle PL/SQL, do you know at what stage the query in a cursor is actually executed?
Is it at the DECLARE stage or when you first OPEN / LOOP on it?
I suspect upon the OPEN or the LOOP.
I.e. if I had a cursor declared but did not loop (using FOR .. LOOP) through it for some hours, would the query results be as at the time of declare or as at the time of the OPEN / FOR...LOOP?
E.g.
CURSOR invoices
  IS
    SELECT invoice_number
    FROM unpaid_invoices
    WHERE due_date < TRUNC (SYSDATE)
BEGIN
-- Some time consuming processing that will take an hour
-- Start Loop through cursor of invoices.
  <<overdue_invoices>>
  FOR inv IN invoices LOOP
-- Some processing on the invoice
  END LOOP overdue_invoices;
END;Would the cursor details for invoices be as at the CURSOR declaration or when the FOR inv... is reached?
Thanks
Martin

When you use a for loop like that it implicitly opens the cursor at the start and closes it at the end of the loop.
But the SQL query in that cursor is actually "run" when the first implicit or explicit fetch is issued, and retrieves new rows for every subsequent fetch.
Of course, before rows can be retrieved there needs to be several other tasks.
There's a good description of the steps of SQL processing in this part of the documentation.
http://download.oracle.com/docs/cd/E11882_01/appdev.112/e10471/adfns_sqlproc.htm
PL/SQL implicitly declares a cursor for all SQL data manipulation statements, including queries that return only one row. For queries that return multiple rows, you can explicitly declare a cursor to process the rows individually.
A cursor is a handle to a specific private SQL area. In other words, a cursor can be thought of as a name for a specific private SQL area. A PL/SQL cursor variable enables the retrieval of multiple rows from a stored subprogram.Edited by: fsitja on Dec 15, 2009 1:03 AM

Similar Messages

  • SQL server, data retrieval in real time?

    Hi, I am trying to write a simple java program that will connect to a SQL database and retrieve inputted data. Ideally, I would like it to be outputted in some format, possibly string.. XML, it really doesn't matter to me. I really would like it to be in real time though (ie. when new data is inputted, the data is immediately outputted to the screen). Could someone point me in a direction? I am not really that good with programming, some sample code would be helpful. Thank you in advance.

    Well, to start with, the programmer at the user interface end, can tweak their code a little, but in reality, you are not going to impact the function of the DB very much unless you have adm rights in the DB and can do opimizations on it according to what your users need--such as indexes and physical ordering data.
    That said, your going to JDBC to connect to the DB or the JDBC/ODBC bridge. Look up the form and optimizations of sql select queries for your choosen DB.

  • Need to know at what point does file get copied to table

    Hi.
    I'm looking at the file browse page item and wanted to find out at what point does the file's data get placed in wwv_flow_files?
    Does it do it automatically on page submit/refresh?

    Hi Alistair,
    there is no name for this process since it is not an APEX process. The mod_plsql or
    PL/SQL embedded Gateway code is responsible for this. That's also the reason why
    this must be configured in the modplsql's dads.conf file.
    The insertion of files into WWV_FLOW_FILES is a process which you cannot
    influence in an APEX environment. What you can do is coding an onSubmit process
    which SELECTs the file from WWV_FLOW_FILES and inserts it into your own
    table ... but the file will always take the path via WWV_FLOW_FILES
    Does this help ...?
    -Carsten
    Oracle 11g Release 2 ist freigegeben!
    http://www.oracle.com/global/de/community/index.html
    SQL und PL/SQL: Tipps, Tricks & Best Practice
    http://sql-plsql-de.blogspot.com

  • Data Retrieval in task audit

    Hello,
    What is the exact meaning of Data Retrieval in task audit? Is it just data retrieved by reports?
    Thanks
    Lilach

    Hi Lilach,
    It would mean data retrieval through adhoc analysis or grids,HsGetValue Functions etc.
    You could also check Task Audit table you can see activity codes which has details below.
    ActivityID
    ActivityName
    0
    Rules Load
    1
    Rules Scan
    2
    Rules Extract
    3
    Consolidation 
    4
    Chart Logic
    5
    Translation 
    6
    Custom Logic
    7
    Allocate 
    8
    Data Load
    9
    Data Extract
    10
    Data Extract via HAL
    11
    Data Entry
    12
    Data Retrieval
    13
    Data Clear
    14
    Data Copy
    15
    Journal Entry
    16
    Journal Retrieval
    17
    Journal Posting
    18
    Journal Unposting
    You can query AppName_Task_Audit & HSV_Activity_Users tables by joining them and applying condition on Activity id from App_Task_Audit.
    Thanks
    Amith

  • SQL developer to debug a cursor data ?

    In the SQL Developer, how to debug a cursor ? In the debug mode, can I see its data what the cursor points to ?
    Thanks
    CURSOR C_EMP (DEPT_NUM_1  NUMBER)
        IS
        SELECT E.EMPLOYEE_ID, E.LAST_NAME, D.DEPARTMENT_NAME
        FROM EMPLOYEES E JOIN DEPARTMENTS D on(D.DEPARTMENT_ID = E.DEPARTMENT_ID)
        WHERE 
        D.DEPARTMENT_ID = DEPT_NUM_1;

    >
    In the SQL Developer, how to debug a cursor ? In the debug mode, can I see its data what the cursor points to ?
    CURSOR C_EMP (DEPT_NUM_1 NUMBER)
    IS
    SELECT E.EMPLOYEE_ID, E.LAST_NAME, D.DEPARTMENT_NAME
    FROM EMPLOYEES E JOIN DEPARTMENTS D on(D.DEPARTMENT_ID = E.DEPARTMENT_ID)
    WHERE
    D.DEPARTMENT_ID = DEPT_NUM_1;
    >
    A cursor is a definition and not executable code so there is nothing to 'debug'. That would be analogous to saying you want to 'debug' a query. What does that even mean?
    What do you mean by 'see its data what the cursor points to'?
    Do you mean see the query underlying the cursor? Then no - you cannot do that.
    You can fetch the data or, using dynamic sql you could describe the result set to see what column names and datatypes will be returned but there would be no information about where that data actually came from.
    This question isn't really a sql developer question but when you have one of those you should post in the sql developer forum at
    SQL Developer

  • What does the usage of CURSOR word mean in an SQL statement?

    Hey folks,
    Please check out the following query and do please explain me what does the usage of CURSOR keyword in an SQL statement mean.
    select deptno,cursor(select ename from emp a where a.deptno=b.deptno) from dept b;
    well, the output was like this,
    DEPTNO CURSOR(SELECTENAMEFR
    10 CURSOR STATEMENT : 2
    CURSOR STATEMENT : 2
    ENAME
    CLARK
    KING
    20 CURSOR STATEMENT : 2
    CURSOR STATEMENT : 2
    ENAME
    SMITH
    JONES
    SCOTT
    ADAMS
    FORD
    30 CURSOR STATEMENT : 2
    CURSOR STATEMENT : 2
    ENAME
    ALLEN
    WARD
    MARTIN
    BLAKE
    TURNER
    JAMES
    6 rows selected.
    40 CURSOR STATEMENT : 2
    CURSOR STATEMENT : 2
    no rows selected
    Your favour'll be deeply appreciated.
    Cheers,
    PCZ

    This returns a non-square result set. Each row of the result is a deptno and then a result set of the enames in that deptno.
    This can be useful when you need to send a lot of data to a client application in a single query that would otherwise contain a lot of redundancy. It tends to be a relatively unusual construct (I've only found one situation where it was appropriate in my career) and requires some relatively sophisticated understanding on both the database and client sides.
    Justin

  • How can I find out what is causing this error in SQL Developer Data Modeler

    Friends,
    I am trying to import entities into into SQL Developer Data Modeler from Oracle Designer 10.1.2.3.
    In case of need I perform these steps to perform the import:
    File --> Import --> Oracle Designer Model --> Select database connection --> Select work area --> select application system --> select one entity --> Click finish --> Import starts
    During the import process I see an alert dialog box with the message:
    There are errors in import - check Log file Clicking Ok dismisses the alert box and I see the following summary screen:
    Oracle SQL Developer Data Modeler Version: 2.0.0 Build: 584
    Oracle SQL Developer Data Modeler Import Log
    Date and Time: 2010-08-09 14:27:26
    Design Name: erdtest
    RDBMS: Oracle Database 10g
              All Statements:           32
              Imported Statements:      32
              Failed Statements:           0
              Not Recognized Statements:      0The Entity is then displayed in the Logical View within SQL Developer Data Modeler.
    Upon checking the log file I see the following entry:
    2010-08-09 13:50:34,025 [Thread-11] ERROR ODExtractionHandler - Error during import from Designer Repository
    java.lang.NullPointerException
         at oracle.dbtools.crest.imports.oracledesigner.logical.ODORelation.createArcs(Unknown Source)
         at oracle.dbtools.crest.imports.oracledesigner.logical.ODORelation.generate(Unknown Source)
         at oracle.dbtools.crest.imports.oracledesigner.ODExtractionHandler.generateDesign(Unknown Source)
         at oracle.dbtools.crest.imports.oracledesigner.ODExtractionController$Runner.run(Unknown Source)
         at java.lang.Thread.run(Thread.java:619)Can anyone shed any light on this error?
    Thanks in advance for any help you may be able to provide.

    No this helps a lot. It's not strange. Firstly, in a versioned repository you should see Private Workareas and Shared workareas, so your workarea may be in either of these. It won't be in the Global Shared Workarea, as this only for non-versioned repositories. (I like to open the RON by selecting the full Repository, that way I can see the private and shared worlareas and the configuration and containers all in the same tree.
    Now your workarea is defined by a set of rules, so when you expand the workarea in the RON, and select the object, then that's the workarea and object you'll see in the import dialog in the Data Modeler. So if you check it out and check it back in, and can't see it in the RON, then the rule is not seeing this object. (Did you refresh the workarea in the RON?) If you can't see it in the RON, you can't see it in the Data Modeler. If you're working in a versioned repository, you need to work in the specific work area, i.e V27 and this is what you need to select in the Data Modeler.
    It looks like you are selecting the wrong workarea in the Data Modeler.
    Sue

  • Cursor query retrieves records in anon pl/sql block but no data in store pr

    Hello,
    Can any one please help me,
    I am using the below query, to get the table name and constraint name
    select table_name,constraint_name
    from all_constraints
    where constraint_type in ('R','P')
    and status = 'ENABLED'
    and owner='IRIS_DATA'
    order by constraint_type desc;
    The below query retrieves data in anonymous pl/sql block and retrieve no data when i use the same cursor with the same query in procedure inside the package.
    CREATE USER CLONEDEV
    IDENTIFIED BY CLONE123
    DEFAULT TABLESPACE IRIS
    TEMPORARY TABLESPACE TEMP;
    GRANT DBA TO CLONEDEV;
    the user from which i am executing this query is granted dba role.
    My oracle version is 10.2.0.4
    Please update if you any other information.
    Please advice.
    Thanks

    >
    Roles cannot be used by pl/sql.
    >
    NOT TRUE!
    That is an oft quoted myth. There are many posts in the forum that mis-state this.
    Roles can be, and are used by PL/SQL. In fact, OP stated and demonstrated that in their question: their anonymous block worked.
    The Oracle docs provide a very clear explanation - the core restriction is for NAMED PL/SQL blocks that use DEFINER's rights (the default):
    >
    Roles Used in Named Blocks with Definer's Rights
    All roles are disabled in any named PL/SQL block (stored procedure, function, or trigger) that executes with definer's rights. Roles are not used for privilege checking and you cannot set roles within a definer's rights procedure.
    >
    The Database Security Guide has the information in the section 'How Roles Work in PL/SQL Blocks (which, of course, wouldln't be needed if they didn't work. ;) )
    http://docs.oracle.com/cd/B28359_01/network.111/b28531/authorization.htm#i1007304
    >
    The use of roles in a PL/SQL block depends on whether it is an anonymous block or a named block (stored procedure, function, or trigger), and whether it executes with definer's rights or invoker's rights.
    Roles Used in Named Blocks with Definer's Rights
    All roles are disabled in any named PL/SQL block (stored procedure, function, or trigger) that executes with definer's rights. Roles are not used for privilege checking and you cannot set roles within a definer's rights procedure.
    The SESSION_ROLES view shows all roles that are currently enabled. If a named PL/SQL block that executes with definer's rights queries SESSION_ROLES, then the query does not return any rows.
    See Also:
    Oracle Database Reference
    Roles Used in Named Blocks with Invoker's Rights and Anonymous PL/SQL Blocks
    Named PL/SQL blocks that execute with invoker's rights and anonymous PL/SQL blocks are executed based on privileges granted through enabled roles. Current roles are used for privilege checking within an invoker's rights PL/SQL block. You can use dynamic SQL to set a role in the session.

  • What is the format for the DATE date type in SQL

    I just wanted to know what the format is for the Date type in Oracle 10g SQL. I need to be able to specify to the second, but not further than that. Thanks in advance.

    Hi,
    DATE format is configurable and can be easily changed. I suggest the function TO_DATE. For example:
    to_date('10-20-2008', 'MM-DD-YYYY')
    to_date('20/12/08 20:15', 'dd/mm/yyyy hh24:mi')Regards,
    Miguel

  • Problem with pl/sql table data type

    hai friends,
    i have one procedure it has some in parameters and one out parameter which fetches values to front end.
    But the out parameter will be pl/sql table data type.
    if it is ref cursor
    then i declared as
    var x refcursor;
    exec procedure_name(1,:x);
    it is ok.
    but for pl/sql table data type is out parameter then what i will do in the prompt .
    give me syntax and clarify my doubt.
    advanced thanks...
    madhava

    The SQL*Plus VARIABLE statement does not support user-defined types, hence it cannot support nested tables. It can support cursors because they can be weakly typed (so we can use the one SQL*Plus VAR to hold any shape of resultset). Nested tables are strongly typed; SQL*Plus is a relatively stupid interface and cannot be expected to understand UDT.
    So, it you want to use nested tables as output you'll need to code a wrapping procedure that can understand your nested table type and handle it accordingly.
    Sorry.
    Cheers, APC

  • SQL Developer Data Modeler: Logical model, unable to create arc

    I have a logical model in SQL Developer Data Modeler. Entity A has two 'incoming' foreign key relations to entities B and C. Both releations have the same optionality and cardinality. I want to place both relations in an arc. I see the buttons in my toolbar menu, but they are not enabled. Even when I select both relations it is not.
    Bringing the logical model to the relational model creates foreign keys. These foreign keys can be selected and when I do that the 'Create new arc' button in the relational model toolbar is enabled.
    What am I missing in the logical model ...? The documentation doesn't give a clue.
    Erik

    Thanks Sue, that worked. It is different from the way the ERD in Designer worked. Is there any reasoning behind it. As far as I know you should only put into an arc two or more relationships pointing to the same entity with the same optionality and the same cardinality. Selecting the entity with the relationships can only help in identifying at which relationship end the arc should be created but that is obvious.
    And asking the question is answering it: it will help in adding an arc to two or more entities originating and ending at the same entity (and same cardinality and optionality).
    Is that the reasoning behind it?
    Regards
    Erik

  • "File Browse" type item: at what point the fields mime_type, file_name, last_update_date get their values?

    Hi there,
    I have an issue with "File Browse" type item, my question is: at what point the fields mime_type, file_name, last_update_date get their values? I want to add them to the collection but if I try to do it in Process "On Submit - After Computations and Validations" the values in those fields are still null (that's for the insert, and for the update they have old values). Also I don't seem to be able to add blob to the collection ("File Browse" type item (which has the source as blob database column) can't be assigned as parameter p_blob001 of APEX_COLLECTION.ADD_MEMBER, error is: PLS-00306: wrong number or types of arguments in call to 'ADD_MEMBER').
    What I am trying to do is this: I have parent table (TABLE1), and child table (TABLE2).  In TABLE2 I store documents. I have a main page that is form on TABLE1, and on it I have button "Add documents" that opens popup window with form on TABLE2. On submit of popup page I want to avoid inserting document into TABLE2, instead I want to send data to the main page (using collection) and insert in only once the main page is submitted.
    I would appreciate any advice on this...
    Thanks,
    Tanya

    Hello Tanya,
    Can you post your PL/SQL code which you are trying?
    Regards,
    Hari

  • Size of PL\SQL Cursor block

    I want to know the internal working of the PL\SQL Cursor. Lets start with a scenario.
         1) I have 3 table with 20 million records each.
         2) I am making a join out of them on a certain condition which gives me 60 million records, now I am putting them in a cursor.
    Questions :
         i) Can the Oracle PL\SQL Cursor is capable of holding that much records or there is any limit for it ?
         ii) If there is a limit for such block, then where can I find it and how can I set it ?
         3) If there is no such limit, then running such a query and holding all the data in the Cursor Block would degrade the performance. How Oracle handles such situations, It might be possible that Oracle only have the indexes of those records in the Cursor, but still it would be to time consuming to index 3 different table
    Kindly Inform me about this issue ....
    Thanks & regards
    - Nabil Shams

    1. Oracle PL/SQL cursor is just a handle to shared SQL area in Oracle SGA and as such does not consume an awful lot of memory (I would dare to say it consumes peanuts). What is more important it does not depend on number of rows you are going to fetch via cursor
    2. Therefore the answer to question 2 is NO
    3. Running the query could and would affect you in most unpleasnat ways if you tried to store results of the query in your process memory (PGA/UGA), or more specifically if you created PL/SQL indexed by table and tried to pack all 60000000 rows into it.
    If you fetch rows one by one, process the row, throw it away - then no problem... but what do you need to do it for?

  • Need a column based off a PL/SQL cursor, how to do this?

    Oracle9i Enterprise Edition Release 9.2.0.8.0 - 64bit Production
    With the Real Application Testing option
    JServer Release 9.2.0.8.0 - Production
    Report Builder 10.1.2.2.0
    ORACLE Server Release 10.1.0.5.0
    Oracle Procedure Builder 10.1.2.2.0
    Oracle ORACLE PL/SQL V10.1.0.5.0 - Production
    Oracle CORE     10.1.0.5.0     Production
    Hi,
    I am trying to create a fairly basic report but in the initial data model query, I have one column that is built based off of a PL/SQL cursor. How do I add this into my report? I have tried to create a function under Program Units and reference the function in my select statement for the data model however it keeps saying it is an invalid identifier, and I assume its looking in our database and not the function I created inside the report.
    This is what it looks like:
    SELECT   a.id,
             b.name,
             F_GET_GENERIC_NAME(b.code) "Generic Name",
             c.strength...and the function i want it to use in my report:
    FUNCTION F_GET_GENERIC_NAME (in_code varchar2) RETURN VARCHAR2 IS
    BEGIN
    DECLARE
    generic_name table.column%TYPE;
    CURSOR cs_get_generic_name IS
    SELECT /*+ USE_HASH (AR) */ desc
    FROM  a_ref AR, ai_ic AC    
    WHERE AC.code = in_code
    AND   AC.code = AR.code
    ORDER BY sort_nbr;
    BEGIN
           generic_name := '';
              FOR v_get_generic_name IN cs_get_generic_name
              LOOP
                 generic_name := generic_name || RTRIM(v_get_generic_name.desc)
                                     || '/' || ' ';
              END LOOP;
              generic_name := SUBSTR(generic_name, 1,(NVL(LENGTH(generic_name), 0) - 2));
    RETURN generic_name;
    END;
    END;
        Any suggestions? Should I just create the function in the database?

    I am trying to do it through a formula column but am encountering the following problem.
    I've created a formula column in my data model where I want the column to be (inside the group).
    Inside the PL/SQL for the column I reference the function that I had previously created with the following:
    function CF_Generic_nameFormula return Char is
    begin
         F_GET_GENERIC_NAME(:code);
    end;However it won't let me compile, giving the error that "F_GET_GENERIC_NAME is not a procedure or is undefined." Is there something else that I am missing?
    EDIT: Nevermind, i missed something simple like putting a return in front of the function call.
    Edited by: a small rabbit on Nov 3, 2009 10:43 AM
    Edited by: a small rabbit on Nov 3, 2009 10:44 AM

  • Plsql procedure with sql query data

    plsql newbie(learning sql): please excuse for asking a basic plsql question
    Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Prod on windows server 2003
    PL/SQL Release 10.2.0.1.0 - Production
    "CORE     10.2.0.1.0     Production"
    TNS for 32-bit Windows: Version 10.2.0.1.0 - Production
    NLSRTL Version 10.2.0.1.0 - Production
    CREATE OR REPLACE PROCEDURE p_check
    AS
    v1 NUMBER(20,0);
    BEGIN
    SELECT count(*) INTO v1
    FROM ad_reg;
    dbms_output.put_line(v1);
    end;
    inserts the number of rows in the table ad_reg into v1
    and displays v1 by using dbms_output
    i want my procedure to return table data
    CREATE TABLE "TEST_SH"
    "TEXT_ID" NUMBER,
    "TEXT" CLOB
    Insert into TEST_SH (TEXT_ID) values (1);
    Insert into TEST_SH (TEXT_ID) values (2);
    Insert into TEST_SH (TEXT_ID) values (3);
    Insert into TEST_SH (TEXT_ID) values (4);
    there will be a way with which i can see all the data(select * from test_sh) as the output of the plsql procedure
    1.please tel me what to google
    2.please guide me with a site or tell what is actually used and help me
    3.or post an example here
    thank you much
    Edited by: 946207 on Nov 20, 2012 2:05 PM

    946207 wrote:
    there will be a way with which i can see all the data(select * from test_sh) as the output of the plsql procedurePL/SQL is
    a) nothing at all like T-SQL
    b) two different languages, PL (Programming Logic) and SQL
    SQL is used to access data in the Oracle database. So select * from test_sh is the correct statement to access the data.
    The client can make execute this SQL statement itself. And view the data. However, this requires the client to understand the SQL language, and the database model. Which is typically not that you want. You want to abstract that from the client.
    Why would you want the client to deal with the complexities of the SQL language? Or deal with the complexities of your database model in Oracle?
    So instead of the client doing SQL, PL/SQL can do it on behalf of the client. The client uses PL/SQL as an API - in a similar fashion as the client would use any other API.
    The PL/SQL code determines the SQL statement - and returns a SQL cursor handle to the client. The client then simply uses the SQL cursor that the PL/SQL code created for it (and the client needs to close the SQL cursor when done, so as to not cause server resource leakage).
    E.g. (using PL/SQL as an API to abstract SQL and the data model)
    create or replace procedure TestProc( c out sys_refcursor ) is
    begin
      open c for select * from test_sh order by 1;
    end;The client then calls TestProc and gets a reference cursor handle. E.g. (the client call to Oracle):
    begin
      TestProc( c => :1 );
    end;The client needs to supply a host (client language) bind variable of type cursor, as value for bind variable +:1+.
    This is the correct way to deal with PL/SQL and cursors from a client GUI/language perspective. Any other method, like using PL/SQL to cache SQL data as a collection, is usually very wrong.

Maybe you are looking for

  • Is latest Java SE 1.4.2_42 available to download? Need Patch!

    Dear All, Because the latest security issue (Feb/2013) also affect 1.4.2, it need to be patched. [http://www.oracle.com/technetwork/topics/security/javacpufeb2013update-1905892.html] And there is also a release note for 1.4.2_42 that fix that securit

  • How to import pics from iPhoto?

    I'm trying to import my existing photos to Lightroom from my computer, but I don't see iPhoto (where all of my photos are) as an option to import from.  How can I import those existing photos?  Thank you!

  • Does the original Xserve Raid (2003) support 750Gb drives.

    Is the latest firmware compatible with the original Xserve Raid, thus supporting 750Gb discs? Thanks in advance.

  • N97 Mini questions

    Hi Ive just got a few questions is there a way to turn the vibration off for when you lock the keys using the slide button but keep vibration fr when receiving alerts? is there a way to increase the vibration so its more noticable?  Is there a way to

  • 5dmk3 video capture info not showing in LR4?

    Used to have a canon 7d, which created a .thm file that held all the capture data info for the video, but now with the 5dmk3 it seems like the thm file is embedded into the video file, but now in lightroom the capture data does not show up any longer