Stored Procedure of the returned result set in Oracle

Source: Internet
Author: User

The stored procedure for returning a result set in Oracle, Unlike SQL Server, In the stored procedure, Oracle uses Select to return a result set. Instead, it uses Out-type parameters to return a result set. It is actually using REF CURSOR
-- Procedure returned record set: ---------------------- declare a Package -------------- create or replace package pkg_testASTYPEmyrctypeIS ref cursor; PROCEDURE get_r (p_id NUMBER, p_rc OUT myrctype ); -- declare Procedure named get in Package (only the interface has no content) END pkg_test; --------------- declare Package Body, that is, the content in the Package above, including Procedure get --------------------- www.2cto.com create or replace package body pkg_testASPROCEDURE get_r (p_id NUMBER, p_rc OUT Myrctype) ISsqlstr VARCHAR2 (500); BEGINIF p_id = 0 THENOPEN p_rc forselect id, NAME, sex, address, postcode, birthdayFROM student; ELSEsqlstr: = 'select id, name, sex, address, postcode, birthdayfrom student where id =: w_id '; -- w_id is a parameter. -- The following p_rc is a ref cursor type and an OUT parameter, returns a record set. USING p_id is an example of returning a record set from the preceding SQL statement: w_id value pulling: OPEN p_rc FOR sqlstr USING p_id; END IF; END get; END pkg_test; www.2cto.com -- function, the principle is the same as above, but the return value of function is used to return the record set.
Function return record set: CREATE a package and package with ref cursor defined and function: create or REPLACEpackage pkg_test as type myrctype is ref cursor; function get_r (intID number) return myrctype; end pkg_test; www.2cto.com/create or REPLACEpackage body pkg_test as -- function body function get_r (intID number) return myrctype isrc myrctype; -- Define the ref cursor variable sqlstr varchar2 (500 ); begin www.2cto.com if intID = 0 then
-- Static Test: directly return the results of open rc for select id, name, sex, address, postcode, birthday from student; else -- dynamic SQL assignment using the following statement: w_id to declare that the variable is obtained from the external sqlstr: = 'select id, name, sex, address, postcode, birthday from student where id =: w_id '; -- dynamic test, return results with sqlstr string, use using keywords to pass the parameter open rc for sqlstr using intid; end if; return rc; end get; end pkg_test; this article is from the site

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.