[英]Sql Server (breaking sting and doing operations in that order)
我將@Order的varchar值設置為'ST,OT,DT'或'OT,ST,DT'等,並帶有ST,OT,DT的所有可能組合。
然后我想在存儲過程中實現以下邏輯:@order ='ST,OT,DT'如果ST首先出現,則從@SThours中扣除,如果@SThours為零,則從@OThours中扣除,如果@OThours為零,則扣除來自@DThours
我可以繼續寫所有可能的組合的其他條件..但是想要有一個很好的實現,以便將來如果有新的價值,我就不需要更改代碼了……
您可以執行以下操作:
declare @valueToDeductFrom int, @currentIndex int, @currentFieldName char(2)
set @valueToDeductFrom = 0
set @currentIndex = 1
while @currentIndex < 8 and @valueToDeductFrom = 0
begin
set @currentFieldName = substring(@order, @currentIndex, 2)
set @valueToDeductFrom =
case
when @currentFieldName = 'ST' then @SThours
when @currentFieldName = 'OT' then @OThours
when @currentFieldName = 'DT' then @DThours
else 0
end
set @currentIndex = @currentIndex + 3
end
-- do something with @valueToDeductFrom here... Probably deduct from it. :)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.