我們有一個接收4個變量並返回一個int結果的函數。 我們希望將這個函數用於表格中的每個記錄,而這4個變量是從每個記錄中提取的。如何求和sql中的循環中的函數?
我該如何解決?
我的功能:
CREATE FUNCTION dbo.occupiedDaysPerListingFunction(@CheckIn date, @CheckOut date, @Email varchar(80), @Title varchar(50))
RETURNS int
AS
BEGIN
DECLARE @nightsInRange int
select @nightsInRange= (CASE
WHEN DATEDIFF(day,@CheckIn,getdate()) >0 and DATEDIFF(day,@CheckOut,getdate())+100 <0 THEN 100
WHEN DATEDIFF(day,@CheckIn,getdate()) >0 and DATEDIFF(day,@CheckOut,getdate())+100 >0 THEN DATEDIFF(day,getdate(),@CheckOut)
WHEN DATEDIFF(day,getdate(),@CheckIn) >0 and DATEDIFF(day,getdate(),@CheckIn) <100 and DATEDIFF(day,@CheckOut,getdate())+100 >0 THEN DATEDIFF(day,@CheckIn,@CheckOut)
WHEN DATEDIFF(day,getdate(),@CheckIn) >0 and DATEDIFF(day,getdate(),@CheckIn) <100 and DATEDIFF(day,@CheckOut,getdate())+100 <0 THEN DATEDIFF(day,@CheckIn,getdate())+100
WHEN DATEDIFF(day,@CheckIn,getdate()) <0 THEN 0
END)
FROM [dbo].[ORDERS] o
WHERE o.[E-mail] = @Email and o.[title][email protected]
RETURN @nightsInRange
end
您的問題的標題提到「總和」,但沒有出現在問題中。爲什麼不? – HABO