Can we declare a cursor in the Specs of package?

Dear friends
I can declare a cursor in the package Specs so that I can call this slider and use its data in certain procedures and functions of the package. Otherwise I have to write this slider for each subprogramme of a package which I'm not a clever way to accomplish the task.

Hello

Here is a short example with all the way down. Perhaps the concept becomes more clear with this:

first of all, if you do not have the table emp, here the DDL for this example.
Attention, only works for German customers because the names of the months, sorry for that.

CREATE TABLE EMP
(EMPNO NUMBER(4) NOT NULL,
ENAME VARCHAR2(10),
JOB VARCHAR2(9),
MGR NUMBER(4),
HIREDATE DATE,
SAL NUMBER(7, 2),
COMM NUMBER(7, 2),
DEPTNO NUMBER(2));
set echo on

INSERT INTO EMP VALUES
(7369, 'SMITH', 'CLERK', 7902,
TO_DATE('17-DEZ-1980', 'DD-MON-YYYY'), 800, NULL, 20);
INSERT INTO EMP VALUES
(7499, 'ALLEN', 'SALESMAN', 7698,
TO_DATE('20-FEB-1981', 'DD-MON-YYYY'), 1600, 300, 30);
INSERT INTO EMP VALUES
(7521, 'WARD', 'SALESMAN', 7698,
TO_DATE('22-FEB-1981', 'DD-MON-YYYY'), 1250, 500, 30);
INSERT INTO EMP VALUES
(7566, 'JONES', 'MANAGER', 7839,
TO_DATE('2-APR-1981', 'DD-MON-YYYY'), 2975, NULL, 20);
INSERT INTO EMP VALUES
(7654, 'MARTIN', 'SALESMAN', 7698,
TO_DATE('28-SEP-1981', 'DD-MON-YYYY'), 1250, 1400, 30);
INSERT INTO EMP VALUES
(7698, 'BLAKE', 'MANAGER', 7839,
TO_DATE('1-MAI-1981', 'DD-MON-YYYY'), 2850, NULL, 30);
INSERT INTO EMP VALUES
(7782, 'CLARK', 'MANAGER', 7839,
TO_DATE('9-JUN-1981', 'DD-MON-YYYY'), 2450, NULL, 10);
INSERT INTO EMP VALUES
(7788, 'SCOTT', 'ANALYST', 7566,
TO_DATE('09-DEZ-1982', 'DD-MON-YYYY'), 3000, NULL, 20);
INSERT INTO EMP VALUES
(7839, 'KING', 'PRESIDENT', NULL,
TO_DATE('17-NOV-1981', 'DD-MON-YYYY'), 5000, NULL, 10);
INSERT INTO EMP VALUES
(7844, 'TURNER', 'SALESMAN', 7698,
TO_DATE('8-SEP-1981', 'DD-MON-YYYY'), 1500, 0, 30);
INSERT INTO EMP VALUES
(7876, 'ADAMS', 'CLERK', 7788,
TO_DATE('12-JAN-1983', 'DD-MON-YYYY'), 1100, NULL, 20);
INSERT INTO EMP VALUES
(7900, 'JAMES', 'CLERK', 7698,
TO_DATE('3-DEZ-1981', 'DD-MON-YYYY'), 950, NULL, 30);
INSERT INTO EMP VALUES
(7902, 'FORD', 'ANALYST', 7566,
TO_DATE('3-DEZ-1981', 'DD-MON-YYYY'), 3000, NULL, 20);
INSERT INTO EMP VALUES
(7934, 'MILLER', 'CLERK', 7782,
TO_DATE('23-JAN-1982', 'DD-MON-YYYY'), 1300, NULL, 10);

2. package Spec:

create or replace
package test_cursor as 

  --Type for the returncode of the function
  TYPE typ_emp IS TABLE OF emp%rowtype;

  --Array for fetching, of course also possible in the body
  t_emp typ_emp;

  --function wich returns the array from fetching the cursor
  function get_emp return typ_emp;

  --function for manupilation data retrieved by the function
  PROCEDURE man_emp;

end test_cursor;

3. package body

