开发者

How can I write the following query in an executable way in SQL Server Express 2008

select doc_no,date,acc_no,amount,
if xvar=1
(select acc_head from my_crem 开发者_如何学Pythonc where c.acc_no=o.acc_no) as acc_head
else
(select acc_head from my_debm c where c.acc_no=o.acc_no) as acc_head
from my_ordm o;

In the above query, xvar is not a column name. It's a local variable name. Then how can I write the above query?


select 
    o.doc_no,
    o.date,
    o.acc_no,
    o.amount, 
    CASE 
         WHEN @xvar = 1 THEN c.acc_head 
         ELSE d.acc_head
    END as acc_head
from 
    my_ordm o
    left join my_crem c on c.acc_no = o.acc_no   
    left join my_debm d on d.acc_no = o.acc_no;


select
    doc_no,
    date,
    acc_no,
    amount,
    Case @xvar 
           WHEN 1 Then (select acc_head from my_crem c where c.acc_no = o.acc_no)
           ELSE (select acc_head from my_debm c where c.acc_no = o.acc_no)
    END AS acc_head 
from my_ordm o;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