Merge multiple .sql files into one single file

  • Hi,

    Does anyone have a program or utility that can merge multiple .sql files in the windows directory in to one single executable .sql file for fast deployment?

    Scenario....

    I have multiple .sql files: e.g.

    file1.sql
    file2.sql
    file3.sql 
    fileN.sql

    Which I would like to merge in to one single file e.g.
    Files.sql

    Thanks,

    Lewis

  • lewisdow123 90045 - Wednesday, February 1, 2017 4:42 PM

    Hi,

    Does anyone have a program or utility that can merge multiple .sql files in the windows directory in to one single executable .sql file for fast deployment?

    Scenario....

    I have multiple .sql files: e.g.

    file1.sql
    file2.sql
    file3.sql 
    fileN.sql

    Which I would like to merge in to one single file e.g.
    Files.sql

    Thanks,

    Lewis

    Another option - I've usually done those by just creating another main sql script that has the scripts to run a sqlcmd script. Create a script that does something like:

    :r c:\ScriptsDirectory\file1.sql
    :r c:\ScriptsDirectory\file2.sql
    :r c:\ScriptsDirectory\file3.sql
    :r c:\ScriptsDirectory\file4.sql

    And then just execute the main script from the command line with sqlcmd with the input being the main script. Or use sqlcmd mode in SSMS
    It's kind of nice to know how to do that in general. And especially if you get a lot of sql scripts for deployments.
    You can find better examples if you search on execute all sql scripts in a folder (or directory).

    Sue

  • lewisdow123 90045 - Wednesday, February 1, 2017 4:42 PM

    Hi,

    Does anyone have a program or utility that can merge multiple .sql files in the windows directory in to one single executable .sql file for fast deployment?

    Scenario....

    I have multiple .sql files: e.g.

    file1.sql
    file2.sql
    file3.sql 
    fileN.sql

    Which I would like to merge in to one single file e.g.
    Files.sql

    Thanks,

    Lewis

    So you don't mind running files 3 and 4 if 2 fails without causing an abort?

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • You can create a batch file that will merge them for you.

    Create a file called SomeName.BAT
    Add the following content, and then simply Double-Click the .BAT file


    @ECHO OFF
    CLS

    rem Navigate to the folder
    CD C:\Folder1\MyFilesSAreHere

    rem Declare and set the dest file name
    rem NOTE: No whitespace between variable name and value !!!!
    SET %%dst=Files.sql

    rem Note the single ">" to create a new file
    rem  A double ">>" will append to an existing file
    @ECHO. > %%dst

    rem Loop through a csv list of files (in the current folder)
    FOR %%f IN (File1.sql,File2.sql,File3.sql,FileN.sql) DO (

    rem I like to put 2 blank lines followed by a header and separator before the contents of each file.
    @ECHO. > %%dst
    @ECHO. > %%dst
    @ECHO -- Contents of file: %%f >> %%dst
    @ECHO --======================================================================= >> %%dst

    rem Pipe the contents of the current file into the destination file
    TYPE %%f >> %%dst

    )

  • Thank you to all.

    I think my preferred method will be with the .bat file however....

    Is there a way I can loop with out having to name the filename directly?

    FOR %%f IN (File1.sql,File2.sql,File3.sql,FileN.sql) DO (

    Reason being is that there could be many many files...

    Thank you

  • Could just keep it simple and do

    CD C:\MyScripts\
    COPY *.sql > BigScript.sql

  • lewisdow123 90045 - Thursday, February 2, 2017 1:48 AM

    Thank you to all.

    I think my preferred method will be with the .bat file however....

    Is there a way I can loop with out having to name the filename directly?

    FOR %%f IN (File1.sql,File2.sql,File3.sql,FileN.sql) DO (

    Reason being is that there could be many many files...

    Thank you

    There probably is, but I'm afraid I have only figured out how to do it with a fixed list

  • FOR %%f IN (*.sql) DO ....

    Far away is close at hand in the images of elsewhere.
    Anon.

  • David Burrows - Thursday, February 2, 2017 6:58 AM

    FOR %%f IN (*.sql) DO ....

    They have an even easier method now...
    https://technet.microsoft.com/en-us/library/cc753551(v=ws.11).aspx

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Jeff Moden - Thursday, February 2, 2017 7:54 AM

    David Burrows - Thursday, February 2, 2017 6:58 AM

    FOR %%f IN (*.sql) DO ....

    They have an even easier method now...
    https://technet.microsoft.com/en-us/library/cc753551(v=ws.11).aspx

    🙂

    Far away is close at hand in the images of elsewhere.
    Anon.

  • Hi All,

    Thank you for your replies.

    I tried this but it did not work.

    Any help would be very much appreciated.


    @ECHO OFF
    CLS

    rem Navigate to the folder
    CD C:\FileLocation\

    rem Declare and set the dest file name
    rem NOTE: No whitespace between variable name and value !!!!
    SET %%dst=Files.sql

    rem Note the single ">" to create a new file
    rem A double ">>" will append to an existing file
    @ECHO. > %%dst

    rem Loop through a csv list of files (in the current folder)
    FOR %%f IN (*.sql) DO (

    rem I like to put 2 blank lines followed by a header and separator before the contents of each file.
    @ECHO. > %%dst
    @ECHO. > %%dst
    @ECHO -- Contents of file: %%f >> %%dst
    @ECHO --======================================================================= >> %%dst

    rem Pipe the contents of the current file into the destination file
    TYPE %%f >> %%dst

    )

    PAUSE

  • Change drive as well as folder
    Some of your parameter substitutions are wrong
    Some of your redirections are wrong
    Do not put output file (with .sql extension) in same folder as it will be included and your output will be duplicated


    @ECHO OFF
    CLS

    rem Navigate to the folder
    C:
    CD C:\FileLocation\
    MD C:\FileLocation\Output

    rem Declare and set the dest file name
    rem NOTE: No whitespace between variable name and value !!!!
    SET dst=Output\Files.sql

    rem Note the single ">" to create a new file
    rem A double ">>" will append to an existing file
    @ECHO. > %dst%

    rem Loop through a csv list of files (in the current folder)
    FOR %%f IN (*.sql) DO (

    rem I like to put 2 blank lines followed by a header and separator before the contents of each file.
    @ECHO. >> %dst%
    @ECHO. >> %dst%
    @ECHO -- Contents of file: %%f >> %dst%
    @ECHO --======================================================================= >> %dst%

    rem Pipe the contents of the current file into the destination file
    TYPE %%f >> %dst%
    )

    Far away is close at hand in the images of elsewhere.
    Anon.

Viewing 12 posts - 1 through 11 (of 11 total)

You must be logged in to reply to this topic. Login to reply