views:

43

answers:

3

Hi, I use Visual Studio 2008 database project for managing my database scripts and version control. by default visual studio script templates are encoded in UTF-8 format. when I try to concatenate the scripts for database release (using the DOS command "type *.sql > dbscripts.sql") and run the concatenated script from SQL server management studio, I get invalid character error. however if I convert each script to ANSI format (using notepad), and concatenate them then I don't get any errors when I run the concatenated script. but converting each script into a different encoding format is tedious. what is the best way to execute multiple SQL scripts regardless of the encoding format of the scripts? do I need to create a batch file and use sqlcmd to run it and redirect the output to a text file? are there any alternatives? Thanks in advance.

+2  A: 

That's because of the BOM in the file. The TYPE command is too primitive to detect it and will just copy the 3 bytes to the output.

You can change the encoding used for the .sql file. Tedious too btw. File + Save As, click the arrow on the Save button, Save With Encoding and select your current code page. Like 1252 for Western Europe and Americas, it is probably the first one in the list. This is the same thing you did with Notepad.

Beware of the trouble you can get into if the server runs with a different code page and your scripts contain accented characters. I would myself just write a little utility that uses Directory.GetFiles and StreamReader/Writer to fix the problem.

Hans Passant
A: 

You should use the copy *.sql dbscripts.sql

Gaby
tried that already, that didn't work either, I still get the invalid character error.
RKP
@RKP, what if you merge the files first and then convert to ansi the final file. So you only need to do the conversion once.. worth a shot ..
Gaby
no, I tried that one as well and didn't work. in that case the invalid character is visible, that is the only difference. I guess using the "sqlcmd" command seems to be the only option by creating a batch file and putting all the file names in it. I could have overridden the visual studio templates to use ANSI format, but I was looking for a better way to fix this issue. thanks for your reply.
RKP
+1  A: 

found this link which generates "SQLCMD" commands using Windows Powershell. for concatenation, it looks like there is no solution except saving each file in ANSI format. I can at least generate a file with all sqlcmd commands using this without having to concatenate them

http://sqladm.blogspot.com/2010/02/generate-sqlcmd-statements.html

RKP