HOWTO: Get Records and Return Value from Stored Proc Using DEID: Q192667
|
If you have a stored procedure that returns a recordset and has a RETURN coded in it that you want to also capture, you can use the Data Environment (DE) to call the stored procedure and do a Reponse.Write to display the return value. However, you would most likely use the value to determine a course of action and not display it. This sample is done using server-side Active Server Pages (ASP)/Active Data Objects (ADO) and the Data Environment.
NOTE: This article uses the SQL Server database of Pubs. It assumes that you have used the ODBC icon on the Control Panel to create a valid DSN to Pubs. It also assumes that a project has been created and is open in Visual InterDev.
Create Procedure StoredProcedure2
/*
(
@parameter1 datatype = default value,
@parameter2 datatype OUTPUT
)
*/
As
/* set nocount on */
return
Paste the recently copied text at the beginning this newly created file.
Immediately following the pasted text add the command RETURN.
CREATE PROCEDURE byroywret @percentage int
AS
select au_id from titleauthor
where titleauthor.royaltyper = @percentage
return
sub thisPage_onenter()
'instantiate the DERuntime object
set DE = Server.createobject("DERuntime.DERuntime")
DE.init Application("DE")
'get recordset and return value from Stored Proc
set RS = DE.Recordsets("Command1")
set parms = RS.activeCommand.parameters
' hard-code a value of 50 into the "percentage" parameter
parms(1) = 50
RS.open
'grab the return value
intRetVal = parms (0)
Response.Write("The return value is :")
Response.Write(intRetVal)
' loop through the Recordset (RS) and put values into a table
Response.Write("<TABLE>")
do until RS.eof
Response.Write("<TR><TD>" & RS("au_id"))
RS.MoveNext
loop
Response.Write("</TABLE>")
end sub
If your default scripting language is JavaScript (ECMAScript) inside the
newly created sub or function, add the following code:
function thisPage_onenter() {
//instantiate the DERuntime object
var DE = Server.createobject("DERuntime.DERuntime");
DE.init (Application("DE"));
//get recordset and return value from Stored Proc
var RS = DE.Recordsets("Command1");
var parms = RS.activeCommand.parameters;
parms(1) = 50;
RS.open();
intRetVal = parms (0);
Response.Write("The return value is :");
Response.Write(intRetVal);
//loop through the Recordset (RS) and put values into a table
Response.Write("<TABLE>");
while (!RS.EOF) {
Response.Write("<TR><TD>" + RS("au_id"))
RS.MoveNext()
}
Response.Write("</TABLE>");
}
For additional information about how to use the Data Command against a Stored Procedure that returns only a RETURN value but no recordset, please see the following article in the Microsoft Knowledge Base:
Q190762 PRB: Cannot Access a Stored Procedure's Return Value from DTC
Additional query words:
Keywords : kbADO kbVisID600 kbGrpASP
Version : WINDOWS:6.0
Platform : WINDOWS
Issue type : kbhowto
Last Reviewed: April 27, 1999