patternsqlMinor
Script Contents of SQL Server Database From a Batch File
Viewed 0 times
scriptfilesqlcontentsbatchdatabaseserverfrom
Problem
I want to be able to script the schema of a given database into a .sql file from the Windows command prompt. Basically, I want to execute the "Generate Scripts" feature of Management Studio programmatically.
I know this is possible using .NET and SMO, but is there something built-in to do this?
Background to this question: This is meant as a simple auditing instrument. We want to capture the schema every night. Any primitive, low-tech solution works for us.
I know this is possible using .NET and SMO, but is there something built-in to do this?
Background to this question: This is meant as a simple auditing instrument. We want to capture the schema every night. Any primitive, low-tech solution works for us.
Solution
I wrote an open source command line utility named SchemaZen that does this. It's much faster than scripting from management studio and it's output is more version control friendly. It supports scripting both schema and data.
To generate scripts run:
schemazen.exe script --server localhost --database db --scriptDir c:\somedir
Then to recreate the database from scripts run:
schemazen.exe create --server localhost --database db --scriptDir c:\somedir
To generate scripts run:
schemazen.exe script --server localhost --database db --scriptDir c:\somedir
Then to recreate the database from scripts run:
schemazen.exe create --server localhost --database db --scriptDir c:\somedir
Context
StackExchange Database Administrators Q#25103, answer score: 7
Revisions (0)
No revisions yet.