patternsqlMinor
generating scripts - Incorrect syntax near 'GO'
Viewed 0 times
scriptsincorrectsyntaxgeneratingnear
Problem
this is a partial view of my script to generate the drop index for a particular index
the question is:
when I use this:
It works - generates this script:
when I don't use the
Msg 102, Level 15, State 1, Line 38289 Incorrect syntax near 'GO'.
is there any other way to get this done?
,[DropIndexScript] = 'DROP INDEX ' + QUOTENAME(SI.name) +
+ CHAR(13) + ' ON ' +
QUOTENAME(Schema_name(T.Schema_id)) +'.'+ QUOTENAME(T.name) + CHAR(10) + CHAR(13) + 'GO' + CHAR(13)
FROM sys.indexes I
INNER JOIN (
SELECT Object_id
,Schema_id
,NAME
FROM sys.tables
UNION ALL
SELECT Object_id
,Schema_id
,NAME
FROM sys.views
) T
ON T.Object_id = I.Object_id
INNER JOIN sys.sysindexes SI ON I.Object_id = SI.id AND I.index_id = SI.indidthe question is:
when I use this:
+ CHAR(10) + CHAR(13) + 'GO' + CHAR(13)It works - generates this script:
DROP INDEX [IDX_ProdImages_GetProductListingPageDenormalisedData]
ON [dbo].[ProductImages]
GOwhen I don't use the
+ CHAR(10) + CHAR(13) + Msg 102, Level 15, State 1, Line 38289 Incorrect syntax near 'GO'.
is there any other way to get this done?
Solution
The problem is that you are mixing up which character is which in the variations of a newline: CRLF / LF. You have:
Here you use either
Better yet would be having consistency across them. Either use just
,[DropIndexScript] = 'DROP INDEX ' + QUOTENAME(SI.name) +
+ CHAR(13) + ' ON ' +
QUOTENAME(Schema_name(T.Schema_id)) +'.'+ QUOTENAME(T.name)
+ CHAR(10) + CHAR(13) + 'GO' + CHAR(13)Here you use either
CHAR(13) or CHAR(10) + CHAR(13). The problem is that CHAR(13) is a "Carriage Return" / "CR" while CHAR(10) is a "Line Feed" / "LF". You have reversed them and hence are using both "CR" and "LFCR" in various places. If you swap the "10"s for the "13"s it would work correctly.Better yet would be having consistency across them. Either use just
CHAR(10) everywhere, or CHAR(13) + CHAR(10) everywhere.Code Snippets
,[DropIndexScript] = 'DROP INDEX ' + QUOTENAME(SI.name) +
+ CHAR(13) + ' ON ' +
QUOTENAME(Schema_name(T.Schema_id)) +'.'+ QUOTENAME(T.name)
+ CHAR(10) + CHAR(13) + 'GO' + CHAR(13)Context
StackExchange Database Administrators Q#151696, answer score: 2
Revisions (0)
No revisions yet.