如何在vb.net的sql查询中插入if语句?

时间:2014-06-06 03:07:17

标签: sql vb.net

如何在查询中插入if / else?

这是我的sqlcommand查询:

 sqlcommand ("select Machine_Code,Machine_Name,Status from
 Machine_Master where '" & combolinecode.Text & "' = Line_Code" )

我想从 1 =>更改Status的值有效 0 =>不活跃

2 个答案:

答案 0 :(得分:1)

您可以在几乎所有数据库中使用ANSI标准case语句执行此操作:

select Machine_Code, Machine_Name,
       (case when Status = 1 then 'active' else 'not active' end) as status
from Machine_Master
where '" & combolinecode.Text & "' = Line_Code";

我担心如果你使用VBA,你可能会使用Access,它有自己的方式:

select Machine_Code, Machine_Name,
       iif(Status = 1, "active", "not active") as status
from Machine_Master
where '" & combolinecode.Text & "' = Line_Code";

答案 1 :(得分:0)

您可以使用case

select Machine_Code
     , Machine_Name
     , (case when Status = 1 then 'active' else 'not active' end) as Status
from Machine_Master 
where '" & combolinecode.Text & "' = Line_Code