To concatenate Sql Scripts into one big sql script I usually use the following rules and a batch file:
- Always put a “GO” at the end of each script. This ensures that one script doesn’t blend with another one in the same batch.
- Always put at least one blank line at the end and start of the script (I just do this to prevent concatenation of files resulting in the GO and something else joining together and causing an error)
- Each file needs to be in the same encoding. See here
- I try and order the scripts by putting a number at the start of each script. The number will help keep them in order in Explorer and also will mean they should concatenate in the correct order
I create a batch file called Concatenate.bat which has the following contents:
COPY /A *.sql @MyBigScript.sql
This removes the file called @MyBigScript.sql and then concatenates all .sql files into a new file called @MyBigScript.sql.
Update: The @ at the start of the filename will make the @MyBigScript.sql file show at the top of Explorer.