我的SQL代码中存在这个问题:
我只会显示我的WHERE
条款,因为它有点长,
就是这样:
where
((@account_status = 1027 AND a.AccountStatus = 1027 AND a.FolioNo =
@folio_no AND b.ReservationNo = @reservation_id)) OR
((@account_status = 1026 AND a.AccountStatus = 1026 AND a.FolioNo =
@folio_no AND b.ReservationNo = @reservation_id)) OR
((@account_status = 1025 AND a.AccountStatus = 1025 AND @trans_code = 1 AND
a.AccountStatementTransCode = 1 AND b.FolioNo = @folio_no AND
b.ReservationNo = @reservation_id)) OR
((@account_status = 1025 AND a.AccountStatus = 1025 AND @trans_code != 1
AND a.AccountStatementTransCode != 1 AND b.FolioNo = @folio_no AND
b.ReservationNo = @reservation_id)) OR
((a.FolioNo = @folio_no AND b.ReservationNo = @reservation_id AND
@trans_code = 2 AND a.AccountStatementTransCode = 2 AND
case
when @sub_category = 14 then i.category_id is null
else i.category_id = @sub_category
end )) OR
((a.FolioNo = @folio_no AND b.ReservationNo = @reservation_id AND
@trans_code = 3 AND a.AccountStatementTransCode = 3 AND i.category_id =
@sub_category)) OR
((a.FolioNo = @folio_no AND b.ReservationNo = @reservation_id AND
@trans_code = 4 AND a.AccountStatementTransCode = 4 AND i.category_id =
@sub_category)) OR
((a.FolioNo = @folio_no AND b.ReservationNo = @reservation_id AND
@trans_code = 5 AND a.AccountStatementTransCode = 5 AND i.category_id =
@sub_category)) OR
((a.FolioNo = @folio_no AND b.ReservationNo = @reservation_id AND
@trans_code = 6 AND a.AccountStatementTransCode = 6 AND i.category_id =
@sub_category))
我希望如果输入为@sub_category = 14
,那么它将返回包含NULL
值的类别,否则它将返回@sub_category
值。
我怎么能这样做?
答案 0 :(得分:4)
用下面的
替换CASE表达式逻辑AND
1 = CASE WHEN @sub_category = 14 AND i.category_id is null
THEN 1
WHEN @sub_category <> 14 AND i.category_id = @sub_category
THEN 1
ELSE 0
END
答案 1 :(得分:1)
将您的CASE
更改为:
case
when @sub_category = 14 then null
else i.category_id = @sub_category
end
答案 2 :(得分:0)
尝试:简单来说,如果category_id
永远不会暂停-1
,或者您可以使用永远不会在category_id
中的值,即{{1 }}
0,-1