Skip to content

Syntax error in VSCode editor when using IF...ELSE blocks with THROW inside a TRY...CATCH block in a SQL stored procedure. #18669

Open
@Romaniego1977

Description

@Romaniego1977
  • MSSQL Extension Version: 1.28.0
  • VSCode Version: 1.97.1
  • OS Version: Windows 10

Error Description
The VSCode editor shows the error "Incorrect syntax near 'ELSE'. Expecting CATCH, CONVERSATION, or TRY" when analyzing a SQL stored procedure that contains IF...ELSE blocks with a THROW statement inside a TRY...CATCH block. However, the SQL code executes correctly on the SQL Server.

Steps to Reproduce the Error

  1. Open VSCode and connect to a SQL Server.
  2. Create a new .sql file or open an existing one.
  3. Copy and paste the following generic SQL code into the file:
CREATE PROCEDURE dbo.MyProcedure
    @parameter INT
AS
BEGIN
    SET NOCOUNT ON;
    BEGIN TRY
        IF @parameter = 1
        BEGIN
            -- Code for case 1
            SELECT 1;
        END
        ELSE IF @parameter = 2
        BEGIN
            -- Code for case 2
            THROW 50000, 'Error in case 2', 1; -- THROW statement
        END
        ELSE
        BEGIN
            -- Code for default case
            SELECT 3;
        END;
    END TRY
    BEGIN CATCH
        SELECT ERROR_MESSAGE() AS ErrorMessage;
    END CATCH
END;
  1. Observe the error that appears in the VSCode editor.

Expected Behavior
The VSCode editor should analyze the SQL code correctly and not display any syntax errors, as the code is valid and executes without problems on the SQL Server.

Actual Behavior
The VSCode editor displays the error "Incorrect syntax near 'ELSE'. Expecting CATCH, CONVERSATION, or TRY" on the line containing the ELSE keyword.

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions