Posted By

geekzspot on 01/10/13


Tagged


Versions (?)

Oracle Cursor examples


 / Published in: PL/SQL
 

Write to a cursor, with parameters, read, print

  1. -- Write a cursor:
  2.  
  3. FUNCTION write_cursor RETURN sys_refcursor
  4. return_cursor sys_refcursor;
  5. OPEN return_cursor FOR
  6. SELECT * FROM dual; -- Your SQL here!
  7. RETURN return_cursor;
  8. END write_cursor;
  9.  
  10.  
  11. -- Write cursor with parameter:
  12.  
  13. FUNCTION write_cursor(filter_ parameter IN dual.dummy%TYPE DEFAULT 'X') RETURN sys_refcursor
  14. return_cursor sys_refcursor;
  15. OPEN return_cursor FOR
  16. SELECT * FROM dual WHERE dummy = filter_ parameter; -- Your SQL here!
  17. RETURN return_cursor;
  18. END write_cursor;
  19.  
  20.  
  21. -- Read a Cursor:
  22.  
  23. PROCEDURE read_cursor(cursor_in IN sys_refcursor)
  24. l_rec dual%ROWTYPE;
  25. FETCH cursor_in INTO l_rec;
  26. EXIT WHEN cursor%notfound;
  27. END read_cursor;
  28.  
  29.  
  30. -- Testing / Printing:
  31.  
  32. -- Quick and dirty method:
  33.  
  34. SELECT get_employees() FROM dual;
  35.  
  36.  
  37. -- Nice and neat method:
  38.  
  39. SET timing ON
  40. var ref_cursor refcursor;
  41. my_package.my_proc(:ref_cursor);
  42. /
  43. print ref_cursor;

Report this snippet  

You need to login to post a comment.