我想修改下面给出的现有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
答案 0 :(得分:3)
这是一个简单的OR
SELECT Column1
FROM dbo.Table1
WHERE ColumnValue1 = 'One'
AND ((@SomeVariable = 'Word1' AND <the condition ABCD>)
OR
(@SomeVariable = 'WordA' AND <the condition EFGH>))
实际上,您可以在@Somevariable = 'Word1'
时从ABCD获得结果,或在@Somevariable = 'WordA'
时从其他条件“查询”EFGH获得结果。
e.g。
SELECT Column1
FROM dbo.Table1
WHERE ColumnValue1 = 'One'
AND ((dbo.scalarFunctionResult(@SomeVariable) = 'Word1' AND ColumnValue2 = "Word2")
OR
(dbo.scalarFunctionResult(@SomeVariable) = 'WordA' AND ColumnValue3 = "Word3"))
答案 1 :(得分:0)
您是否正在尝试弄清楚如何在SQL中创建IF语句?我不确定我是否理解你的问题。
如果是这样,这里是来自MSDN的引用。在这种情况下你可能不需要它,尽管RichardTheKiwi表示。