Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Syntax error on commented T-SQL only in SQL Server 2012

I had an error occur while installing on a customer site using SQL Server 2012. I was able to reproduce the syntax error locally on SQLExpress 2012. The same DDL script runs fine under 2008 R2 but fails with "Incorrect syntax near '44445'".

Checking the SQL that is being executed, the text '44445' is commented out. Again, this SQL works on 2008 R2. The last line posted is the syntax offender. Notice, it is commented out as is most of this example.

[snipped]    
IF NOT EXISTS (SELECT * FROM ::fn_listextendedproperty(N'Updatable' , N'USER',N'dbo', N'TABLE',N'PublishLog', NULL,NULL))
EXEC dbo.sp_addextendedproperty @name=N'Updatable', @value=N'True' , @level0type=N'USER',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'PublishLog'
GO

--SET ANSI_NULLS ON
--GO
--SET QUOTED_IDENTIFIER ON
--GO
--IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[dbo].[MetaData]') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)
--BEGIN
--CREATE TABLE [dbo].[MetaData](
--  [ID] [int] IDENTITY(1,1) NOT NULL,
--  [DataName] [nvarchar](255) NULL,
--  [DataDescription] [nvarchar](255) NULL,
-- CONSTRAINT [MetaData_PK] PRIMARY KEY NONCLUSTERED 
--(
--  [ID] ASC
--) ON [PRIMARY]
--) ON [PRIMARY]
--END
--GO

--SET ANSI_NULLS ON
--GO

--SET QUOTED_IDENTIFIER OFF
--GO

--IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[dbo].[T_MetaData_DTrig]') AND OBJECTPROPERTY(id, N'IsTrigger') = 1)
--EXEC dbo.sp_executesql @statement = N'CREATE TRIGGER [dbo].[T_MetaData_DTrig] ON [dbo].[MetaData] FOR DELETE AS
--SET NOCOUNT ON
--/* * PREVENT DELETES IF DEPENDENT RECORDS IN ''DocumentsData'' */
--IF (SELECT COUNT(*) FROM deleted, DocumentsData WHERE (deleted.ID = DocumentsData.MetaTagsID)) > 0
--    BEGIN
--        RAISERROR 44445 ''The record can''''t be deleted or changed. Since related records exist in table ''''DocumentsData'''', referential integrity rules would be violated.''
--        ROLLBACK TRANSACTION
--    END

[snipped]
like image 900
AV8R Avatar asked Sep 02 '25 04:09

AV8R


1 Answers

They discontinued that RAISERROR syntax. Now, you "NEED" the parentesis.

Read this: http://technet.microsoft.com/en-us/library/ms144262.aspx

and search for "RAISERROR"

like image 143
L_7337 Avatar answered Sep 05 '25 00:09

L_7337