SQL SERVER – 2005 – Create Script to Copy Database Schema and All The Objects – Stored Procedure, Functions, Triggers, Tables, Views, Constraints and All Other Database Objects

Update: This article is re-written with SQL Server 2008 R2 instance over here: SQL SERVER – 2008 – 2008 R2 – Create Script to Copy Database Schema and All The Objects – Data, Schema, Stored Procedure, Functions, Triggers, Tables, Views, Constraints and All Other Database Objects

Following quick tutorial demonstrates how to create T-SQL script to copy complete database schema and all of its objects such as Stored Procedure, Functions, Triggers, Tables, Views, Constraints etc. You can review your schema, backup for reference or use it to compare with previous backup.

Step 1 : Start

SQL SERVER - 2005 - Create Script to Copy Database Schema and All The Objects - Stored Procedure, Functions, Triggers, Tables, Views, Constraints and All Other Database Objects databasecopy1

Step 2 : Welcome Screen

SQL SERVER - 2005 - Create Script to Copy Database Schema and All The Objects - Stored Procedure, Functions, Triggers, Tables, Views, Constraints and All Other Database Objects databasecopy2

Step 3 : Select One or Multiple Database
If Script all objects in the selected database checkbox is not selected it will give options to selected individual objects on respective screen. (e.g. Stored Procedure, Triggers and all other object will have their own screen where they can be selected)

SQL SERVER - 2005 - Create Script to Copy Database Schema and All The Objects - Stored Procedure, Functions, Triggers, Tables, Views, Constraints and All Other Database Objects databasecopy3

Step 4 : Select database options

SQL SERVER - 2005 - Create Script to Copy Database Schema and All The Objects - Stored Procedure, Functions, Triggers, Tables, Views, Constraints and All Other Database Objects databasecopy4

Step 5 : Select output option

SQL SERVER - 2005 - Create Script to Copy Database Schema and All The Objects - Stored Procedure, Functions, Triggers, Tables, Views, Constraints and All Other Database Objects databasecopy5

Step 6 : Review Summary

SQL SERVER - 2005 - Create Script to Copy Database Schema and All The Objects - Stored Procedure, Functions, Triggers, Tables, Views, Constraints and All Other Database Objects databasecopy6

Step 7 : Observe script generation process

SQL SERVER - 2005 - Create Script to Copy Database Schema and All The Objects - Stored Procedure, Functions, Triggers, Tables, Views, Constraints and All Other Database Objects databasecopy7

Step 8 : Database object script generation completed in new query window

SQL SERVER - 2005 - Create Script to Copy Database Schema and All The Objects - Stored Procedure, Functions, Triggers, Tables, Views, Constraints and All Other Database Objects databasecopy8

Reference : Pinal Dave (https://blog.sqlauthority.com), All images are protected by copyright owner SQLAuthority.com

Database, SQL Backup and Restore, SQL Constraint and Keys, SQL Cursor, SQL Download, SQL Function, SQL Index, SQL Scripts, SQL Stored Procedure, SQL Trigger
Previous Post
SQLAuthority News – Principles of Simplicity
Next Post
SQL SERVER – T-SQL Script to Insert Carriage Return and New Line Feed in Code

Related Posts

673 Comments. Leave new

  • Manish Kaushik
    October 17, 2008 9:59 am

    Hi Pinal,

    I am using the same way as you mentioned in SQL 2008, but instead of “Scripts all objects in the selected database” i selected only the Stored procedures of my database.

    and when I executed the script on some other DB few of the Stored procedures are skipped in the script,

    On investigation about the name of the stored procedure, Its noticed that the skipped sp’s are the one which I had used in many other stored procedure.

    Please advice , how can I get the scripts of all the Sps.

    Thanks
    Manish Kaushik

    Reply
  • i want to take script for database schema and also want to take some table with values

    Reply
  • Hi,

    Does anyone know how to get SQL Server 2005 Generate Scripts to output ONLY the indexes, constraints and triggers (i.e. not the CREATE TABLEs)??

    I can do it in SQL Server 2000 Generate SQL Script by:
    1st Tab – Selecting All Tables
    2nd Tab – Unselect Generate DROP and CREATE commands
    3rd Tab – Selecting the Table Scripting Options in the third tab.

    Thanks,
    Niko

    Reply
  • Hi,

    I know how to generate the script by above mentioned steps.
    But instead of manual generating script, have to write new storedprocedure with argument of table name.
    This has to return the script of selected table.

    Help me if u know the answer.
    Thanks……..

    Reply
  • This was usefull! And it’s not like writing help for Notepad. I wasted 10 minutes on SQL Server help, for nothing.

    Reply
  • hi Pinal,
    I want to do dynamic project which will get all database names dynamicaly.i want to generate dynamic query to get all data base names.Can you please help me for that?
    Waiting for positive reply
    Thanks in Advance.

    Vilinda

    Reply
  • Joe Gakenheimer
    November 10, 2008 3:05 am

    Thanks, I have a task at work to copy from test to production and this does the trick!

    Reply
  • hii
    can u pls define the indexed view with syntax?
    is sql 2005 support it?

    Reply
  • But i think this procedure will not copy any data on the script, isn’t ?

    Reply
  • Hi MaxWell

    Copy the SQl2005 Database to a new temp database. Downgrade to SQL 200- Database ( in DB properties). If needed ( using 2005 only conventions ) remove/change them.
    Once done . Backup and restore in 2000 server, Or detach and reattach.

    – Gs

    Reply
  • Hi,

    Can I automate the generation of scripts (Schema)?

    I need to schedule it as a part of daily backup process. Please help me in this regard.

    -Hema

    Reply
  • Yeah Hema, you can do it either by using powershell, or by using the SMO object.

    Thanks
    Manish

    Reply
  • Thank you so much!

    Reply
  • hi pinal,

    i hv 1 problem . i want to take back of sql server database tables n all other objects. but i dont hv access to database mgmt. studio. so how can i take my database backup..?

    i hv heard tht it is possible using writing script.. n running it on our server as .aspx page…

    so can u help me out in this issue..?

    thanks ..

    waiting for yr response..

    Reply
  • hi ,
    i have one problem, i installed the sqlserver2005 in my system but i didnt get the sample databases like northwind.. and also i didnt get the reporting services.. how can i get these plz help meee and also tell me some points to do this…

    Reply
  • my question is ..how make large table…in today projects..bcz i am a begineer

    Reply
  • I have doubt regarding the convertion of database created in 2005 to sqlserver 2000

    Reply
  • hi,

    This one is really good.

    If anybody want to Delete All Data from DB then Just Run the following query.

    EXEC sp_MSForEachTable ‘ALTER TABLE ? NOCHECK CONSTRAINT ALL’
    GO

    EXEC sp_MSForEachTable ‘
    IF OBJECTPROPERTY(object_id(”?”), ”TableHasForeignRef”) = 1
    DELETE FROM ?
    else
    TRUNCATE TABLE ?

    GO

    EXEC sp_MSForEachTable ‘ALTER TABLE ? CHECK CONSTRAINT ALL’
    GO

    Reply
  • Hi,

    I want to know if is it possible to alter a table (adding a column) in live production SQL server 2005 instance?

    If anyone know please reply.

    Thanks

    Reply
  • Hi,

    I want to know : how to copy some table from database A to database B in microsoft sql server 2005 ? And then how to schedule it everyday ?

    Thanks and regards,
    Sandhi

    Reply

Leave a Reply