PositiveGuy
PositiveGuy

Reputation: 47783

Script entire database SQL-Server

Is there a way I can get a scripting of all tables, procs, and other objects from a database? I know there's an option to script the database but it only gave me some sort of top level script, certainly not a script to create all tables, procs, udfs, .etc.

Upvotes: 101

Views: 113605

Answers (7)

Cameron Forward
Cameron Forward

Reputation: 712

Just looking at the table data, to output all of the table data content in Management Studio 2012 and 2014, it is a bit hidden but I found the option after some looking:

  1. Right click the db
  2. Select 'Tasks' > 'Generate Scripts...'
  3. On 'Set Scripting Options', click 'Advanced'
  4. Under 'General', set 'Types of data to script' to true (it is at the bottom of the 'General' group)

Upvotes: 3

Seth Reno
Seth Reno

Reputation: 5450

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

Upvotes: 16

si618
si618

Reputation: 16858

We ended up using a combination of SSMS script generation to extract schema and data, and then use our own database tool which allows keyword parsing and token based replacement in scripts. It also ensures scripts are only applied once.

Why?

  • We need to support installations on SQL Server 2000, 2005 and 2008, and there are changes to data types between versions, e.g. 2005+ have nvarchar(max), whereas 2000 only supports ntext. So our scripts use a token and based upon the db choice replaces with the correct type.
  • Execution of some scripts requires a wait period after execution, e.g. We found if you didn't wait a few seconds after creating a new databases via a script, the SQL Server might fail sometimes (because it hasn't had time to create the db files) when it went on to create tables, etc.
  • We wanted to maintain a history of what scripts were executed and when.
  • We wanted to allow our Wix MSI installer to specify connection string and credentials, and needed some way to pass these into the scripts, so once again, using tokens and some conditional logic.

Example script (edited for brevity)

-- Sleep: 5 
-- Sleep after creating database to allow file system to create db files
CREATE DATABASE [$Database$]
GO

EXEC sp_dbcmptlevel [$Database$], $CompatabilityLevel$
GO

USE [$Database$]
GO

IF '1'!='$IntegratedSecurity$'
BEGIN
    CREATE LOGIN [$Login$] WITH PASSWORD=N'$Password$', DEFAULT_DATABASE=[$Database$]
    CREATE USER [$User$] FOR LOGIN [$Login$]
    EXEC sp_addrolemember N'db_owner', N'$User$'
END
GO

Upvotes: 3

devio
devio

Reputation: 37225

I wrote a utility for this task, SMOscript.

Script generation is performed by the SMO library, and supports the new object types in SQL 2005 and 2008.

Upvotes: 4

Barry Hurt
Barry Hurt

Reputation: 61

I recommend looking at RedGate SQL packager. It is not free, but has been useful enough to be worth the price.

Upvotes: 1

polyglot
polyglot

Reputation: 2058

If you need to do it programmatically, you can use the SQL DMO library (OLE) against SQL Server 2000, but more likely you may wish to use the SQL SMO library (native .NET libraries) against SQL Server 2005 and later.

Both these libraries are integral to the SQL Server administrative tools install.

This is in the case that generating the full database script from SQL Server Management Studio is insufficient.

Upvotes: 0

Chris Brandsma
Chris Brandsma

Reputation: 11736

From Management Studio Right-click on your database. Tasks -> Generate Scripts.

That should do it.

Upvotes: 176

Related Questions