我在Microsoft SSMS中创建了一个标量值函数,如下所示:
CREATE FUNCTION [dbo].[fGetCurrentBalFromName]
(
@Name NVarchar,
@CliFl Bit
)
RETURNS Money
AS
BEGIN
DECLARE @CurrentBal Money
select @CurrentBal=SUM(tt.Debt) from
(select case t.Clientfl when 1 then dbo.fGetClientNameFromId(t.AgentID)
else dbo.fGetSupplierNameFromId(t.AgentID)
end as Cli,
t.Clientfl,
case t.Clientfl when 1 then
case t.Incomfl
when 1 then t.Amount
else (-1)*t.Amount
end
else
case t.Incomfl
when 1 then (-1)*t.Amount
else t.Amount
end
end as Debt
from [Store].[dbo].[Money] t) tt where tt.Cli=@Name and tt.Clientfl=@CliFl
group by tt.Cli
RETURN @CurrentBal
END
执行时
Select dbo.fGetCurrentBalFromName('Вали',1)
我得不到(NULL)结果。
但是当我尝试单独执行查询时没有参数
select SUM(tt.Debt) from
(select case t.Clientfl when 1 then dbo.fGetClientNameFromId(t.AgentID)
else dbo.fGetSupplierNameFromId(t.AgentID)
end as Cli,
t.Clientfl,
case t.Clientfl when 1 then
case t.Incomfl
when 1 then t.Amount
else (-1)*t.Amount
end
else
case t.Incomfl
when 1 then (-1)*t.Amount
else t.Amount
end
end as Debt
from [Store].[dbo].[Money] t) tt where tt.Cli='Вали' and tt.Clientfl=1
group by tt.Cli
我得到了我想要的东西。 那么我创建标量值函数的错误是什么呢。 任何帮助将不胜感激!
答案 0 :(得分:4)