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 pinal.

    Reply
  • Mangesh Tambare
    November 6, 2014 12:43 pm

    Nice.!

    Reply
  • Hello, I ran the same test you did but got duplicate records. — First File
    SELECT 1
    GO– Second File
    SELECT 2
    GO– Three File
    SELECT 3
    GO– First File
    SELECT 1
    GO– Second File
    SELECT 2
    GO– Three File
    SELECT 3
    GO

    Reply
  • E:\test\type> type *.sql > e:\test\OneFile.sql

    Reply
  • Nice article sir ji

    Reply
  • Hi Pinal, I have multiple SQL files in folder and sub folders. how do I combine them?

    Reply
    • NeerajM – you can give all the paths in the command

      type e:Folder1*.sql e:Folder1Folder2*.sql > e:Folder1final.sql

      Reply
      • Jurandir Balestero
        March 15, 2016 2:27 am

        Hi Pinel, do you know how combine when the script have accentuation, my DB has portuguese language, I use type, but don´t usefull.

  • hi,
    i want get the recode top 5 statename and top 5 districtName …?

    Reply
  • Hey Pinal,
    great solutions – thanks for sharing!
    I was wondering if I could insert a “UNION” after each query as well, so I could build one large query consisting of multiple smaller ones which I can keep in seperate files?
    Best, Vincent

    Reply
  • Hi Pinal,
    Thanks a lot. This was very helpful…

    Reply
  • Great article!

    Reply
  • karthik Thollamadugu
    July 4, 2016 12:14 pm

    Hi, how to do the opposite. i mean splitting and saving to different files

    Reply
  • Prateek Gupta
    July 8, 2016 4:52 pm

    but it resulted into duplicate copy of contents as contents of OneFile is also copied.

    Reply
  • Oscar Bragado Alvarez
    September 16, 2016 2:45 pm

    You are the one!!!

    Reply
  • To everybody who get´s some sort of duplicates out of the batch file listed above:
    I have not the deeper knowledge to explain the specified problem, but my gues is that it might be a problem that the created output file is a *.sql file and is created while still the batch still searches for *.sql files.
    That beeing said the solution is quite simple:
    – create *.txt file
    – rename this txt file to *.sql file

    EXAMPLE:
    type *.sql > combined.txt
    ren combined.txt combined.sql

    Thank you very much :)

    Reply
  • Dave I run this command and it truncates part of my sql scripts (at about 11000 lines or so). Do you know of any limitations regarding this?

    Reply
  • You can try using

    copy *.sql AllSql.sql

    This does not truncate the final sql file.

    Reply
  • Hitesh Thakkar
    June 14, 2017 3:25 pm

    I have 4 .sql files and i am trying to create using type *.sql>filename.sql but it is not create proper output file.
    output file is automatically truncated data after 368 lines.
    same way i have tried with 6 files but same thing happens output file is truncated data after 886 line.

    Reply
  • Phillip M Hanson
    October 18, 2018 10:17 pm

    So, does this allow me to run all of the collective scripts on one sql query?

    Reply
  • hey man i got a not found

    Reply
  • I tried 16 file each one abverage 300 lines but some file content missing

    Reply
    • user of solution
      April 27, 2020 4:17 pm

      Please check the small files encoding, if the files are not unicode then there is a chance of content missing

      Reply

Leave a Reply