Results 1 to 5 of 5

Thread: [RESOLVED] Case Statement

  1. #1

    Thread Starter
    Just Married shakti5385's Avatar
    Join Date
    Mar 2006
    Location
    Udaipur,Rajasthan(INDIA)
    Posts
    3,747

    Resolved [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

  2. #2
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    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.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  3. #3

    Thread Starter
    Just Married shakti5385's Avatar
    Join Date
    Mar 2006
    Location
    Udaipur,Rajasthan(INDIA)
    Posts
    3,747

    Thumbs up 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.

  4. #4
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    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.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  5. #5

    Thread Starter
    Just Married shakti5385's Avatar
    Join Date
    Mar 2006
    Location
    Udaipur,Rajasthan(INDIA)
    Posts
    3,747

    Re: [RESOLVED] Case Statement

    Thanks for gerat help.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width