Annoying T-SQL Script error “Incorrect syntax near ‘ ‘.”

by Dec 15, 2016

I’m hoping you’ll find this post useful as finally got my issue solved but kept me going in circle.  As I’m use to integrate my T-SQL scripts into my PowerShell scripts, but first I need to verify that everything is working as expected.  Well, I ended up getting this annoying error every time I executed a valid SQL script:
 
Msg 102, Level 15, State 1, Line 4
Incorrect syntax near ‘ ‘.
 
Here’s my scenario, I’m using an web application that have an administrator option to generate a SQL script from the result displayed on the application datagrid.    So, I just Copy/Paste the script into my SSMS and fine tuned the code for what I need.  But, In my case, I’m migrating some SQL tasks from SQL Server 2005 to 2014.
 
 
 
But, I ended up getting this error on a valid and well-structured SQL script. I can execute without any error the script on a SSMS connected to a SQL Server 2014.
Unfortunately, when I execute the same script on SSMS connected to a SQL Server 2005 then I get the error:” Msg 102, Level 15, State 1, Line 4
Incorrect syntax near ‘ ‘.
 
 
 
No matter what I do to, Copy/Paste to another editor and save the file as UTF-8 encoding. I still get the annoying error.
 
So, how to fix it! After checking some of the common internet forums, most of them blamed function(s) been use in the SQL script.  But, I couldn’t find any information to help resolve  the issue until turned into using “Design Query in Editor“.
 
 
 
By highlighting the SQL script code, the right-click in the code, then select “Design Query in Editor” option.  This will open the SQL script graphic designer with the existing code.
 
 
 
In the “Query Designer“, this gives me a couple of options: either to click on  Click ‘OK’ to override the existing code, or just highlight/Copy/Paste the code to another query pane and execute the code.  The error finally cleared!
 
 
 
So, by using the “Query Designer“,  I was able to get rid of the error and execute the SQL script.