Microsoft KB Archive/300488

= How to run SQL Server stored procedures from an ASP Page =

Article ID: 300488

Article Last Modified on 12/14/2005

-

APPLIES TO


 * Microsoft Active Server Pages 4.0
 * Microsoft SQL Server 2000 Standard Edition
 * Microsoft SQL Server 7.0 Standard Edition
 * Microsoft SQL Server 2005 Developer Edition
 * Microsoft SQL Server 2005 Enterprise Edition
 * Microsoft SQL Server 2005 Standard Edition
 * Microsoft SQL Server 2005 Workgroup Edition

-



This article was previously published under Q300488



SUMMARY
This article describes how to run SQL Server stored procedures and use parameters from an Active Server Pages (ASP) page.

This article assumes that you are familiar with the procedure to use ActiveX Data Objects (ADO) in an ASP page.



Step-by-Step Example
  Run the following query in SQL Server Query Analyzer or SQL Server Management Studio against the Pubs database: CREATE proc MyProc (   @price smallint,    @out smallint OUTPUT ) AS Select @out = count(*) from titles where price < @price GO The stored procedure (&quot;MyProc&quot;) takes one input parameter (&quot;@price&quot;) and returns one output parameter (&quot;@out&quot;).

Note By default, the Northwind sample database and the pubs sample databases are not installed in SQL Server 2005. These databases can be downloaded from the Microsoft Download Center. For more information about how to download the Northwind sample database and the pubs sample database, visit the following Microsoft Web site:

http://msdn2.microsoft.com/en-us/library/ms143221.aspx

  The following ASP sample code calls the newly created stored procedure. You can use this ASP code to set up the input parameter and run the query.   Use the CreateParameter method to create parameters in Microsoft ActiveX Data Objects (ADO) as follows: Set myParameter = Command.CreateParameter (Name, [Type], [Direction], [Size], [Value])   Appended the parameter to the Parameters collection as follows: Command.Parameters.Append myParameter NOTE: The parameters in the Parameters collection must match the order of the parameters in the stored procedure.   Run the command to pass parameter values in and out of the stored procedure as follows: <% Dim cmd Dim ln Dim retCount

Set cmd = Server.CreateObject(&quot;ADODB.Command&quot;) With cmd .ActiveConnection = &quot;Paste your connection string here&quot; .Commandtext = &quot;MyProc&quot; .CommandType = adCmdStoredProc .Parameters.Append .CreateParameter(&quot;@price&quot;, adSmallInt, adParamInput, 10) .Parameters(&quot;@price&quot;) = 22 .Parameters.Append .CreateParameter(&quot;@retValue&quot;, adSmallInt, adParamOutput, 10) .Execute ln,, adExecuteNoRecords retCount = .Parameters(&quot;@retValue&quot;) End with

Response.Write retcount

Set cmd = Nothing %>                       

NOTE: The constants that are used in this sample can be found in the Adovbs.inc file. This file is installed during Active Server Pages setup and placed in the \Aspsamp\Samples folder, which is normally located in your \Inetpub folder. It is recommended programming practice to use the constants rather than the numerical values when you call your stored procedure so that your code is easier to read and maintain. <%@ LANGUAGE = VBScript %>

 Modify the ADO connection string as appropriate for your environment.</li>  Save the ASP page, and view it in the browser.

NOTE: It can be difficult to determine how to properly call a stored procedure if you are unaware of the stored procedure's parameter information. Without the correct information, you cannot properly create the ADO parameters. You can use the Refresh method of the Parameter object to populate the Parameters collection automatically, based on the stored procedure's definition on the server. For example: Command.Parameters.Refresh </li></ol>

<div class="references_section">