1. This is also the simplest method. Two input parameters have no return value:
Set connection = server. Createobject ("ADODB. Connection ")
Connection. Open somedsn
Connection. Execute "procname varvalue1, varvalue2"
'Clear all objects as nothing and release resources
Connection. Close
Set connection = nothing
2. If you want to return the recordset:
Set connection = server. Createobject ("ADODB. Connection ")
Connection. Open somedsn
Set rs = server. Createobject ("ADODB. recordset ")
Rs. Open "Exec procname varvalue1, varvalue2", connection
'Clear all objects as nothing and release resources
Rs. Close
Connection. Close
Set rs = nothing
Set connection = nothing
3 none of the above two methods can return values (except for recordset). To obtain the return value, use the command method.
First, there are two return values. One is to directly return a value in the stored procedure, just like the return values of C and VB functions; the other is to return multiple values, the variable names that store these values must be specified in the call parameters first.
In this example, we need to process multiple parameters, input parameters, output parameters, return record sets, and a direct return value (enough ?)
The stored procedure is as follows:
Use pubs
Go
-- Create a stored procedure
Create procedure sp_pubstest
-- Define three parameter variables. Note that the third parameter is used for output.
@ Au_lname varchar (20 ),
@ Intid int,
@ Intidout int output
As
Select @ intidout = @ intid + 1
Select *
From authors
Where au_lname like @ au_lname + '%'
-- Returns a value directly.
Return @ intid + 2
ASP that calls the Stored ProcedureProgramAs follows:
<% @ Language = VBScript %>
<%
Dim cmdsp
Dim adors
Dim adcmdspstoredproc
Dim adparamreturnvalue
Dim adparaminput
Dim adparamoutput
Dim adinteger
Dim ival
Dim oval
Dim adofield
Dim advarchar
'These values are predefined constants in VB and can be called directly, but they are not predefined in VBScript.
Adcmdspstoredproc = 4
Adparamreturnvalue = 4
Adparaminput = 1
Adparamoutput = 2
Adinteger = 3
Advarchar = 200
Ival = 5
Oval = 3
'Create a command object
Set cmdsp = server. Createobject ("ADODB. Command ")
'Establish a connection
Cmdsp. activeconnection = "driver = {SQL Server}; server = (local); uid = sa; Pwd =; database = pubs"
'Define the name of the command object call
Cmdsp. commandtext = "sp_pubstest"
'Set the command call type to stored procedure (admo-spstoredproc = 4)
Cmdsp. commandtype = adw.spstoredproc
'Add parameters to the command object
'The stored procedure has a direct return value and is an integer. The missing value is 4.
Cmdsp. Parameters. append cmdsp. createparameter ("return_value", adinteger, adparamreturnvalue, 4)
'Define a struct input parameter
Cmdsp. Parameters. append cmdsp. createparameter ("@ au_lname", advarchar, adparaminput, 20, "M ")
'Define an integer input parameter
Cmdsp. Parameters. append cmdsp. createparameter ("@ intid", adinteger, adparaminput, ival)
'Define an integer output parameter
Cmdsp. Parameters. append cmdsp. createparameter ("@ intidout", adinteger, adparamoutput, oval)
'Run the stored procedure and get the returned record set
Set adors = cmdsp. Execute
'Print each record. The fields in the record are virtual.
While not adors. EOF
For each adofield in adors. Fields
Response. Write adofield. Name & "=" & adofield. Value & "<br>" & vbcrlf
Next
Response. Write "<br>"
Adors. movenext
Wend
'Print two output values:
Response. Write "<p> @ intidout =" & cmdsp. parameters ("@ intidout"). Value & "</P>"
Response. Write "<p> return value =" & cmdsp. parameters ("return_value"). Value & "</P>"
'Cleaning
Set adors = nothing
Set cmdsp. activeconnection = nothing
Set cmdsp = nothing
%>
In addition, there are other methods, which may be slightly biased and will be discussed later.
I have referenced many articlesArticle, Which is not listed here.