Concatenating Multiple SQL Files

Steve Stedman has a quick Powershell one-liner to concatenate multiple files:

I come across the need occasionally to deploy a set of sql files that are all checked into source control in different files with a file hierarchy like this:

  • Database Name
    • Type of object (proc, table, view, etc)
      • Name of object

When I go to deploy the scripts I need to manually combine all the SQL files into one to move to production, qa or test for deployment. After getting annoyed at lots of copy and paste I finally discovered an easy powershell script to combine all the files into one.

Steve points out at the end that if the file does not end with “GO” then combining multiple things, like stored procedures, together might result in unexpected behavior.  I’ve done something similar to Steve’s script, except as you stream the content, append a newline, “GO,” and another newline.

Related Posts

Agent Job Cmdlets In dbatools

Garry Bargsley has a series looking at different cmdlets in dbatools.  Today’s focus is Agent jobs: Agent Jobs are the heartbeat to most SQL Server environments.  Jobs are how we move data, purge data, automate processes, backup databases, perform database maintenance and many other functions.  Managing one SQL Server and the Agent Jobs can be […]

Read More

Tee-Object In Powershell

Adam Bertram has a paean in honor of Tee-Object: What does Tee-Object  do anyway? Tee-Object basically kills two birds with one stone. This cmdlet redirects output from a PowerShell command and either saves it to a file or to a variable while also returning the output to the pipeline. It allows a scripter to save the […]

Read More

Categories

May 2018
MTWTFSS
« Apr Jun »
 123456
78910111213
14151617181920
21222324252627
28293031