Transfer Data between stored procedures

Source: Internet
Author: User

---- Start

Stored Procedures can call each other. How can we transmit data between them? There are three methods.

I. Data Transmission Through Input and Output Parameters

This is the simplest method, but it is worth noting that the return statement in the stored procedure has different meanings than the return statement in other programming languages. The Return Statement in the stored procedure puts back the sqlcode value.

Ii. pass data through session global variables

-- Connect to the database <br/> connect to sample! </P> <p> -- creates a global session variable <br/> Create variable myvar integer default 0! </P> <p> -- create Stored Procedure 1 <br/> Create procedure testproc1 () <br/> begin <br/> set myvar = 10; <br/> end! </P> <p> -- create Stored Procedure 2 <br/> Create procedure testproc2 (Out MSG char (20 )) <br/> begin <br/> call testproc1 (); </P> <p> If myvar = 10 then <br/> set MSG = 'test successfully '; <br/> else <br/> set MSG = 'test failed'; <br/> end if; <br/> end! </P> <p> -- call Stored Procedure 2 <br/> call testproc2 (?)! </P> <p> -- delete a stored procedure <br/> drop procedure testproc1! <Br/> drop procedure testproc2! </P> <p> -- close the connection <br/> connect reset! 

Iii. Passing cursor between stored procedures

 

To use the result set of another stored procedure in one stored procedure, perform the following steps:

1. Declare a result set locator: declare <rs_locator_var> result_set_locator varying;

2. Associate the result set locator with the caller process: Associate result set Locator (<rs_locator_var>) with procedure <proc_called>;

3. assign a cursor pointing to the result set from the call process: allocate <cursor> cursor for result set <rs_locator_var>;

-- Connect to the database <br/> connect to sample! </P> <p> -- create Stored Procedure 1 <br/> Create procedure testproc1 () <br/> dynamic result sets 1 <br/> P1: begin <br/> -- declare the cursor <br/> declare mycur cursor with return for <br/> select * from <br/> (<br/> values <br/> (1 ), <br/> (2), <br/> (3) <br/>); <br/> -- open the cursor <br/> open mycur; <br/> end P1! </P> <p> -- create Stored Procedure 2 <br/> Create procedure testproc2 (Out res integer) <br/> begin <br/> declare sqlcode int default 0; </P> <p> -- declare a result set locator <br/> declare loc1 result_set_locator varying; <br/> declare X int default 0; </P> <p> set res = 0; <br/> call testproc1 (); </P> <p> -- associate the result set locator with the caller Process <br/> associate result set Locator (loc1) with procedure testproc1; </P> <p> -- allocate the cursor pointing from the call process to the result set <br/> allocate C1 cursor F Or result set loc1; </P> <p> fetch from C1 into X; <br/> while sqlcode = 0 DO <br/> set res = res + X; <br/> fetch from C1 into X; <br/> end while; <br/> end! </P> <p> -- call Stored Procedure 2 <br/> call testproc2 (?)! </P> <p> -- delete a stored procedure <br/> drop procedure testproc1! <Br/> drop procedure testproc2! </P> <p> -- close the connection <br/> connect reset! 

 

 

---- For more information, see:SQL pl

----Statement: indicate the source for reprinting.

---- Last updated on 2010.2.3

---- Written by shangbo on 2010.2.3

---- End

 

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.