Tuesday, March 20, 2012

Restore Database - Incorrect syntax near 'GO'

Hello, is it something new in SQL 2005, that you cannot have a GO statement at the end of a restore database command? If I run a statement like below, it fails on teh GO...

Msg 102, Level 15, State 1, Line 5

Incorrect syntax near 'GO'.

But if I remove the GO it works.... I coulda sworn a GO was ok to complete a batch with ANY SQL statements in it?!? Am I doing something stugatz?

Thanks, Bruce

RESTORE DATABASE MyDatabase

FROM DISK = 'E:\SQL\MyDatabase.BAK'

WITH NORECOVERY,

MOVE 'MyDatabase_Data' TO 'E:\SQL\MyDatabase_Data.MDF',

MOVE 'MyDatabase_Log' TO 'E:\SQL\MyDatabase_Log.LDF'

GO

What tool are you using? GO should be parsed out by the tool and never sent to SQL Server itself, but that does not appear to be occurring here.|||Just using the regular ole SQL Server Mgt Studio query window... GO is still set as my delimiter, I checked that... It's not a big deal, just curious to know if I am doing something strange or something is different in SQL 2005... that doesn't allow you to have a GO immediately after a RESTORE? probably the syntax of my RESTORE is wrong somehow?!? Bruce|||

No, it worked for me. Have you tried putting another return in there (maybe there is an invalid character)? This should work.

Also you are not trying to EXEC it right? (I am pretty sure you would have said Smile This will cause that error:

exec ('

RESTORE DATABASE MyDatabase

FROM DISK = ''E:\SQL\MyDatabase.BAK''

WITH NORECOVERY,

MOVE ''MyDatabase_Data'' TO ''E:\SQL\MyDatabase_Data.MDF'',

MOVE ''MyDatabase_Log'' TO ''E:\SQL\MyDatabase_Log.LDF''

GO'

)

|||

I can't remember clearly...

Isn't it necessary to also RESTORE the Log when you move the file in a database RESTORE.

Code Snippet


RESTORE DATABASE MyDatabase
FROM DISK = 'E:\SQL\MyDatabase.BAK'
WITH
NORECOVERY,
MOVE 'MyDatabase_Data' TO 'E:\SQL\MyDatabase_Data.MDF',
MOVE 'MyDatabase_Log' TO 'E:\SQL\MyDatabase_Log.LDF'

RESTORE LOG MyDatabase
FROM MyDataBase_Log
WITH RECOVERY

|||ok, this was driving me nuts... but, found it... retyped the entire command and it worked... then compared the before and after SQL code... somewhere in the script there was a carriage return "0d" with no line feed "0a"... so it was invisible to the eye... and a "GO" must have to be on a "line" being defined as after a "0d 0a" combo... knew it was something stugatz. must have been done in an old editor many moons ago I guess, not sure how else you'd lose PART of a carriage return/line feed.... Thanks, Bruce|||Arnie, good question. The database I happen to be dealing with is SIMPLE recovery mode, but do you have to restore the LOG separately if it was a FULL recovery model? Why then restore the Log in the RESTORE DATABASE command? or if your example said "WITH RECOVERY" wouldn't that restore the LOG (LDF) that is named in the RESTORE DATABASE MOVE statement? or if it's FULL recovery mode, and you did NOT specify the RESTORE LOG, hmmm, what do you get? Anyways, my original issue was resolved by finding a bad carriage return/ line feed character... thought I might need a semi-colon to end the RESTORE command, like the WITH statement requires a semi-colon... not... But now you have me wondering about this LOG restore thing.... back to BOL I go........................Thanks, Bruce|||

With a 'Simple' recovery model, there shouldn't be any additional effort required to restore the log files. However, since you left the database in NORECOVERY mode, it would be necessary to add the RESTORE LOG as I suggested.

With a 'Full' recovery model, you would want to restore the database first, and then restore the log file(s), using RECOVERY on the last log file restore.

I'm glad you found the 'culprit'. Darn non-visible characters sometimes bite us...

No comments:

Post a Comment