Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I fix "incorrect syntax near GO" errors in scripts generated with Microsoft's Database Publishing Wizard?

I've been searching for a "definitive" solution to this for a couple of hours now and can't find out exactly what the problem is so am hoping someone can point me in the right direction.

I'm trying to create a script for a database (an MDF file DB) on a remote machine and use it to create the same database locally. I've got Visual Studio 2010 on both machines so used the Database Publishing Wizard to generate the script.

I next created a blank DB in Visual Studio locally (another MDF file DB) and selected to create a new query. I've pasted the copied script in but when I try and run it I get dozens of "incorrect syntax near GO" errors. I'm not sure how many in total as the alert pop-up goes off the bottom of my screen. It would seem to be a lot though.

I trimmed off all but the very top of the script and I still get the same errors with it. It now looks like this:

SET ANSI_NULLS ON

GO

I still get the "incorrect syntax near GO" error with this.

Is there something inherently wrong with the scripts this tool generates or am I doing something wrong with it?

The remote DB is 2008 R2 and my local is just plain 2008, hence why I don't just copy the MDF file across.

If anyone has any suggestions, they would be most welcome.

like image 207
Kevin Wilson Avatar asked Nov 21 '10 11:11

Kevin Wilson


2 Answers

From the docs:

GO is not a Transact-SQL statement; it is a command recognized by the sqlcmd and osql utilities and SQL Server Management Studio Code editor.

Either use such a tool to execute the script or omit the GO commands.

like image 109
Hans Passant Avatar answered Nov 13 '22 10:11

Hans Passant


I ran into the same problem recently and found the right answer to it (I think).

If you create a query in some random editor other than SQL Server Management Studio Code editor at the end of a line you may get a CARRIAGE RETURN (\r) character instead of a CARRIAGE RETURN + LINE FEED (\r\n). And even if you hit the DEL key at the end of the line and then Enter it doesn't change.

Use Notepad++ to replace CR (\r) for CRLF (\r\n) - keep the order, it's important!

like image 27
Leonardo Allievi Avatar answered Nov 13 '22 10:11

Leonardo Allievi