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

  • Jayaprakash Rajendran
    December 1, 2009 11:05 am

    Sir,

    How to i Migrate Windows Sql server Database to Linux Sqllite Database ????. Please Help Me

    – Jayaprakash Rajendran

    Reply
  • Hi,
    I want to move records from one database table to another database table ( both table structures are same) . But i want here i want to move first 1000 records and then next 1000 records like that. i don’t have any key in my table.

    Thanks in advance.

    Reply
  • Thanks,
    its realy good demostration…

    Reply
  • Thanks…

    Reply
  • How can i retrieve list of user defined roles assigned to a user ? I tried with sp_helpuser gives only one role at a time…even though user has mutliple roles… again this is for SQL Server 2000 db

    thanks in advance for the help……

    Reply
  • Pinal,

    You mentioned in the few top replies that you sent e-mails to people showing how to generate similar scripts programmatically (in other words, script table creation and all related information: indexes, constraints, triggers) using code.

    Would you please send me this by e-mail as well and it may be a great new blog post.

    Thanks in advance.

    Reply
  • Hi Pinal,

    I think the information you provide is great, please keep up the work.

    If possible could you please send me the script to automate this process

    Thanks in Advance
    Brett

    Reply
  • SIVANANDA REDDY
    December 22, 2009 5:42 pm

    i want comparation between two data bases table wise, i want only diffent table names and different columns only

    like

    tablename col1,col2

    if (ds.Tables[0].Rows.Count > 0 && ds1.Tables[0].Rows.Count > 0)
    {
    try
    {
    IList missedFields = new List();

    foreach (DataRow dr in ds.Tables[0].Rows)
    {
    bool stts = false;
    foreach (DataRow dr1 in ds1.Tables[0].Rows)
    {
    if (dr[“Column_name”].ToString() == dr1[“Column_name”].ToString())
    {
    stts = true;
    break;
    }
    }
    if (stts == false)
    {
    missedFields.Add(“from US:” + dr[“Column_name”].ToString());
    }
    }
    if (missedFields.Count == 0)
    {
    Response.Write(“Success”);
    }
    else
    {
    DataTable dt = new DataTable();
    DataColumn dc = new DataColumn(“Missed Field Names:”);
    dt.Columns.Add(dc);
    foreach (string str in missedFields)
    {
    DataRow dr = dt.NewRow();
    dr[0] = str;

    dt.Rows.Add(dr);
    }
    GridView1.DataSource = dt;
    GridView1.DataBind();
    }
    }
    catch (Exception ex)
    {
    Response.Write(ex.Message.ToString());
    }

    }

    Reply
  • SIVANANDA REDDY
    December 22, 2009 5:43 pm

    i want un matched Table name with all Rows

    Reply
  • Hi,
    Can we have such type of sql commands by which we can generate create script of any table.
    I am thinking about to making a procedure which can copy a table with its whole data.
    Thanks
    Rahul

    Reply
    • Hello Rahul.

      Use Database Publishing Wizard to generate script of tables with data.

      Try searching for key word Database Publishing Wizard either in Google Search Engine or SQLAuthority search.

      ~ IM.

      Reply
    • Hi Rahul,

      In SQL Server 2008, you can script the table with data (an insert statement for each row) using SQL Server Scripting wizard. In this wizard, on Choose Script Options page set the Script Data option to True.

      Regards,
      Pinal Dave

      Reply
  • could you tell me how to run script in vs2008 it has inbuilt db
    i have script for creating database

    Reply
  • How can i scrip a database with all tables and its datas?

    i am using sql 2005 express edition.

    can u help on this

    Reply
  • Can you kindly tell me which commands syntax are changed in SQL 2008 compare with SQL 2000?

    Thanks

    Reply
  • Hi,

    To cover this topic completely a whole book can be written. Here I mentioned few enhancement in SQL Server 2005 that are also applied to SQL Server 2008.

    New Data types
    Error handling with TRY-CATCH
    ROW_NUMBER and other ranking functions
    CTE
    OUTPUT clause
    INTERSECT and EXCEPT operands
    PIVOT and UNPIVOT functions
    CROSS APPPLY operator

    For more details please check the following article:
    https://docs.microsoft.com/en-us/previous-versions/sql/sql-server-2008/cc721270(v=sql.100)

    Regards,
    Pinal Dave

    Reply
  • Hello,

    I was wondering what would be the best approach to creating a “template” to create a database, tables and indexes based off of current databases I have already created. I have used current tools within management studio and combined the ‘create database template’ with a the script created with one of the current databases which contains the tables and indexes I would like to be able to reuse. I don’t need data. Any help would be appreciated.

    Thanks for your time.

    Jake

    Reply
  • hi pinal
    can u tell me how to run database script / restore database script from command line in sql server 2005

    Reply
    • Hello Aatish,

      Use the SQLCMD utility for this purpose. At first right the complete t-sql script to restore database in a notepad for example in c:RestoreScript.txt
      Now go to command prompt and write a command as below:

      >sqlcmd -S “yourServerName” -U “UserName” -P “password” -i c:RestoreScript.txt

      If you can login with windows credential then username and password are not required.

      Regards,
      Pinal Dave

      Reply
  • plz tell me how to restore database from command line.

    Reply
  • Dear Pinal

    I have a problem with a database in SQL 2008.
    I can not generate the script of this database. I only tried with the option of generating script in SQL Server Management Studio.
    I can generate script for all other databases except this.
    I also tried this database by taking backup and restoring it in another machine. The result is same.
    The scripting utility can see all the tables, functions and procedures etc. but it will show it is validating the settings, if we try to do a script.
    Any idea how to take script for this stubborn database..?
    Your valuable suggestion is requested in this regard….

    With Love
    Thomas

    Reply
  • Hi,

    Can anybody tell me how can I write a stored procedure to copy a database from one server to another server. This procedure is going to be accessed by relevant parameters from an application.

    Reply
  • Hi ,

    I am Fresher with more curious to learn new things.

    I am going to Design one web-Site for showing Details that had been stored on database (on Mysql which is existing one) ,Will it be possible to copy all that data in SQL-2005 database as it is.
    Also which tool/technique i prefer to copy that to my Sql database ,i want to design website in C#.net,Firstly it’s an it’s intranet application,if successful may ploy on Internet.

    Another question is that can i convert flash file application in to text ?

    hope you will be there for me
    thanks for your important suggestions!!!

    Reply

Leave a Reply