I want to do something like this:
declare @temp as varchar
set @temp='Measure'
if(@temp == 'Measure')
Select Measure from Measuretable
else
Select OtherMeasure from Mea开发者_开发技巧suretable
Two things:
- Only need one (1) equals sign to evaluate
- You need to specify a length on the VARCHAR - the default is a single character.
Use:
DECLARE @temp VARCHAR(10)
SET @temp = 'm'
IF @temp = 'm'
SELECT 'yes'
ELSE
SELECT 'no'
VARCHAR(10)
means the VARCHAR will accommodate up to 10 characters. More examples of the behavior -
DECLARE @temp VARCHAR
SET @temp = 'm'
IF @temp = 'm'
SELECT 'yes'
ELSE
SELECT 'no'
...will return "yes"
DECLARE @temp VARCHAR
SET @temp = 'mtest'
IF @temp = 'm'
SELECT 'yes'
ELSE
SELECT 'no'
...will return "no".
You can also try this for match string.
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'
declare @temp as varchar
set @temp='Measure'
if(@temp = 'Measure')
Select Measure from Measuretable
else
Select OtherMeasure from Measuretable
What you want is a SQL case statement. The form of these is either:
select case [expression or column]
when [value] then [result]
when [value2] then [result2]
else [value3] end
or:
select case
when [expression or column] = [value] then [result]
when [expression or column] = [value2] then [result2]
else [value3] end
In your example you are after:
declare @temp as varchar(100)
set @temp='Measure'
select case @temp
when 'Measure' then Measure
else OtherMeasure end
from Measuretable
精彩评论