Stored procedure return parameters

Adaptive Server stored procedures and mainframe server transactions can return values for specified “return parameters.” Changes made to the value of a return parameter inside the stored procedure or transaction are then available to the program that called the procedure or transaction. This is analogous to the “pass by reference” facility available in some programming languages.

In order for a parameter to function as a return parameter, it must be declared as such within the stored procedure. Client-Library applications use the CTBPARAM routine to indicate return parameters.

Processing RPC return parameters

Return parameter values are available to an application as a parameter result set only if the application invoked the stored procedure using an RPC.

CTBRESULTS returns CS-PARAM-RESULT if a parameter result set is available to be processed. Because stored procedure parameters are returned to an application as a single row, one call to CTBFETCH copies all of the return parameters for a procedure into the program variables designated via CTBBIND. However, an application should always call CTBFETCH in a loop until it returns CS-END-DATA.

When executing a stored procedure, the server returns any parameter values immediately after returning all row results. Therefore, an application can fetch return parameters only after processing the stored procedure row results. A stored procedure can generate several sets of row results—one for each select it contains. An application must call CTBRESULTS and CTBFETCH as many times as necessary to process these row results before calling CTBFETCH to fetch the stored procedure return parameters.

Stored procedure return status

Adaptive Server, Open Server, and Open ServerConnect applications can all return a status.

All stored procedures that run on an Adaptive Server version 4.0 or later return a status. Stored procedures usually return 0 to indicate normal completion. For a list of Adaptive Server default return status values, see return in the Adaptive Server Enterprise Reference Manual, which is part of the basic Sybase documentation set. Open ServerConnect status values are documented under TDSNDDON and TDSTATUS in the Mainframe Connect Server Option Programmer’s Reference for COBOL.

Because return status values are a feature of stored procedures, only an RPC or a language request containing an execute statement can generate a return status.

When executing a stored procedure, Adaptive Server returns the status immediately after returning all other results. Therefore, an application can fetch a return status only after processing the stored procedure row and parameter results, if any.

Open Server applications return the status after any row results, but either before or after return parameters.

Processing an RPC return status

CTBRESULTS returns CS-STATUS-RESULT if a return status result set is available to be processed. Because a return status result set contains only a single value, one call to CTBFETCH copies the status into the program variable designated via CTBBIND. However, an application should always call CTBFETCH in a loop until it returns CS-END-DATA.