Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I save the schema of a SQL Database to a file?

I'm writing a software application in C#.Net that connects to a SQL Server database. My C# project is under SVN version control, but I'd like to include my database schema in the SVN repository as well. An answer to a previous question of mine suggested storing the scripts to generate the database in version control. Is there a way to automatically generate these scripts from an existing database?

I'm very new to SQL Server, but I noticed in management studio that the SQL commands to create a table can be generated automatically by right clicking on the table and clicking "Script Table As". Is there an equivalent command that would work with the entire database?

like image 399
Eric Anastas Avatar asked Feb 27 '23 00:02

Eric Anastas


2 Answers

You didn't mention which version of SQL Server, but in 2008 (and possibly earlier), you can right-click the database and choose Tasks -> Generate Scripts. The wizard will walk you through the steps.

like image 180
Paul Kearney - pk Avatar answered Mar 02 '23 03:03

Paul Kearney - pk


You can use SQL-DMO or SQL SMO to script out, or a product like APEXSQLScript (if you have a lot of dependencies, you're going to want to use a mature 3rd party tool like this).

like image 22
Cade Roux Avatar answered Mar 02 '23 03:03

Cade Roux