早上好。我遇到查詢問題。我想在查詢中選擇某些內容,但前提是其他字段是有意義的。下面的查詢將更好地解釋如果語句在sql查詢中
select
Case isnull(rl.reason,'Not Found')
When 'D' then 'Discontinued'
When 'N' then 'Not Found'
When 'I' then 'Inactive'
When 'C' then 'No Cost'
When '' then 'Not Found'
End as Reason, ***If statement to select pv.descriptor only if reason is in ('D','I','C')***pv.descriptor
from table1 as rl
left join table2 as v on v.field= rl.field
***Here i want an if statment to run if reason is in ('D','I','C')***
left join table3 as pv on
Case rl.scantype
when 'S' then cast(ltrim(rtrim(pv.field#1)) as varchar)
when 'U' then cast(ltrim(rtrim(pv.field#2)) as varchar)
when 'V' then cast(ltrim(rtrim(pv.vfield#3)) as varchar)
end
= rl.scan and pv.vend_no = rl.vendnum
***'**If statement ends*****
left join storemain..prmastp as p on p.emuserid = rl.userid
where rl.scandate between GetDate() -7 and GetDate() order by rl.scandate desc
我想if語句只選擇如果選擇的理由是「d」,「我」,or'C」的描述符。如果不是,我想要一個空值,因爲除非原因是'D','I','C',否則我不會做這個變量。順便說一下,我可以使用case語句,其中我在左連接的中間使用它。它工作得很好。這不是我的問題。
查詢並沒有真正更好地解釋它。 – 2009-08-18 14:43:42
您無法在SQL中使用CASE進行流程控制。 – PhilPursglove 2009-08-18 14:49:40
所以我們已經確定了什麼不是問題。什麼是問題? – Eric 2009-08-18 14:55:13