What's the most efficient way to return a boolean (true/false) out of a T-SQL Stored Procedure? I want it to do a query and return whether it succeeded or not. I'm calling via ASP.
从T-SQL存储过程中返回布尔值(true / false)的最有效方法是什么?我想让它做一个查询并返回它是否成功。我是通过ASP打电话的。
Let me be a little more specific about what I'm doing.
让我对我正在做的事情稍微具体一点。
If a record exists in a table (indicating a document is already reserved and can't be checked out), I want to notify the user on the front end. I'll determine that by checking Exists... in T-SQL and then somehow pushing that back to Classic ASP (return value, parameter, recordset field).
如果表中存在记录(表示文档已被保留且无法检出),我想在前端通知用户。我将通过在T-SQL中检查存在...然后以某种方式将其推回到经典ASP(返回值,参数,记录集字段)来确定。
Does that make any answer more reasonable?
这会使任何答案更合理吗?
3 个解决方案
#1
1
I have this function in ASP that assume that the SP takes the last parameter as an integer output value.
我在ASP中有这个函数,假设SP将最后一个参数作为整数输出值。
Returning and integer is better, cause you can return several states, and not only 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
#2
1
Return a bit: "an integer data type that can take a value of 1, 0, or NULL."
返回一点:“一个整数数据类型,可以取值为1,0或NULL。”
#3
1
Not a good idea.
不是个好主意。
A return value, output parameter or a recordset will be undefined or not set or partial if you have an error. For example, a CAST error will abort the code (without TRY/CATCH).
如果出现错误,则返回值,输出参数或记录集将是未定义的或未设置或部分。例如,CAST错误将中止代码(不带TRY / CATCH)。
A far better method will rely on Exception handling, like this:
一个更好的方法将依赖于异常处理,如下所示:
BEGIN TRY
...
--assume worked
END TRY
BEGIN CATCH
DECLARE @foo varchar(2000)
SET @foo = ERROR_MESSAGE()
RAISERROR (@foo, 16,1)
END CATCH
However, I suspect I could be answering your later question about "why didn't SQL Server do ...?"...
但是,我怀疑我可以回答你后来的问题:“为什么SQL Server没有......?”......
#1
1
I have this function in ASP that assume that the SP takes the last parameter as an integer output value.
我在ASP中有这个函数,假设SP将最后一个参数作为整数输出值。
Returning and integer is better, cause you can return several states, and not only 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
#2
1
Return a bit: "an integer data type that can take a value of 1, 0, or NULL."
返回一点:“一个整数数据类型,可以取值为1,0或NULL。”
#3
1
Not a good idea.
不是个好主意。
A return value, output parameter or a recordset will be undefined or not set or partial if you have an error. For example, a CAST error will abort the code (without TRY/CATCH).
如果出现错误,则返回值,输出参数或记录集将是未定义的或未设置或部分。例如,CAST错误将中止代码(不带TRY / CATCH)。
A far better method will rely on Exception handling, like this:
一个更好的方法将依赖于异常处理,如下所示:
BEGIN TRY
...
--assume worked
END TRY
BEGIN CATCH
DECLARE @foo varchar(2000)
SET @foo = ERROR_MESSAGE()
RAISERROR (@foo, 16,1)
END CATCH
However, I suspect I could be answering your later question about "why didn't SQL Server do ...?"...
但是,我怀疑我可以回答你后来的问题:“为什么SQL Server没有......?”......