Problems running scripts through osql.exe

I am trying to run update scripts for my software in this format:

osql.exe -i "path \ to \ script" -U "user" -P "Password" -S "Location sqlserver" -d "Database name" -n -b

Most scripts are in the same format and all end with GO. Many of them work fine, but all random scripts return an error and will not work. Error: โ€œThe incorrect syntax next toโ€œ โˆฉ โ€in line 1. The script may be as simple as INSERT, but always this error. I can not find anything on the Internet that could help. Can I give someone representation?

Scripts are executed only manually. It is also interesting if I create a new text document and paste the script into a new file and change it to .sql and run this file, then it works fine. I just did it for all the โ€œbrokenโ€ scripts, but it still happens with new ones and will happen with changed ones as well, and in the end.

+4
source share
2 answers

Most likely because the file is encoded as Unicode instead of UTF-8. You can check this in Notepad ++ among other free utilities. Try converting it to UTF-8 and see if that helps.

UPDATE

Bugfix: as the article related in the comments explains, osql can parse text files encoded as UTF-16 (Unicode 1200) or ANSI (Windows-1252), but cannot parse UTF-8 encoded files.

+3
source

This sounds like a Unicode problem (especially since copying / pasting into a new document works). To verify this, you can use type and redirect to a temporary file that will force it to ANSI like this:

 type \path\to\script.sql > %TEMP%\newscriptname.sql & osql.exe -i "%TEMP%\newscriptname.sql" -U "user" -P "Password" -S "Location of sqlserver" -d "Database name" -n -b 
+1
source

All Articles