How Oracle's stored procedures return result sets

Source: Internet
Author: User
Procedure returns the recordset:
CREATE OR REPLACE PACKAGE pkg_test
As
TYPE Myrctype is REF CURSOR;

PROCEDURE Get (p_id number, p_rc out myrctype);
End Pkg_test;
/

CREATE OR REPLACE PACKAGE body pkg_test
As
PROCEDURE Get (p_id number, p_rc out Myrctype)
Is
Sqlstr VARCHAR2 (500);
BEGIN
IF p_id = 0 THEN
OPEN P_RC for
SELECT ID, NAME, sex, address, postcode, birthday
from student;
ELSE
SQLSTR: =
' Select Id,name,sex,address,postcode,birthday
From student where id=:w_id ';
OPEN P_RC for sqlstr USING p_id;
End IF;
End get;
End Pkg_test;
/

function returns the recordset:
Create packages and packages and functions with REF CURSOR definitions:
CREATE OR REPLACE
Package Pkg_test AS
/* Define REF CURSOR type
No return type, for weak type, allow dynamic SQL query,
Otherwise, it is strongly typed and cannot use dynamic SQL queries;
*/
Type myrctype is REF CURSOR;

--Function declaration
function get (IntID number) return myrctype;
End Pkg_test;
/

CREATE OR REPLACE
Package Body Pkg_test AS
--Function body
function get (IntID number) return Myrctype is
RC Myrctype; --Define REF CURSOR variable
Sqlstr VARCHAR2 (500);
Begin
If Intid=0 Then
--static testing, directly returning results directly with SELECT statement
Open RC for select id,name,sex,address,postcode,birthday from student;
Else
--Dynamic SQL assignment, using: w_id to declare that the variable is obtained externally
SQLSTR: = ' Select Id,name,sex,address,postcode,birthday from student where id=:w_id ';
--Dynamic test, return the result with SQLSTR string, pass parameter with using keyword
Open RC for sqlstr using IntID;
End If;

return RC;
End get;

End Pkg_test;
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.