13.01.2013 Views

OpenEdge Data Management: DataServer for Microsoft SQL Server

OpenEdge Data Management: DataServer for Microsoft SQL Server

OpenEdge Data Management: DataServer for Microsoft SQL Server

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Interfacing with RDBMS stored procedures<br />

Interfacing with RDBMS stored procedures<br />

As previously mentioned, the RUN STORED-PROCEDURE statement is the initial statement required<br />

<strong>for</strong> the execution of all stored procedures you define and initiate through <strong>OpenEdge</strong>. It can run<br />

an RDBMS stored procedure bound by the data source, or allow you to send <strong>SQL</strong> to a MS <strong>SQL</strong><br />

<strong>Server</strong> data source using an <strong>OpenEdge</strong> <strong>Data</strong><strong>Server</strong>. The specific, additional keywords and<br />

elements you must include in a stored procedure or define <strong>for</strong> a send-sql-statement depend on<br />

the stored procedure characteristics including its signature, data results, and methods chosen to<br />

retrieve output to an ABL procedure.<br />

This section identifies and describes the syntax elements, keywords, and other requirements<br />

associated with obtaining stored procedure output from a stored procedure data request.<br />

Example 3–3 shows a stored procedure with multiple result sets, output parameters, and a return<br />

value.<br />

/* pcust stored procedure*/<br />

CREATE PROCEDURE pcust (@num INT, @orders INT OUT, @states INT OUT) AS<br />

BEGIN<br />

SELECT customer.cust_num, customer.name, order_num FROM customer, order_<br />

WHERE customer.cust_num = order_.cust_num AND customer.cust_num > @num<br />

SELECT @orders = @@rowcount<br />

SELECT cust_num, state.state FROM customer, state WHERE<br />

customer.state = state.state AND customer.cust_num > @num<br />

SELECT @states = @@rowcount<br />

END<br />

RETURN 0<br />

Example 3–3: A stored procedure with multiple result sets, output parameters,<br />

and a single return value<br />

This Transact-<strong>SQL</strong> code creates the stored procedure pcust and defines three parameters: num,<br />

which is an input parameter (the default parameter type), and orders and states, which are<br />

output parameters. The procedure returns values <strong>for</strong> the output parameters to the caller after<br />

processing the results of the pcust SELECT statements. You can think of output parameters as<br />

temporary fields associated with a record buffer defined by the procedure definition in the<br />

<strong>OpenEdge</strong> schema image; that is, you can access the data in these columns using the standard<br />

notation of tablename.fieldname. (Note that although pcust is a stored procedure, it is stored<br />

as a table definition.) For example, you can access the data in the orders and states fields by<br />

specifying pcust.orders and pcust.states. All the parameters in the example have an<br />

INTEGER data type.<br />

Retrieving return codes<br />

You can create a stored procedure that provides return code in<strong>for</strong>mation. For example, it might<br />

indicate whether the stored procedure was successful or whether it encountered an error<br />

condition.<br />

Example 3–4 shows how ABL code runs the stored procedure pcust. It uses the PROC–STATUS<br />

function and the CLOSE STORED–PROC statement to retrieve the return code and assign the value<br />

to the variable stat. The meaning of this return code is defined by the underlying data source.<br />

3–15

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!