从 T-SQL 存储过程返回布尔值

Cav*_*rob 5 t-sql sql-server asp-classic

从 T-SQL 存储过程返回布尔值(真/假)的最有效方法是什么?我希望它执行查询并返回是否成功。我通过 ASP 调用。

让我更具体地介绍一下我正在做的事情。

如果表中存在记录(表明文档已被保留并且无法签出),我想在前端通知用户。我将通过检查 T-SQL 中的 Exists... 来确定这一点,然后以某种方式将其推回经典 ASP(返回值、参数、记录集字段)。

这会让答案更合理吗?

Edu*_*eni 2

我在 ASP 中有这个函数,假设 SP 将最后一个参数作为整数输出值。

返回整数更好,因为您可以返回多种状态,而不仅仅是 true/false。

Function RunSPReturnInteger(strSP , params())
    On Error resume next

    ''// Create the ADO objects
    Dim cmd
    Set cmd = server.createobject("ADODB.Command")

    ''// Init the ADO objects & the stored proc parameters
    cmd.ActiveConnection = GetConnectionString()
    cmd.CommandText = strSP
    cmd.CommandType = adCmdStoredProc

    ''// propietary function that put the params in the cmd
    collectParams cmd, params

    ''// Assume the last parameter is outgoing
    cmd.Parameters.Append cmd.CreateParameter("@retval", adInteger, adParamOutput, 4)

    ''// Execute without a resulting recordset and pull out the "return value" parameter
    cmd.Execute , , adExecuteNoRecords
    If err.number > 0 then
        BuildErrorMessage()
        exit function
    end if
    RunSPReturnInteger = cmd.Parameters("@retval").Value

    ''// Disconnect the recordset, and clean up
    Set cmd.ActiveConnection = Nothing
    Set cmd = Nothing

    Exit Function
End Function
Run Code Online (Sandbox Code Playgroud)