Returning Rows Call : Select « Select Query « Oracle PL / SQL






Returning Rows Call

    
SQL>
SQL> CREATE TABLE emp (
  2     empID INT NOT NULL PRIMARY KEY,
  3     Name      VARCHAR(50) NOT NULL);

Table created.

SQL> INSERT INTO emp (empID,Name) VALUES (1,'Tom');

1 row created.

SQL> INSERT INTO emp (empID,Name) VALUES (2,'Jack');

1 row created.

SQL> INSERT INTO emp (empID,Name) VALUES (3,'Mary');

1 row created.

SQL> INSERT INTO emp (empID,Name) VALUES (4,'Bill');

1 row created.

SQL> INSERT INTO emp (empID,Name) VALUES (5,'Cat');

1 row created.

SQL> INSERT INTO emp (empID,Name) VALUES (6,'Victor');

1 row created.

SQL>
SQL> CREATE OR REPLACE PACKAGE emp_pkg
  2  AS
  3  TYPE studCur IS REF CURSOR;
  4  PROCEDURE Getemp(o_StudCur OUT studCur);
  5  END emp_pkg;
  6  /

Package created.

SQL>
SQL> CREATE OR REPLACE PACKAGE BODY emp_pkg
  2  AS
  3     PROCEDURE Getemp(o_StudCur OUT studCur)
  4     IS
  5     BEGIN
  6        OPEN o_StudCur FOR
  7           SELECT empID, Name FROM emp;
  8     END Getemp;
  9  END emp_pkg;
 10  /

Package body created.

SQL> SET SERVEROUT ON
SQL> DECLARE
  2     TYPE studCurType IS REF CURSOR;
  3     mycur studCurType;
  4     studrow emp%ROWTYPE;
  5  BEGIN
  6     emp_pkg.Getemp(mycur);
  7     FETCH mycur INTO studrow;
  8     WHILE mycur%FOUND
  9     LOOP
 10        dbms_output.put_line(studrow.empID || '   ' ||
 11                             studrow.Name);
 12        FETCH mycur INTO studrow;
 13     END LOOP;
 14  END;
 15  /
1   Tom
2   Jack
3   Mary
4   Bill
5   Cat
6   Victor

PL/SQL procedure successfully completed.

SQL>
SQL> drop table emp;

Table dropped.

   
    
    
    
  








Related examples in the same category

1.The following code provides a breakdown of the basic SELECT statement on the Oracle platform:
2.Use Arithmetic operators with literal values to derive values: add 5 to salary
3.SELECT statement uses the not equal (< >) operator in the WHERE clause
4.Using the > operator
5.UPPER(SUBSTR(first_name, 2, 8)): Combining Functions
6.Plus in select statement
7.Minus in select
8.Math calculation in select
9.CONCATENATING TEXT
10.Returning All Columns
11.Returning Multiple Columns
12.Employees from new york who have gifts
13.Find all employees who are younger than employee whose id is 9999
14.Selecting Categories That Contain Product
15.Selecting Products That Belong to Category
16.Selecting Products That Belong to Department with Join
17.Using select statement and char function to create insert statement