I am testing a project in VS2012.
The database project is targeted to SQL Server 2012, and has a SQLCLR Target Framework of .NET Framework 4.5.
One of the table definitions looks like this:
CREATE TABLE [dbo].[adSources] (
[SourceID] INT NOT NULL, [ComplianceUnitID] INT NOT NULL, [Name] NVARCHAR (50) NOT NULL, [DataDomainID] INT NOT NULL, [IsDiscarded] BIT CONSTRAINT [DF_adSources_IsDiscarded] DEFAULT ((0)) NOT NULL, [TXID] INT NOT NULL, CONSTRAINT [PK_adSources] PRIMARY KEY CLUSTERED ([SourceID] ASC), CONSTRAINT [FK_adSources_adComplianceUnits] FOREIGN KEY ([ComplianceUnitID]) REFERENCES [dbo].[adComplianceUnits] ([ComplianceUnitID]), CONSTRAINT [FK_adSources_mdDataDomains] FOREIGN KEY ([DataDomainID]) REFERENCES [dbo].[mdDataDomains] ([DataDomainID]), CONSTRAINT [FK_adSources_tRoots] FOREIGN KEY ([SourceID]) REFERENCES [dbo].[tRoots] ([RootID])
);
When I build the database project from VS 2012, the project builds, no errors, producing TestDb.dacpac.
However, I want to programmatically build the .dacpac file using Microsoft.Build.Evaluation:
var databaseProject = ProjectCollection.GlobalProjectCollection.LoadProject(scioDbProjectPath);
bool databaseBuilt = databaseProject.Build(new[] { "Rebuild" });
When I execute this code in debug mode, the database builds successfully, but in the output window I see
"A first chance exception of type 'antlr.NoViableAltForCharException' occurred in Microsoft.SqlServer.TransactSql.ScriptDom.dll
While this is a one-time exception and no big deal, our larger project has many tables, and for some reason this exception slows the build time down significantly.
I did discover for this problem that removing the semi-colon from the CREATE TABLE statement actually made the problem go away. But I am having trouble finding any documents that say I shouldn't have the semicolon in place.
So I was able to workaround this problem by removing the semicolons from the SQL files (although I am not sure why I need to remove them at this point and would rather not).
However, in the same project there is a stored procedure definition:
CREATE PROCEDURE [dbo].[purge_tValidationErrors]
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON
DECLARE @SupercededRuns TABLE (
BatchRunID int)
-- get batches where a more recent batch has been run for that source
WITH ranked_batches AS (
SELECT BatchRunID,
-- we want to prioritize completed batches over failed batches (failed batches likely do not have any record level errors)
DENSE_RANK() OVER (
PARTITION BY BatchTypeID, SourceID
ORDER BY
CASE BatchStatusID
WHEN 2 THEN 0 -- successful completion (shared top priority)
WHEN 3 THEN 0 -- completion with warnings (shared top priority)
WHEN 4 THEN 1 -- total failure is bottom priority
END,
Start DESC) BatchRank
FROM tBatchRuns
-- exclude currently running batches (we don't want to remove old errors until totally replaced by new ones)
WHERE BatchStatusID != 1
)
INSERT INTO @SupercededRuns (BatchRunID)
SELECT BatchRunID
FROM ranked_batches
WHERE BatchRank != 1
DECLARE @CurrentBatch TABLE (
ValidationErrorID int
)
DECLARE @MaxErrorID int
DECLARE @Continue bit
SET @Continue = 1
-- Break the delete up into batches as one big delete will hose SQL Server
WHILE @Continue = 1
BEGIN
SELECT @MaxErrorID = MAX(ValidationErrorID) FROM @CurrentBatch
DELETE FROM @CurrentBatch
INSERT INTO @CurrentBatch (ValidationErrorID)
SELECT TOP 100 ValidationErrorID FROM tValidationErrors
WHERE BatchRunID IN (SELECT BatchRunID FROM @SupercededRuns)
AND ValidationErrorID > ISNULL(@MaxErrorID, 0)
ORDER BY ValidationErrorID
BEGIN TRAN
-- delete error data first
DELETE FROM tValidationErrorData
WHERE ValidationErrorID IN (
SELECT ValidationErrorID
FROM tValidationErrors
WHERE ValidationErrorID IN (SELECT ValidationErrorID FROM @CurrentBatch)
)
-- delete errors
DELETE FROM tValidationErrors
WHERE ValidationErrorID IN (SELECT ValidationErrorID FROM @CurrentBatch)
IF @@ROWCOUNT = 0
BEGIN
SET @Continue = 0
END
COMMIT
END
END
When my program builds the database project, in my debug window I see 21 occurrences of "A first chance exception of type 'antlr.MismatchedTokenException' occurred in Microsoft.SqlServer.TransactSql.ScriptDom.dll" in the debug window.
I am not sure what to change in the stored procedure file to keep these errors from happening. When I build my full database project (multiple stored procedures, multiple tables, etc...), I see hundreds or thousands of these exceptions. I am pretty sure
the developer is just using design tools and not writing all this SQL by hand, but I can't be sure.
Is there some version disconnect that would cause this problem? It slows my automation down considerably, and I would like to think there is a solution that I am just missing.
According to the debug output, MSBuild is loading C:\windows\Microsoft.Net\assembly\GAC_MSIL\Microsoft.SqlServer.TransactSql.ScriptDom\v4.0_12.0.0.0__89845dcd8080cc91\Microsoft.SqlServer.TransactSql.ScriptDom.dll
As I mentioned above, everything works - the database builds with all the tables and sprocs, but it just slows down everything, and I don't know why those exceptions are occurring.