我建立這個UDFUDF不返回相同的值選擇包含在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語句(用'ND011811001'代替@BoxNumber)時...
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,這是正確的。
所以最大的問題是WHY?
連接& case語句中的所有數據類型都是相同的。
我注意到你的UDF使用的是硬編碼的BoxNumber值,而不是你傳入的參數。這可能沒有什麼區別,但它肯定是你在聲明函數「完成」之前想要改變的東西。 – 2012-03-15 21:11:28