microsoft / SqlScriptDOM

ScriptDOM/SqlDOM is a .NET library for parsing T-SQL statements and interacting with its abstract syntax tree
MIT License
128 stars 13 forks source link

ScriptDom parser for Sql Server 2022 fails to parse exec variable with version #27

Open Turyansky opened 1 year ago

Turyansky commented 1 year ago

Steps to Reproduce:

  1. Create a new C# Console Project
  2. Add latest Nuget package for Microsoft.SqlServer.DacFx (161.6367.0-preview)
  3. Run code below
using Microsoft.SqlServer.TransactSql.ScriptDom;

var parser = new TSql160Parser(true); // Sql server 2022 parser

IList<ParseError> errors;

parser.Parse(new StringReader("CREATE PROCEDURE Proc1 AS EXEC ProcName;2"), out errors);
// No parsing errors

parser.Parse(new StringReader("CREATE PROCEDURE Proc1 AS DECLARE @ProcName SYSNAME = 'Proc2' EXEC @ProcName"), out errors);
// No parsing errors

parser.Parse(new StringReader("CREATE PROCEDURE Proc1 AS DECLARE @ProcName SYSNAME = 'Proc2' EXEC @ProcName;2"), out errors);
// Parsing error! "Incorrect syntax near ;." This should parse succesfully

Did this occur in prior versions? If not - which version(s) did it work in? Not work in all prior versions.

(DacFx/SqlPackage/SSMS/Azure Data Studio)

barbaravaldez commented 1 year ago

Hi @Turyansky, thank you for submitting this issue. I couldn't find documentation of variable versioning in SQL Server. Can you please clarify the issue? What does ";2" mean in this case?

poizan42 commented 11 months ago

AFAICT it is equivalent to

CREATE PROCEDURE Proc1 AS
BEGIN
    DECLARE @ProcName SYSNAME = 'Proc2'
    EXEC @ProcName;2
END

The ";2" is the "procedure group" functionality, see ";number" at https://learn.microsoft.com/en-us/sql/t-sql/language-elements/execute-transact-sql?view=sql-server-ver16