SQL SERVER – Combined Multiple .SQL files into One Single File

Here is an interesting problem which I enjoyed solving yesterday.

There were multiple SQL Files in my one folder. When I had to send it to my friend I had to collect all the files into a folder and zip it to send it via email. My friend who was using mobile device told me that it is difficult to see the content of the file on a mobile device so it would be nice if I can just send all the SQL files combined in one single file and send it to him.

I loved the idea, but the challenge was that there are over 100s of the files were there and combining them into a single file would be a manual task for hours. I did not like the solution and I went online to find some cool solution. After a while I found a solution that I can use wiht command prompt to combine multiple files into a single file and it is easy to do so.

Here is an example. Let us create three SQL Files.

SQL SERVER - Combined Multiple .SQL files into One Single File combinesql1

SQL SERVER - Combined Multiple .SQL files into One Single File combinesql2

Now we will execute the following command in command prompt which will combine all the three files into a single file.

The command is

type *.sql > OneFile.sql

SQL SERVER - Combined Multiple .SQL files into One Single File combinesql3

Above command will combine all the three files into a single called OneFile.sql.

Following image displays the content of the OneFile.sql which is the concatenation of all the sql files in the folder.

SQL SERVER - Combined Multiple .SQL files into One Single File combinesql4

I enjoyed solving this little problem.

Reference: Pinal Dave (https://blog.sqlauthority.com)

Batch
Previous Post
EXCEL / SQL SERVER – Extract the Domain from an Email Address
Next Post
SQL SERVER – Search Records with Single Quotes

Related Posts

36 Comments. Leave new

  • Nice article…Saved a lot of time..!!

    Reply
  • Denver Alferez
    August 13, 2019 9:49 pm

    thanks a lot for your help! that would helped me a lot!

    Reply
  • Hi

    Lovely, buut, in my case, somewhere it was like this:

    SELECT 2

    GOSELECT 1

    GO

    and became useless :( Do you have solution for that?

    Reply
  • I have files ending with numbers like m1.sql, m2.sql so on till m40.sql. With this approach m10.sql, m11.sql are picked up after m1.sql. Is there a way to get around this issue?

    Reply
  • Working fine, but there is an interesting thing happening: i have ~25 files. All are 1-2 kb. I combine them and the resulting file is 86 MB. Checked the file but the content is whats in those 25 files. I think windows is doing something strange here. Still it works on import but uploading such a file to something like phpmyadmin is hard on most webservers :D

    Reply
  • Worked great! thanks.

    Reply

Leave a Reply