readyroll/generate-sql-merge — pull request #2 by robboek (2014-03-30 22:03:15+00:00)
Title: Changes to improve readability and support calling via sqlcmd.

I'm using this proc to automate creation of merge script files for use in a post deployment script. I'm using a command script to loop through a text file with a list of tables, and generate a merge script for each table.

**CreateMergeScript.sql**
```sql
--This SQLCMD script generates a merge script for the passed in table.
--Required SQLCMD variables:
--	$(FileName): Name of file to store the generated script.
--	$(Schema): Schema of table to generate.
--	$(TableName): Name of table to generate.

:Setvar SQLCMDHEADERS -1
:Setvar SQLCMDMAXVARTYPEWIDTH 0

:out $(FileName)

EXEC sp_generate_merge @table_name = '$(TableName)', @schema = '$(Schema)', @nologo = 1, @include_use_db = 0, @results_to_text = 1
```
**CreateReferenceDataMergeScripts.cmd**
```
@ECHO OFF
REM This command script reads from ReferenceDataTables.txt and will call CreateMergeScript.sql
REM for each line in the file. It expects there will be a single table name per line, formatted
REM as ..
REM
REM You must pass in the target server and database, and authenticate using integrated security.
REM
REM To open a command prompt using windows credentials on another domain:
REM RUNAS /netonly /user:\ "cmd"
REM You will be prompted for a password.
REM
REM Example execution: CreateReferenceDataMergeScripts.cmd

FOR /F "tokens=1,2 delims=." %%a IN (ReferenceDataTables.txt) DO (
SQLCMD -S %1 -d %2 -i "CreateMergeScript.sql" -v Schema="%%a" -v TableName="%%b" -v FileName=".\ReferenceData\%%a.%%b.Merge.sql"
)
```