August 18, 2010 at 5:40 am
Hi,
We're getting an error when using EXEC (@command) when @command contains 'GO', but only in certain circumstances. I've given some examples below.
DECLARE @command VARCHAR(MAX)
SET @command = 'select 1' + CHAR(10) + 'go' + char(10) + 'select 2'
exec (@command)
This works fine, I get the expected output. But when I try
DECLARE @command VARCHAR(MAX)
SET @command = 'ALTER INDEX ALL ON Payroll.dbo.Employee REBUILD' + CHAR(10)
+ 'GO' + char(10)
+ 'ALTER INDEX ALL ON Payroll.dbo.Company REBUILD'
EXEC (@command)
I receive this error message:
Msg 156, Level 15, State 1, Line 3
Incorrect syntax near the keyword 'ALTER'.
Any ideas?
August 18, 2010 at 5:47 am
Run this, look at the columnname in the top resultset ๐
DECLARE @command VARCHAR(MAX)
SET @command = 'SELECT GETDATE()' + CHAR(10) + 'go' + char(10) + 'SELECT GETDATE()'
PRINT @command
exec (@command)
Use ; as a statement separator.
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Viewing 2 posts - 1 through 1 (of 1 total)
You must be logged in to reply to this topic. Login to reply