Friday, March 6, 2009

Useful Batch File For Generating SQL Create Script from .mdf

Need to generate create scripts for your SQL Express database?  Here's a little batch I've used several times recently to do just that:

@echo off
SET DatabaseFileName=DbName
 
ECHO Generating code for %DatabaseFileName%
ECHO Generating create script 'CreateDatabaseSchema.sql'
 
"C:\Program Files\Microsoft SQL Server\90\Tools\Publishing\SqlPubWiz.exe" script -C "Data Source=.\SQLEXPRESS;AttachDbFilename=%CD%\%DatabaseFileName%.mdf;Integrated Security=True;User Instance=True"  -noschemaqualify -schemaonly -nodropexisting -f CreateDatabaseSchema.sql

To use, just drop in a .bat next to the .mdf, change DbName and run.  It will output 'CreateDatabaseSchema.sql containing create script for all of the tables and stored procs in the database. 

No comments: