如何在MSSQL中設置變量並在語句中進一步使用它?SQL case語句和使用返回值
set @DOT =((case when[Day 1] > 12 then ([Day 1] - 12) else 0 end)
+ (case when[Day 2] > 12 then ([Day 2] - 12) else 0 end)
+ (case when[Day 3] > 12 then ([Day 3] - 12) else 0 end)
+ (case when[Day 4] > 12 then ([Day 4] - 12) else 0 end)
+ (case when[Day 5] > 12 then ([Day 5] - 12) else 0 end)
+ (case when[Day 6] > 12 then ([Day 6] - 12) else 0 end)
+ (case when[Day 7] > 12 then ([Day 7] - 12) else 0 end))
case when [Timesheet Total] <= 40 and @DOT = 0 then 0
when [Timesheet Total] <= 40 and @DOT > 0 then @DOT
when [Timesheet Total] > 40 and @DOT = 0 then ([Timesheet Total] -40)
when [Timesheet Total] > 40 and @DOT > 0 and ([Timesheet Total] -40) > @DOT then ([Timesheet Total] -40)
when [Timesheet Total] > 40 and @DOT > 0 and ([Timesheet Total] -40) < @DOT then @DOT
else 0
end
在這種情況下,標量函數可能是最好的方法。 –