Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return a value and a result set from stored procedure classic asp

I want to get both a return code and a result set back from a stored procedure in classic 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

So in ASP I can do the following to get the return value

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")

or this to get the result set.

Set cmd = CreateObject("ADODB.Command")
with cmd
    .ActiveConnection = cnnstr
    .CommandType = adCmdStoredProc
    .CommandText = "CheckEmployeeId"
    .Parameters.Refresh
    .Parameters("@EmployeeName")    = EmployeeName
    Set rst = .Execute()
end with

Is there a way to get both?

like image 486
David Elliott Avatar asked Feb 06 '17 14:02

David Elliott


1 Answers

You are already doing it just combine the two.

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")

You don't need to choose one or the other the are independent of each other. The only issue will be the order they return, remember that both OUTPUT and RETURN values will not be accessible until all returned Recordsets are closed.

Personally, I prefer to close them straight away by storing them as 2 Dimensional Arrays.

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
like image 74
user692942 Avatar answered Oct 13 '22 02:10

user692942