Results 1 to 2 of 2
  1. #1
    Join Date
    Dec 2001
    Location
    Canada
    Posts
    45

    Red face Unanswered: case when -- fatest method --

    What is the best pratice

    SELECT CASE
    WHEN @MyVar = 1 THEN col1
    WHEN @MyVar = 2 THEN col2
    WHEN @MyVar = 3 THEN col3
    WHEN @MyVar = 4 THEN col4
    END as Result
    FROM table1

    OR

    SELECT CASE @MyVar
    WHEN 1 THEN col1
    WHEN 2 THEN col2
    WHEN 3 THEN col3
    WHEN 4 THEN col4
    END as Result
    FROM table1

    ??

    Does a difference exist?

    Thanks
    Franky
    FBoucheros@hotmail.com

  2. #2
    Join Date
    Jun 2003
    Location
    Ohio
    Posts
    12,592
    Provided Answers: 1
    Hmmm...the second is better in my opinion just because it is more readable. It's also possible that the SQL Server will check the @MyVar value four times in the first example, and only one time in the second. So the second may be faster, though its also possible that the optimizer may look at the first example, realize it is equivalent to the second example, and effectively convert it. Remember that it is not your SQL code that actually gets executed, but the compiled process that the optimizer creates from it.

    blindman

Posting Permissions

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