0

我想修改下面给出的现有 SQL 查询(编号 1)。首先,我需要知道是否可以做我想做的事情(第 2 点)。如果是,那么我可以通过哪些方式做到这一点。

--**(Number 1)** What I currently have      

SELECT  Column1
    FROM  dbo.Table1
    WHERE ColumnValue1 = 'One'
    AND scalarFunctionResult(Var) = 'Word1'
    AND ColumnValue2 = "Word2"


--*(Number 2)* What I need      
SELECT  Column1
    FROM  dbo.Table1
    WHERE ColumnValue1 = 'One'
    AND SomeVariable IN (scalarFunctionResult(Var) = 'Word1', 'WordA')
-- Perform a query based 
--on value of SomeVariable, Please read pseudo code below 
    AND ColumnValue2 = "Word2"

--Pseudo code for logic
--if(SomeVariable = 'Word1') then run query ABCD,
--else if(SomeVariable = 'WordA') then run some other query EFGH
4

2 回答 2

3

这是一个简单的OR

SELECT Column1
  FROM dbo.Table1
 WHERE ColumnValue1 = 'One'
   AND ((@SomeVariable = 'Word1' AND <the condition ABCD>)
        OR
        (@SomeVariable = 'WordA' AND <the condition EFGH>))

实际上,您可以从 ABCD when@Somevariable = 'Word1'或从其他条件“查询”EFGH when获得结果@Somevariable = 'WordA'

例如

SELECT Column1
  FROM dbo.Table1
 WHERE ColumnValue1 = 'One'
   AND ((dbo.scalarFunctionResult(@SomeVariable) = 'Word1' AND ColumnValue2 = "Word2")
        OR
        (dbo.scalarFunctionResult(@SomeVariable) = 'WordA' AND ColumnValue3 = "Word3"))
于 2013-04-02T04:01:46.510 回答
0

您是否想弄清楚如何在 SQL 中创建 IF 语句?不确定我是否正确理解了您的问题。

如果是这样,这里是MSDN的参考。正如 RichardTheKiwi 所展示的那样,在这种情况下你可能不需要它。

于 2013-04-02T08:33:44.937 回答