如何在sql server 2008中使用存储过程中的if比较两个字符串?

时间:2022-03-09 15:53:25

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 Measuretable

4 个解决方案

#1


18  

Two things:

两件事情:

  1. Only need one (1) equals sign to evaluate
  2. 只需要一(1)个等号来评估
  3. You need to specify a length on the VARCHAR - the default is a single character.
  4. 您需要在VARCHAR上指定长度 - 默认值是单个字符。

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 -

VARCHAR(10)表示VARCHAR最多可容纳10个字符。更多行为的例子 -

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".

......将返回“否”。

#2


1  

declare @temp as varchar
  set @temp='Measure'
  if(@temp = 'Measure')
Select Measure from Measuretable
else
Select OtherMeasure from Measuretable

#3


1  

What you want is a SQL case statement. The form of these is either:

你想要的是一个SQL case语句。这些形式是:

  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

#4


1  

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'

#1


18  

Two things:

两件事情:

  1. Only need one (1) equals sign to evaluate
  2. 只需要一(1)个等号来评估
  3. You need to specify a length on the VARCHAR - the default is a single character.
  4. 您需要在VARCHAR上指定长度 - 默认值是单个字符。

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 -

VARCHAR(10)表示VARCHAR最多可容纳10个字符。更多行为的例子 -

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".

......将返回“否”。

#2


1  

declare @temp as varchar
  set @temp='Measure'
  if(@temp = 'Measure')
Select Measure from Measuretable
else
Select OtherMeasure from Measuretable

#3


1  

What you want is a SQL case statement. The form of these is either:

你想要的是一个SQL case语句。这些形式是:

  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

#4


1  

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'