Hacktoberfest 2026: the issues maintainers tagged for October, open and beginner-friendly. Browse Hacktoberfest issues

Sql170ScriptGenerator.GenerateScript removing required semicolon from BEGIN...END block within Stored Procedure creation

Open
#219 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
64/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Quiet
Tech stack
csharp, sql
Domain
backend, databases

Research direction

Start with the provided C# reproduction, especially TSql170Parser and Sql170ScriptGenerator.GenerateScript, and trace how the BEGIN...END statement is rendered when IncludeSemicolons is enabled. Reproduce the output and verify that the generated procedure preserves the semicolon required before the following WITH CTE, then confirm the formatted SQL executes successfully.

Written by the indexing model from the issue text.

Description

formatting

I attempted to format my T-SQL script using the GenerateScript method in the Sql170ScriptGenerator class. The following script was a test used to generate a stored procedure:

ORIGINAL:
CREATE PROCEDURE spDEBUG_ReleaseToolTestScript 
AS
IF EXISTS (SELECT 1) BEGIN;
	SELECT 1;
END;
WITH TEST AS (
	SELECT 1 AS One
)
SELECT *
FROM TEST;
GO
FORMATTED:
CREATE PROCEDURE spDEBUG_ReleaseToolTestScript
AS
IF EXISTS (SELECT 1)
    BEGIN
        SELECT 1;
    END
WITH   TEST 
AS     (SELECT 1 AS One)
SELECT *
FROM   TEST ;

Running the formatted SQL script will throw the following error in T-SQL:
Msg 319, Level 15, State 1, Line 88 Incorrect syntax near the keyword 'with'. If this statement is a common table expression, an xmlnamespaces clause or a change tracking context clause, the previous statement must be terminated with a semicolon.

Code to reproduce:

using Microsoft.SqlServer.TransactSql.ScriptDom;
using System.Diagnostics;

var sql = @"
CREATE PROCEDURE spDEBUG_ReleaseToolTestScript 
AS
IF EXISTS (SELECT 1) BEGIN;
	SELECT 1;
END;
WITH TEST AS (
	SELECT 1 AS One
)
SELECT *
FROM TEST;
GO";

Console.WriteLine("ORIGINAL:" + sql);

var reader = new StringReader(sql);
var parser = new TSql170Parser(true);

var fragment = (TSqlScript)parser.Parse(reader, out var errors);

if (errors.Count > 0)
{
    Console.WriteLine("Failed to parse SQL script:");
    foreach (var error in errors)
    {
        Console.WriteLine(error.Message);
    }

    Debugger.Break();
}

var options = new SqlScriptGeneratorOptions
{
    IncludeSemicolons = true,
    IndentationSize = 4
};
var generator = new Sql170ScriptGenerator(options);

generator.GenerateScript(fragment.Batches[0].Statements[0], out var formattedSql);

Console.WriteLine();
Console.WriteLine("FORMATTED:" + Environment.NewLine + formattedSql);
Console.ReadLine();
Dominant language
GAP
Stars
277
Forks
43
Avg merge
6d 17h
Merged PRs (30d)
3

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from microsoft/SqlScriptDOM

All issues in microsoft/SqlScriptDOM

Similar issues

More Backend & API Design issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.