Results 1 to 7 of 7
  1. #1
    Join Date
    Aug 2003
    Posts
    111

    Unanswered: IF {ELSE IF} Construct

    Hi

    Since there is no IF {ELSE IF} constructs in TSQL, I assume the following will do the equivalent of ELSE IF, please verify. Thanx


    IF condition
    BEGIN
    -- some TSQL
    END
    ELSE IF condition
    BEGIN
    -- some TSQL
    END
    ELSE
    BEGIN
    -- some TSQL
    END


    James

  2. #2
    Join Date
    Aug 2003
    Posts
    111
    Please comment on any deviation from standard programming that this IF ELSE IF construct may introduce. I am too novice to see it.

    Cheers

    James

  3. #3
    Join Date
    Jun 2003
    Location
    Ohio
    Posts
    12,592
    Provided Answers: 1
    This should work, but your code will be more readable if you can use a CASE statement instead.

    blindman

  4. #4
    Join Date
    Aug 2003
    Posts
    111
    true
    but isn't IF and ELSE more efficient than CASE
    I assume that because what i said is true in general programming

    cheers
    james

  5. #5
    Join Date
    Jun 2003
    Location
    Ohio
    Posts
    12,592
    Provided Answers: 1
    For a single criteria IF ELSE is probably more efficient, but when you start nesting IF statements I doubt there is any difference. The db engine has to make the same logical comparisons in either case.

    Evaluation of a CASE statement completes as soon as a match is found, and further criteria are not considered. I'm not sure if this is true of nested IF/ELSE statements; ie, the optimizer may evaluate the entire statement. Perhaps someone else on the forum knows how the optimizer handles this scenario.

    Truth is, neither of these is a very fast operation when performed against large tables. You gotta do what you gotta do.

    blindman

  6. #6
    Join Date
    Feb 2002
    Location
    Houston, TX
    Posts
    809

    Re: IF {ELSE IF} Construct

    Originally posted by nano_electronix
    Hi

    Since there is no IF {ELSE IF} constructs in TSQL, I assume the following will do the equivalent of ELSE IF, please verify. Thanx


    IF condition
    BEGIN
    -- some TSQL
    END
    ELSE IF condition
    BEGIN
    -- some TSQL
    END
    ELSE
    BEGIN
    -- some TSQL
    END


    James
    I think this is the correct solution. Lets say "condition" refers to weekend day, a holiday or a week day flag and "TSQL" refers to three totaly diffrent queries. Your code would be resonable.

    Now lets say "TSQL" is identical except for the treatment of a date column and all you want is the words "Holiday", "Weekend" or "Weekday" returned in your query, a CASE statment might be the better choice.

    Clear as mud?
    Paul Young
    (Knowledge is power! Get some!)

  7. #7
    Join Date
    Aug 2003
    Posts
    111
    agree

Posting Permissions

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