Category Archives: Syntax

Copy query from an Excel worksheet

I recently ran a query, copy and pasted the results in an Excel Workbook. The data in the first worksheet and the query in the next Worksheet for reuse in case I need it again.

So today I had to run that query again…copied the query from the worksheet and got an error:

Msg 102, Level 15, State 1, Line 1Incorrect syntax near ‘ ‘.

I double clicked the error and the following line got highlighted:

DECLARE @country_id CHAR(2)

Apparently there is an error at the end of char(2). I removed that line so the following line became the first line of the code:

SELECT @country_id = ‘BE’

Tried to parse once more (I expected another error because the declaration was missing), the same error occurred.  I knew it had something to do with the copy from Excel. After all, Excel is not the right type of application to store queries.

I tried to paste the query in Notepad so all formatting would be gone and it still wouldn’t work.
Paste and copy once more from within Gmail didn’t work either until I actually send an e-mail to myself with the query in the body of the mail. I copied the query to SSMS and it finally worked!!