martinjw / dbschemareader

Read database metadata (from SqlServer/Oracle/MySql/SQLite/PostgreSql/DB2 etc) into one simple model
Microsoft Public License
293 stars 127 forks source link

DdlGeneratorFactory use ‘GO’ instead of semicolon to terminate statements in same file #168

Closed whibdev closed 1 year ago

whibdev commented 1 year ago

I’m using the DdlGeneratorFactory to generate table scripts from an existing SqlServer (2023) datbase and saving them in separate files in a sql database project (sqlproj). Sample output of one table script looks like this:

CREATE TABLE [dbo].[Pet]
(
  [Id] INT IDENTITY(1,1) NOT NULL,
  [Name] NVARCHAR (50)  NOT NULL,
  [EnteredOn] SMALLDATETIME  NOT NULL,
  [EnteredBy] INT  NOT NULL
);
ALTER TABLE [dbo].[Pet] ADD CONSTRAINT [PK__Pet__3214EC079445C894] PRIMARY KEY ([Id]);

When I use the ‘dotnet build’ command to compile the project to a dacfac file, I get a build error due to invalid syntax.

 Build error SQL71006: Only one statement is allowed per batch. A batch separator, such as 'GO', might be required between statements.

The issue is caused by using a semicolon to terminate a statement, instead of a ‘GO’ command, between statements in the same file. When I manually edit the generated table files output (as follows) by adding a ‘GO’ command between the statements, the build works.

CREATE TABLE [dbo].[Pet]
(
  [Id] INT IDENTITY(1,1) NOT NULL,
  [Name] NVARCHAR (50)  NOT NULL,
  [EnteredOn] SMALLDATETIME  NOT NULL,
  [EnteredBy] INT  NOT NULL
);
GO
ALTER TABLE [dbo].[Pet] ADD CONSTRAINT [PK__Pet__3214EC079445C894] PRIMARY KEY ([Id]);

Please note: The current output (without the ‘GO’ statement) parses and executes without error in SSMS.

Is there any way to optionally add a GO statement as described to avoid the 'dotnet build' errors?

martinjw commented 1 year ago
var ddlGeneratorFactory = new DdlGeneratorFactory(SqlType.SqlServer);
//as now
var ddl = ddlGeneratorFactory.AllTablesGenerator(schema).Write();

//new option- only works for SqlServer
 ddlGeneratorFactory.UseGranularBatching = true;
 var dllWithBatching = ddlGeneratorFactory.AllTablesGenerator(schema).Write();
martinjw commented 1 year ago

Pre-release here https://github.com/martinjw/dbschemareader/releases/tag/2.8.2.2

whibdev commented 1 year ago

The update resolves the issue

whibdev commented 1 year ago

Do you have a publish timeline for this update?

martinjw commented 1 year ago

It's released and on nuget.