|
-
Dec 13th, 2007, 06:24 AM
#1
Thread Starter
Just Married
[RESOLVED] Case Statement
Hi all
I am facing some problem in the following please tell me that what is wrong.
Code:
DECLARE @TableName nvarchar(Max),@Abbreviation nvarchar(100)
SET @Abbreviation='HH'
select CASE @Abbreviation
WHEN 'HH' THEN SET @TableName='Hotel_History'
WHEN 'HA' THEN SET @TableName='Hotel_Accomodation'
WHEN 'HD' THEN SET @TableName='Hotel_Dining'
WHEN 'HB' THEN SET @TableName='Hotel_Banquet_Conference'
WHEN 'HF' THEN SET @TableName='Hotel_Facilities_Services'
WHEN 'HV' THEN SET @TableName='Hotel_Virtual_Tour'
WHEN 'HG' THEN SET @TableName='Hotel_Gallery'
ELSE SET @TableName='NotMatch'
END
PRINT @TableName
Thanks
-
Dec 13th, 2007, 08:32 AM
#2
Re: Case Statement
Case if not a logic-flow type of statement.
Although it certainly appears to be one - it's not at all like the VB SELECT/CASE.
It's really an in-line scalar function - so it's used like this instead.
Code:
DECLARE @TableName nvarchar(Max),@Abbreviation nvarchar(100)
SET @Abbreviation='HH'
Set @TableName=CASE @Abbreviation
WHEN 'HH' THEN 'Hotel_History'
WHEN 'HA' THEN 'Hotel_Accomodation'
WHEN 'HD' THEN 'Hotel_Dining'
WHEN 'HB' THEN 'Hotel_Banquet_Conference'
WHEN 'HF' THEN 'Hotel_Facilities_Services'
WHEN 'HV' THEN 'Hotel_Virtual_Tour'
WHEN 'HG' THEN 'Hotel_Gallery'
ELSE 'NotMatch'
END
PRINT @TableName
It's more like the IIF() function in VB6.
[edit]And it's always great to have an ELSE. CASE without an ELSE is non-deterministic - meaning that it can return a NULL. Non-deterministic expressions can causes issues in strange places.
We never write a CASE/WHEN that doesn't have an ELSE [/edit]
Last edited by szlamany; Dec 13th, 2007 at 08:56 AM.
-
Dec 13th, 2007, 08:42 AM
#3
Thread Starter
Just Married
Re: Case Statement
I make some modification your code and now it is working.
Code:
SELECT @TableName= CASE @Table_Abbreviation
WHEN 'HH' THEN 'Hotel_History'
WHEN 'HA' THEN 'Hotel_Accomodation'
WHEN 'HD' THEN 'Hotel_Dining'
WHEN 'HB' THEN 'Hotel_Banquet_Conference'
WHEN 'HF' THEN 'Hotel_Facilities_Services'
WHEN 'HV' THEN 'Hotel_Virtual_Tour'
WHEN 'HG' THEN 'Hotel_Gallery'
ELSE 'NotMatch'
END
Thanks
Last edited by shakti5385; Dec 13th, 2007 at 08:51 AM.
-
Dec 13th, 2007, 08:57 AM
#4
Re: [RESOLVED] Case Statement
oops - I had a stray SELECT - I have edited the post.
You could still use SET - since you simply filling a variable with a value.
-
Dec 13th, 2007, 11:49 PM
#5
Thread Starter
Just Married
Re: [RESOLVED] Case Statement
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|