我想做这样的事情:
declare @temp as varchar
set @temp='Measure'
if(@temp == 'Measure')
Select Measure from Measuretable
else
Select OtherMeasure from Measuretable
答案 0 :(得分:19)
两件事:
使用:
DECLARE @temp VARCHAR(10)
SET @temp = 'm'
IF @temp = 'm'
SELECT 'yes'
ELSE
SELECT 'no'
VARCHAR(10)
表示VARCHAR最多可容纳10个字符。行为的更多例子 -
DECLARE @temp VARCHAR
SET @temp = 'm'
IF @temp = 'm'
SELECT 'yes'
ELSE
SELECT 'no'
...将返回“是”
DECLARE @temp VARCHAR
SET @temp = 'mtest'
IF @temp = 'm'
SELECT 'yes'
ELSE
SELECT 'no'
...将返回“否”。
答案 1 :(得分:1)
declare @temp as varchar
set @temp='Measure'
if(@temp = 'Measure')
Select Measure from Measuretable
else
Select OtherMeasure from Measuretable
答案 2 :(得分:1)
您想要的是SQL案例陈述。 这些形式是:
select case [expression or column]
when [value] then [result]
when [value2] then [result2]
else [value3] end
或:
select case
when [expression or column] = [value] then [result]
when [expression or column] = [value2] then [result2]
else [value3] end
在你的例子中,你是:
declare @temp as varchar(100)
set @temp='Measure'
select case @temp
when 'Measure' then Measure
else OtherMeasure end
from Measuretable
答案 3 :(得分:1)
您也可以尝试使用匹配字符串。
DECLARE @temp1 VARCHAR(1000)
SET @temp1 = '<li>Error in connecting server.</li>'
DECLARE @temp2 VARCHAR(1000)
SET @temp2 = '<li>Error in connecting server. connection timeout.</li>'
IF @temp1 like '%Error in connecting server.%' OR @temp1 like '%Error in connecting server. connection timeout.%'
SELECT 'yes'
ELSE
SELECT 'no'