Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 mulitple conditional statements in SP

Author  Topic 

PeterG
Posting Yak Master

156 Posts

Posted - 2002-04-29 : 19:18:47
What is the equivalent of a select...case VB statement in T-SQL? I've looked at CASE statement, but it is not what I'm looking for. What I want to do is this:

declare @varA int, @varB varchar(12)
Select @varA = fieldname from tablename;

if @varA = 1
then @varB = "Variable A"
else if @varA = 2
then @varB = "Variable B"
else if @varA = 3
then @varB = "Variable C"
.... and so on until @varA = 10

I could have 10 if statements if I want to, but can I use a cleaner syntax like the select...case block?

motokevin
Starting Member

36 Posts

Posted - 2002-04-29 : 19:38:16
I think CASE may work the way you want it to. Run this code and play with the value of @MyVar2 and you will see how it works.

Declare @MyVar1 Varchar(5)
Declare @MyVar2 Integer
Set @MyVar2 = 3

Set @MyVar1 = Case @MyVar2
WHEN 1 THEN 'one'
WHEN 2 THEN 'two'
WHEN 3 THEN 'three'
WHEN 4 THEN 'four'
ELSE 'Big'
END

Select @MyVar1 As TheResult

Go to Top of Page

byrmol
Shed Building SQL Farmer

1591 Posts

Posted - 2002-04-29 : 21:41:27
Peter,

We don't even have to bother with a CASE statement...


DECLARE @varA int, @varB VARCHAR(12)
SET @varA = 4
Select @varB = 'Variable ' + UPPER(CHAR(96 + @varA))
Select @varB


HTH

DavidM

Tomorrow is the same day as Today was the day before.
Go to Top of Page
   

- Advertisement -