#Example
Step 1 : create a sample table to execute procedure.
Step 2 : create procedure returning ref cursor in out parameter.
Step 3 : Execute stored procedure.
#Step 1 :Create Sample table and insert few values.
Step 1 : create a sample table to execute procedure.
Step 2 : create procedure returning ref cursor in out parameter.
Step 3 : Execute stored procedure.
#Step 1 :Create Sample table and insert few values.
SQL> CREATE TABLE LoginInfo
(
LogID number,
LogDateTime date,
UserName varchar2(10)
);
SQL> INSERT INTO LoginInfo VALUES(1,TO_DATE('01/01/2015 20:10:40','dd/mm/yyyy hh24:mi:ss'),'USER_A');
SQL> INSERT INTO LoginInfo VALUES(2,TO_DATE('02/01/2015 09:15:20','dd/mm/yyyy hh24:mi:ss'),'USER_A');
SQL> INSERT INTO LoginInfo VALUES(3,TO_DATE('02/01/2015 10:09:00','dd/mm/yyyy hh24:mi:ss'),'USER_B');
SQL> INSERT INTO LoginInfo VALUES(4,TO_DATE('02/01/2015 12:10:31','dd/mm/yyyy hh24:mi:ss'),'USER_A');
SQL> INSERT INTO LoginInfo VALUES(5,TO_DATE('03/01/2015 11:30:05','dd/mm/yyyy hh24:mi:ss'),'USER_A');
SQL> INSERT INTO LoginInfo VALUES(6,TO_DATE('03/01/2015 12:40:07','dd/mm/yyyy hh24:mi:ss'),'USER_B');
NOTE : TO_DATE function is used to insert time values with date.
NOTE : TO_DATE function is used to insert time values with date.
SQL>SELECT * FROM loginInfo;
LOGID
|
LOGDATETIME
|
USERNAME
|
1
|
01-01-2015 20:10:40
|
USER_A
|
2
|
02-01-2015 09:15:20
|
USER_A
|
3
|
02-01-2015 10:09:00
|
USER_B
|
4
|
02-01-2015 12:10:31
|
USER_A
|
5
|
03-01-2015 11:30:05
|
USER_A
|
6
|
03-01-2015 12:40:07
|
USER_B
|
#Step 2 :Create Procedue returning Ref Cursor.
CREATE OR REPLACE PROCEDURE Proc_forCursor(
p_cursor OUT SYS_REFCURSOR, p_user IN LoginInfo.UserName%TYPE)
IS
BEGIN
OPEN p_cursor FOR
SELECT * FROM LoginInfo WHERE UserName = p_user;
END;
/
#Step 3 : Execute stored procedure.
DECLARE
v_userCursor SYS_REFCURSOR;
v_table LoginInfo%ROWTYPE;
BEGIN
proc_forCursor(v_userCursor,'USER_A');
LOOP
FETCH v_userCursor INTO v_table;
EXIT WHEN v_userCursor%NOTFOUND;
dbms_output.put_line(v_table.LogID ||' '|| v_table.USERNAME);
END LOOP;
CLOSE v_userCursor;
END;
/
Result :
1 USER_A
2 USER_A
4 USER_A
5 USER_A
Result :
1 USER_A
2 USER_A
4 USER_A
5 USER_A
No comments:
Post a Comment