stored processes with input parameters and output parameters
The return value is actually a special output parameter. In most cases, we use a stored process that has both input and output parameters, such as we want to obtain the user name of an ID user in the User Information table, and then there is an input parameter----user ID, and an output parameter----user name. The storage processes that implement this feature are as follows:
/*sp4*/
CREATE PROCEDURE DBO. GetUserName
@USERID INT,
@USERNAME VARCHAR () OUTPUT
As
SET NOCOUNT on
BEGIN
IF @USERID is NULL return
SELECT @USERNAME =username
From DBO. [USERINFO]
WHERE userid= @USERID
Return
End
Go
The ASP code that invokes the stored process is as follows:
' * * Call a stored process with input and output parameters * *
DIM Mycomm,userid,username
USERID = 1
SET Mycomm = SERVER. CreateObject ("Adodb.command")
Mycomm. ActiveConnection = Myconstr ' myconstr is a database connection string
Mycomm.commandtext = "GetUserName" ' specifies the name of the stored process
Mycomm.commandtype = 4 ' indicates that this is a stored process
Mycomm. PREPARED = TRUE ' requires that SQL commands be compiled in advance
' Declare parameters
Mycomm. PARAMETERS. APPEND Mycomm. CreateParameter ("@USERID", 3,1,4,userid)
Mycomm. PARAMETERS. APPEND Mycomm. CreateParameter ("@USERNAME", 200,2,40)
Mycomm. EXECUTE
' Get the argument
USERNAME = Mycomm (1)
SET Mycomm = Nothing
In the above code, you can see that, unlike declaring a return value, you need 5 parameters to declare an input parameter, and 4 parameters to declare an output parameter. When declaring an input parameter, 5 parameters are: Parameter name, parameter data type, parameter type, data length, parameter value. When declaring an output parameter, there is no last argument: parameter value.
It is important to note that when declaring parameters, the order must be the same as defined in the stored process, and the data type and length of each parameter are the same as defined in the stored process.
If the stored process has more than one parameter, the ASP code becomes cumbersome, and you can use the WITH command to simplify the code:
' * * call stored processes with input and output parameters (simplified code) * *
DIM mycomm,userid,username
USERID = 1
SET mycomm = SERVER. CreateObject ("Adodb.command")
with Mycomm
. ActiveConnection = Myconstr ' myconstr is a database connection string
. CommandText = "GetUserName" Specifies the stored process name
. CommandType = 4 ' indicates that this is a stored process
. PREPARED = TRUE ' requires the SQL command to be compiled in advance
. PARAMETERS. APPEND. CreateParameter ("@USERID", 3,1,4,userid)
. PARAMETERS. APPEND. CreateParameter ("@USERNAME", 200,2,40)
. EXECUTE
End With
USERNAME = Mycomm (1)
SET Mycomm = Nothing
Suppose you want to create a 10 command object if we want to obtain a user name of 1 to 10, 10 users? No. If you need to call the same stored process multiple times, you will get different output by simply changing the input parameters:
' * * Calls the same stored process multiple times * *
DIM Mycomm,userid,username
USERNAME = ""
SET Mycomm = SERVER. CreateObject ("Adodb.command")
For USERID = 1 to 10
With Mycomm
. ActiveConnection = Myconstr ' myconstr is a database connection string
. CommandText = "GetUserName" ' specifies the name of the stored process
. CommandType = 4 ' indicates that this is a stored process
. PREPARED = TRUE ' requires that SQL commands be compiled in advance
IF USERID = 1 THEN
. PARAMETERS. APPEND. CreateParameter ("@USERID", 3,1,4,userid)
. PARAMETERS. APPEND. CreateParameter ("@USERNAME", 200,2,40)
. EXECUTE
ELSE
' re-assigning values to the parameters (at which point the parameter values do not change and the argument does not have to be declared again)
. PARAMETERS ("@USERID") = USERID
. EXECUTE
End IF
End With
USERNAME = USERNAME + mycomm (1) + "," ' Maybe you like to use array storage
NEXT
SET Mycomm = Nothing
As you can see from the above code: when you call the same stored process repeatedly, you can simply reassign the input parameters that have changed the value, which greatly reduces the amount of code when there are multiple input and output parameters, and the value of only one input parameter changes at each call.