15

私はこのようなことをしたい:

declare @temp as varchar
    set @temp='Measure'

if(@temp == 'Measure')
   Select Measure from Measuretable
else
   Select OtherMeasure from Measuretable
4

4 に答える 4

25

2つのこと:

  1. 評価に必要な等号は 1 つだけです
  2. VARCHAR で長さを指定する必要があります。デフォルトは 1 文字です。

使用する:

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'

...「いいえ」を返します。

于 2010-05-20T20:33:21.203 に答える
1

必要なのはSQLのcaseステートメントです。これらの形式は次のいずれかです。

  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
于 2010-05-21T03:12:00.933 に答える
1
declare @temp as varchar
  set @temp='Measure'
  if(@temp = 'Measure')
Select Measure from Measuretable
else
Select OtherMeasure from Measuretable
于 2010-05-20T20:32:01.580 に答える
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'
于 2016-05-03T06:39:56.447 に答える