locked
Isn't an empty BEGIN END block legal? RRS feed

  • Question

  • Hey everyone,

    This is puzzling me. The code below does not work:

    if (1 = 1)
    begin
    --select 'hello2'
    end
    else
    begin
    select 'hello'
    end
    


    as it returns the following error:

    Msg 156, Level 15, State 1, Line 5

    Incorrect syntax near the keyword 'else'.

    Msg 102, Level 15, State 1, Line 8

    Incorrect syntax near 'end'.



    The following DOES work:

    if (1 = 1)
    begin
    select 'hello2'
    end
    else
    begin
    select 'hello'
    end


    This implies you can't have an empty BEGIN END block. I thought this was legal?
    Wednesday, May 13, 2009 3:20 AM

Answers


  • A BEGIN and END statement block must contain at least one Transact-SQL statement.

    see http://msdn.microsoft.com/en-us/library/ms189079(sql.90).aspx

    KH Tan
    • Marked as answer by FireMyst Wednesday, May 13, 2009 3:29 AM
    Wednesday, May 13, 2009 3:24 AM

All replies


  • A BEGIN and END statement block must contain at least one Transact-SQL statement.

    see http://msdn.microsoft.com/en-us/library/ms189079(sql.90).aspx

    KH Tan
    • Marked as answer by FireMyst Wednesday, May 13, 2009 3:29 AM
    Wednesday, May 13, 2009 3:24 AM
  • Thanks for the reference. Was trying to find it before posting.

    I find this annoying because as a developer if we're working on a massive stored proc and need to add in a new if-else statement, and do each section one at a time to test, I should be allowed to leave the block empty as a sort of "place holder".
    Wednesday, May 13, 2009 3:31 AM
  • agreed. this is a bit annoying
    KH Tan
    Wednesday, May 13, 2009 3:34 AM