2
votes

As a noob to stored procedures, I can't get my head around how to get a classic ASP (vbscript) page to return a value from a stored procedure. I can write data to a table ok, it's just retrieving stuff that has me stuck.

If I use a very basic example. This is a stored procedure that should return a count of the records in a table:

CREATE PROCEDURE [dbo].[SP_RecordCount] 

AS

SELECT COUNT(*) FROM MyTable

What Classic ASP code (and what changes to the SPROC) would I need to write to be able to display the value in a browser?

Once I understand how to get a basic output such as this, I should hopefully(!) be able to build on the knowledge.

Thanks.

3

3 Answers

5
votes

Probably the cleanest way is to modify your stored procedure like this:

CREATE PROCEDURE [dbo].[SP_RecordCount] 
@CountResult int OUTPUT
AS

SELECT @CountResult = COUNT(*) FROM MyTable

... and this is the server-side code to call it and read the value:

Dim cmd
Set cmd = Server.CreateObject("ADODB.Command")

Set cmd.ActiveConnection = .... previously opened ADO connection here
cmd.CommandType = adCmdStoredProc  'be sure adCmdStoredProc constant is set in scope, or hardcode relevant integer instead'
cmd.CommandText = "SP_RecordCount"
cmd.Parameters.Refresh 

cmd.Execute 
Dim count       
count = cmd.Parameters(1)
2
votes

See How to call SQL Server stored procedures from ASP

 <%@ LANGUAGE="VBSCRIPT" %>
   <!--#include virtual="/ASPSAMP/SAMPLES/ADOVBS.INC"-->
   <HTML>
   <HEAD><TITLE>Place Document Title Here</TITLE></HEAD>
   <BODY>
   This first method queries the data source about the parameters
   of the stored procedure. This is the least efficient method of calling
   a stored procedure.<BR>
   <%
   Set cn = Server.CreateObject("ADODB.Connection")
   Set cmd = Server.CreateObject("ADODB.Command")
   cn.Open "data source name", "userid", "password"
   Set cmd.ActiveConnection = cn
   cmd.CommandText = "sp_test"
   cmd.CommandType = adCmdStoredProc
   ' Ask the server about the parameters for the stored proc
   cmd.Parameters.Refresh
   ' Assign a value to the 2nd parameter.
   ' Index of 0 represents first parameter.
   cmd.Parameters(1) = 11
   cmd.Execute
   %>
   Calling via method 1<BR>
   ReturnValue = <% Response.Write cmd.Parameters(0) %><P>

   <!-- ************************************************************ -->

   Method 2 declares the stored procedure, and then explicitly declares
   the parameters.<BR>
   <%
   Set cn = Server.CreateObject("ADODB.Connection")
   cn.Open "data source name", "userid", "password"
   Set cmd = Server.CreateObject("ADODB.Command")
   Set cmd.ActiveConnection = cn
   cmd.CommandText = "sp_test"
   cmd.CommandType = adCmdStoredProc
   cmd.Parameters.Append cmd.CreateParameter("RetVal", adInteger, _
      adParamReturnValue)
   cmd.Parameters.Append cmd.CreateParameter("Param1", adInteger, _
      adParamInput)
   ' Set value of Param1 of the default collection to 22
   cmd("Param1") = 22
   cmd.Execute
   %>
   Calling via method 2<BR>
   ReturnValue = <% Response.Write cmd(0) %><P>

   <!-- ************************************************************ -->

   Method 3 is probably the most formal way of calling a stored procedure.
   It uses the canocial
   <%
   Set cn = Server.CreateObject("ADODB.Connection")
   cn.Open "data source name", "userid", "password"
   Set cmd = Server.CreateObject("ADODB.Command")
   Set cmd.ActiveConnection = cn
   ' Define the stored procedure's inputs and outputs
   ' Question marks act as placeholders for each parameter for the
   ' stored procedure
   cmd.CommandText = "{?=call sp_test(?)}"
   ' specify parameter info 1 by 1 in the order of the question marks
   ' specified when we defined the stored procedure
   cmd.Parameters.Append cmd.CreateParameter("RetVal", adInteger, _
   adParamReturnValue)
   cmd.Parameters.Append cmd.CreateParameter("Param1", adInteger, _
     adParamInput)
   cmd.Parameters("Param1") = 33
   cmd.Execute
   %>
   Calling via method 3<BR>
   ReturnValue = <% Response.Write cmd("RetVal") %><P>
   </BODY>
   </HTML>
0
votes

Add the command SET NOCOUNT ON in your procedure

CREATE PROCEDURE [dbo].[SP_RecordCount] 

AS

SET NOCOUNT ON

SELECT COUNT(*) FROM MyTable