create or replace
package body test_cursor as

  FUNCTION get_emp RETURN typ_emp AS

  cursor c_emp is select * from emp;

  BEGIN
    open c_emp;

    fetch c_emp BULK COLLECT INTO t_emp;

    CLOSE c_emp;

    --t_emp returns the whole table set from emp
    return t_emp;
  end get_emp;

  PROCEDURE man_emp AS
  --just for not confusing names, is the same as t_emp of course
  v_emp_array typ_emp;

  BEGIN
    --call the function and retrieve the whole data set
    v_emp_array := get_emp;

    --now manipulate the data, in this case just write the names to the calling client
    FOR rec IN v_emp_array.FIRST .. v_emp_array.LAST
    loop
      dbms_output.put_line(v_emp_array(rec).ename);
    end loop;

  end man_emp;

end test_cursor;

4. the procedure call

SET serveroutput ON
exec test_cursor.man_emp;

5. and here's the result:

anonymer Block abgeschlossen
SMITH
ALLEN
WARD
JONES
MARTIN
BLAKE
CLARK
SCOTT
KING
TURNER
ADAMS
JAMES
FORD
MILLER

Please be aware, it's just for demonstration purposes, of course, it makes no sense to display names in this way. But how to call a function return tables with sets of data recovery of the sliders is shown here.

HTH

Joerg

Tags: Database

