存储过程作为 IF 条件布尔表达式返回



在 IF 语句中使用存储过程(或函数(返回作为条件的最佳方法是什么?

允许我做一些接近于此的事情,声明了一个过程/函数"CheckCondition",它使用 CAST 或作为输出返回真/假 1/0 值

IF CheckCondition 1, 'asfa' 
BEGIN
//do something
ELSE
//do something else
END

该函数/过程不需要执行插入或更新,只需选择,所以我想函数就可以了。

谢谢

具有输出参数的存储过程 一个解决方案;

create proc dbo.Condition
@Result bit output
as
set nocount on
set @Result=0 -- assumes false . . .
if 1=1 -- i.e. this is TRUE
begin
set @Result=1
end
go
declare @Result bit
exec dbo.Condition
@Result=@Result output
print @Result -- here's the result

上面的例子创建了一个进程,对接处的位是如何执行它并获得结果

从评论中,终于这样做了,用函数代替程序:

CREATE FUNCTION [dbo].[CheckCondition]
(
@Parameter1 int = 0,
@Parameter2 NVARCHAR(MAX) = 0,
)
RETURNS bit
AS
BEGIN   
IF ( boolean expression with selects )          
RETURN CAST(1 AS bit)
ELSE
RETURN CAST(0 AS bit)
RETURN CAST(0 AS bit)
END

像这样使用它:

IF dbo.CheckCondition(@param1, 'sdasf') = 1
//Do something
ELSE
//Do something else

谢谢大家!

最新更新