我创建了这个UDF
CREATE FUNCTION [dbo].[HasExtendedRetentionSamples] (@BoxNumber varchar(20))
RETURNS int
AS
BEGIN
declare @cnt int
set @cnt = 0
select @cnt = (select count(*)
from tFreezerBoxInfo bi
inner join tFreezerDetails fd on fd.boxTrayId = bi.boxTrayId
inner join tncDrugTestListNew dt on dt.labnumber = fd.labnumber
inner join ExtendedRetentionSites a on dt.number = a.number
where boxnumber = 'ND011811001'
and
case isnull([retention],0)
when 0 then proposedDestructionDate
else dateadd(dd,abs([retention]),proposedDestructionDate)
end <> proposedDestructionDate)
return @cnt
END
执行UDF时
select dbo.[HasExtendedRetentionSamples] ('ND011811001')
返回值0,这是不正确的,
当我执行UDF中包含的SQL语句时(将@BoxNumber替换为'ND011811001')...
select count(*)
from tFreezerBoxInfo bi
inner join tFreezerDetails fd on fd.boxTrayId = bi.boxTrayId
inner join tncDrugTestListNew dt on dt.labnumber = fd.labnumber
inner join ExtendedRetentionSites a on dt.number = a.number
where boxnumber = 'ND011811001'
and
case isnull([retention],0)
when 0 then proposedDestructionDate
else dateadd(dd,abs([retention]),proposedDestructionDate)
end <> proposedDestructionDate
返回值5,这是正确的。
所以最重要的问题是为什么????
连接中的所有数据类型&amp;案例陈述是一样的。
答案 0 :(得分:0)
我会将函数更改为this,从子查询中取出计数:
CREATE FUNCTION [dbo].[HasExtendedRetentionSamples] (@BoxNumber varchar(20))
RETURNS int
AS
BEGIN
declare @cnt int
set @cnt = 0
select @cnt = count(*)
from tFreezerBoxInfo bi
inner join tFreezerDetails fd on fd.boxTrayId = bi.boxTrayId
inner join tncDrugTestListNew dt on dt.labnumber = fd.labnumber
inner join ExtendedRetentionSites a on dt.number = a.number
where boxnumber = 'ND011811001'
and
case isnull([retention],0)
when 0 then proposedDestructionDate
else dateadd(dd,abs([retention]),proposedDestructionDate)
end <> proposedDestructionDate
return @cnt
END
尝试将您的CASE
语句更改为:
case
when isnull([retention],0) = 0 then proposedDestructionDate
else dateadd(dd,abs([retention]),proposedDestructionDate)
end <> proposedDestructionDate
答案 1 :(得分:0)
我很尴尬地说,我发现了为什么会这样......
当我创建表ExtendedRetentionSites时,它被创建了&amp;作为主人填充我。我意识到并以DBO作为所有者重新创建了表,填充了这个表,但是从不丢弃与我拥有的名称相同的表。我发了一个插入声明&amp;它插入到我拥有的ExtendedRetentionSites中,但是这些数据从未进入DBO拥有的表。
Soooooo当我运行select脚本时,它使用了我拥有的ExtendedRetentionSites,它有新行,它会给我我想要的结果。当我运行UDF时,它使用了DBO拥有的表,而没有新行,因此没有返回任何值或零计数。
感谢所有帮助我解决这个问题的人,我现在将我的脑袋从我的屁股中解脱出来。回去工作。
再次感谢所有!!!!