Similar Questions

  • Declare the cursor within the begin/end block

    Hi all
    Can we declare a cursor inside a begin/end block. If we can, please let me know how. I want to declare a cursor where it will return value based on the settings of the user. User will enter in as username(for example), we have to recover the ID, the user name and we must move to cursor. To do this, we wrote a query to fetch id in a variable in the other block of start/end and we pass this variable in the cursor. Is this possible.



    Thank you and best regards,
    Mahesh

    In general, yes you can, you simply nest another block of execution inside of your...

    begin
      declare
        cursor x is select dummy from dual;
      begin
        ...
      end;
    end;
    /
    
  • Can we reset a slider at the top

    Hello

    Can we reset a cursor to the first row after the last row?

    I want to do something like this

    DECLARE
    News SYS_CURSOR;
    queryContext DBMS_XMLGEN.ctxHandle;
    result CLOB.

    BEGIN
    Heart OPEN FOR seq.nextval tid, testcol1 FROM testtable;
    queryContext: = DBMS_XMLGEN.newContext (cur);
    result: = DBMS_XMLGEN.getXML (queryContext);
    DBMS_XMLGEN.closeContext (queryContext);

    -The cursor is at the end of the Recordset, I need to go back to the first row and insert the data into a table

    RESET the heart; -This command not exist, just an idea
    LOOP
    -Insert each row in the table
    END LOOP;
    CLOSE cur;
    END;

    I was able to interview with select same statement again if I didn't have generated a value in a sequence.

    Omer

    A friend of yours says:
    TWG does mean?

    Global temporary table

  • Hello friends, I want to know what the data type can be declared in pl/sql, but cannot be declared in sql?

    Hello friends, I want to know what the data type can be declared in pl/sql, but cannot be declared in sql?

    Hello friends, I want to know what the data type can be declared in pl/sql, but cannot be declared in sql?

    That question is ambiguous: there is NOT a SINGLE type of data that can be declared in pl/sql, but not in sql.

    The Oracle documentaton is your friend.

    The doc of the SQL language treats the SQL data types.

    http://docs.Oracle.com/CD/B28359_01/server.111/b28318/datatype.htm

    The doc PL/SQL does PL/SQL data types. Because PL/SQL is a language of proceedings (and sql is NOT) there are several types of data, as shown in this doc, PL/SQL-specific:

    PL/SQL scalar data types are:

    • SQL data types
    • BOOLEAN
    • PLS_INTEGER
    • BINARY_INTEGER
    • REFCURSORexplained in "Cursor Variables"
    • User-defined subtypes

    ALWAYS start with the Oracle documentation for these fundamental questions.

    Several times, as with this question, the question is too general to give a simple answer.

  • Declare a cursor that can be played back in array of type

    Hello

    I have a problem - how to declare a cursor that can be played back in array of type.

    
    create or replace type testType as object  
    (  
      dokid number(10),          
      name varchar2(20)  
    );  
      
    CREATE OR REPLACE TYPE testTableType AS TABLE OF testType;  
      
    procedure test(  
         author in number      
    )   
    is  
      tempTable testTableType := testTableType();  
      --cursor type should be changed
      curs out sys_refcursor;
    begin  
         --open cursor  
         open curs for  
         select documentID, documentName from documents;
         
         --loop that fetches data
         loop
              FETCH curs 
              BULK COLLECT INTO tempTable LIMIT 100;
              --need bulk collect as table has too many records if used without bulk
              EXIT WHEN curs %NOTFOUND;
              --do some work with temp table
              DBMS_OUTPUT.put_line(tempTable .count || ' rows');
         end loop;
         close curs;
           
          
    end test;
    

    First question is why want you to loop through data of cursor? Do it in SQL directly, it is the fastest way to do it.

    What about fixing your code, you can go. Code in RED are which I changed.

    test procedure
    (
    the author number
    )
    is
    tempTable testTableType: = testTableType();

    curs sys_refcursor;
    Start
    Open the curs
    for
    Select testtype (documentID, documentName) in documents;

    loop
    FETCH Curs
    bulk collect into temptable limit 100;
    dbms_output.put_line (TEMPTABLE. Count | "lines");
    When exit curs % notfound;
    end loop;

    close the curs;
    end test;
    /

  • Windows 10 most recent overview 10041, Firefox Nightly x 64 Technology, can run, can't access settings, cannot place the cursor in the URL box

    Have Windows over recent Technical Preview build 10041, Firefox Nightly x 64 39.0a1 10. Firefox will be launched, but when the browser seems no entry screen as possible. Cannot access settings, cannot put the cursor in the URL box. Tried unsuccessfully compatibility modes. I can not able to use this browser on Thursday, March 26, but it worked without problem Wednesday, March 25. My speculation would be that among the latest two updates nightly broke it. The displayed issue of Chrome, like Firefox does not.

    Hello, this seems to be a known bug that is being developed (bug #1147953).
    Please also note that windows 10 is in it's pre-release preview State and there are still changes the basic platform, it will not be fully supported by firefox and things are required to be wrong on occasion.

  • Not fully functional mouse with MacBook Pro 2010. I can scroll up and down, but can not move the cursor.  The mouse works with my new Macbook 12. No mouse problem but Macbook Pro?

    Not fully functional mouse with MacBook Pro 2010. I can scroll up and down, but can not move the cursor.  The mouse works with my new Macbook 12. No mouse problem but Macbook Pro?

    What specific mouse?

  • Hit the tab key changes the withdrawal. How can I do tab take the cursor to the next tab stop?

    Currently in the latest version of Pages, the tab key changes the line indent. How can I take the cursor to the next tab stop? The document was created in Word format. I work with Yosemite 10.10.5 on a MacBook Pro with 8G of RAM and hard disk of 1 t.

    The removal of default tab in v5.6.2 Pages is 0.5. You have formally the additional tab stops in Text Format: Panel layout, or with your ruler displayed or clicked on it to set the tab indents?

    Pages is not a clone of the word and the Act to translate automatically the Word document to the internal .pages format when opened by Pages - may or may not honor prior model and Word document settings.

  • I can't bid on firefox or explorer. I place the cursor in the quote box, and it will not record the numbers. Can someone please help?

    I can't bid on firefox or explorer. I place the cursor in the quote box, and it will not only record the numbers. Can anyone help please thanks

    Problem solved. My husband came and noticed that the number lock has been deactivated. I can't thank you enough for your help. Hope my solution helps someone else with the same problem.

  • Hi, while writing an email online using Firefox 3.6.3, I was using one of the "CTRL + left arrow or right" to move the cursor on the text. I must have pressed some other key because the screen has become huge - and I can't scroll vertically or horizont

    Hi, while writing an email online using Firefox 3.6.3, I was using one of the "CTRL + left arrow or right" to move the cursor on the text. I must have pressed some other key because the screen has become huge - and I can't scroll vertically or horizontally, which makes the pages of Mail of Hotmail impossible. I have connected since using the Explorer MSFT and it works very well. This problem occurs only on the page of Mail of Hotmail Live not on the home page or other.
    Could you please let me know if there is a problem with Firefox, or if you have any advice on how to restore the appearance of Hotmail in Firefox?

    This has happened

    Just once or twice

    == I'm compiling an email in Hotmail Live

    See this:
    https://support.Mozilla.com/en-us/KB/page+zoom

  • How can I get a translator of the cursor

    Where and how can I get a cursor which translates what is postponed

    Hello

    The Bing toolbar has a button "translate this page".
    www.bingtoolbar.com
    The Google toolbar has a context menu with a choice being to translate highlighted text.
    www.ToolBar.google.com

    Hope this helps,
    Don

  • I can not navigate via hotmail. I.E. I can bring up hotmail, but when I move the cursor on the junk mail and left click nothing happens.

    I can't eliminate or select the messages in the Inbox and display them. Again, I put the cursor on the message and left click and nothing happens.

    Hi JohnHudson22,

    1. don't you make changes on the computer before the show?

    2 is the issue limited to hotmail Web site only?

    Perform an optimization of Internet Explorer on the computer of the article.

    How to optimize Internet Explorer

    http://support.Microsoft.com/kb/936213/ro

    Note: Reset the Internet Explorer settings can reset security settings or privacy settings that you have added to the list of Trusted Sites. Reset the Internet Explorer settings can also reset parental control settings. We recommend that you note these sites before you use the reset Internet Explorer settings.

    You can also post your query in Windows Live Solution Center for better support.

    http://www.windowslivehelp.com/product.aspx?ProductID=1

  • I can't boot into windows xp. The windows logo comes on but then a black screen blank (you can see and move the cursor with the mouse however) is displayed.

    I can't boot into windows xp. The windows logo comes on but then a black screen blank (you can see and move the cursor with the mouse however) is displayed.

    anesvet,
    I'm going to sound like I'm repeating myself tonight... (see previous 4 posts...)  but try this:

    When you start start pressing F8 until you get the Windows startup options.  Then try "Last known good configuration" see if that helps.  If this does not work, try to F * then try to go to Safe Mode.  If those who do not help, I would start with your XP CD to recovery mode and run "chkdsk/r" then "sfc/scannow".  QQ learn manage!

  • My toolbar disappeared and only comes at the time when I hold the cursor at the top of the screen. How can I get it back?

    My toolbar that since always on on my Windows screen simply ceased to appear and it comes at the time where I keep the cursor at the top of the screen.  How can I get it back?

    You have clicked on full screen. Hold the cursor at the top and drag it to the right along the toolbar and click on the restore button which is the button (enlarge/restore down) between narrow it down and close.

  • Inadvertently, I moved my cursor using the touchpad and it decreased my policy and I can't go back to a larger size. my daughter thinks it has something to do with scrolling

    Inadvertently, I moved my cursor using the touchpad and it decreased my policy and I can't get it for a larger size. my daughter thinks it has something to do with scrolling

    You did not mention this program what happens to, but even in the case of the office, there are standard methods to adjust the zoom level with the keyboard.

    Press Ctrl and more (e, g +) together to increase the zoom level and Ctrl and less (for example) - all to reduce. The keys more or less can be those at the top of the classic (for example to the left of the back) keys or the numeric keypad if you have one.
    Alternatively, you can hold down the CtrI down while turning the scroll of the mouse wheel to adjust the zoom level of the window that the cursor of the mouse is on.
    Many programs have a slider or box to adjust the zoom level. It is usually at the bottom right of the application window. Similar zoom adjustments may be able to make the menu display, Zoom the program.

Maybe you are looking for

  • Audio driver HP pavilion dv5000

    I install windows 7 on my laptop and sound doesent work more. Is there a working driver or what shout I do? I tried several drivers but none of them work.

  • Failed assertion

    I tried to download new games to my game on big fish games Manager, and he said Microsoft Virtual C ++ runtime library. JIT must be enabled. I don't understand what to do. the file it says is graphics\win\d3d9app whatever that means. It's so confusin

  • Photo Gallery stops responding / freeze!

    My computer is 2 years old. I've never had no problem uploading photos using Gallery photos Windows... until this week. Library message is "not responding".Just freezes. No technical info given.I can't move photos or transfer them to an internal driv

  • Module network extended to 7507.

    Hey, people! Please help me! What wan module exist for 7507 with 4 or more interfaces series? Is it necessary use of VIP (versatile interface processor) or not? Thank you Renato

  • Possible to display TNS_ADMIN in PLSQL?

    About the title.  Running on 11 g in Solaris.I want to see the TNS_ADMIN value used the shell bash current which runs my sqlplus.  And before you say just echo $TNS_ADMIN, a 3rd party app is sending the bash + sqlplus command, so let's just say under