我想从经典ASP中的存储过程中获取返回代码和结果集。
CREATE PROCEDURE CheckEmployeeId
@EmployeeName nvarchar(255)
AS
BEGIN
SET NOCOUNT ON;
DECLARE
@Exists INT
, @RowCount Int = 0
, @ReturnValue Int = 1
SELECT EmployeeId FROM Employees WHERE Name = @EmployeeName
set @RowCount = @@ROWCOUNT
if (@RowCount <> 1)
BEGIN
SET @ReturnValue = 2
END
ELSE
BEGIN
SET @ReturnValue = 1
END
RETURN @ReturnValue
END
所以在ASP中,我可以执行以下操作来获取返回值
Set cmd = CreateObject("ADODB.Command")
with cmd
.ActiveConnection = cnnstr
.CommandType = adCmdStoredProc
.CommandText = "CheckEmployeeId"
.Parameters.Refresh
.Parameters("@EmployeeName") = EmployeeName
end with
cmd.Execute()
RetVal = cmd.Parameters("@RETURN_VALUE")
或者这样得到结果集。
Set cmd = CreateObject("ADODB.Command")
with cmd
.ActiveConnection = cnnstr
.CommandType = adCmdStoredProc
.CommandText = "CheckEmployeeId"
.Parameters.Refresh
.Parameters("@EmployeeName") = EmployeeName
Set rst = .Execute()
end with
有没有办法让两者兼得?
答案 0 :(得分:2)
你已经这样做了,只需将两者结合起来。
Set cmd = CreateObject("ADODB.Command")
with cmd
.ActiveConnection = cnnstr
.CommandType = adCmdStoredProc
.CommandText = "CheckEmployeeId"
.Parameters.Refresh
.Parameters("@EmployeeName") = EmployeeName
Set rst = .Execute()
end with
'You will need to close the Recordset before returning the RETURN_VALUE.
RetVal = cmd.Parameters("@RETURN_VALUE")
您不需要选择彼此独立的一个或另一个。唯一的问题是他们返回的订单,请记住,在关闭所有返回的Recordset之前,无法访问OUTPUT
和RETURN
值。
就个人而言,我更喜欢将它们存储为2维数组,直接将它们关闭。
Set cmd = CreateObject("ADODB.Command")
with cmd
.ActiveConnection = cnnstr
.CommandType = adCmdStoredProc
.CommandText = "CheckEmployeeId"
.Parameters.Refresh
.Parameters("@EmployeeName") = EmployeeName
Set rst = .Execute()
If Not rst.EOF Then data = rst.GetRows()
Call rst.Close()
end with
RetVal = cmd.Parameters("@RETURN_VALUE")
'Access Recordset array
If IsArray(data) Then
'Return first column, first row.
Response.Write data(0, 0)
End If