Concatenating Sql Scripts

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:

DEL @MyBigScript.sql
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.

Advertisements

2 Responses to “Concatenating Sql Scripts”

  1. Bilawal Kazi Says:

    the cmd copy command case fails while joining two sql scripts including special characters such as ♥ ☻☺. Any work around for this?

  2. jwwishart Says:

    Bilawal, Sorry about late reply. I would think not… It might be able to be done simply in a powershell script…


Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: