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

  • Gerardo Angeles
    July 10, 2010 2:13 am

    Hello Dave, I need to use sql 2005 sentences for copy a database to another, example copy db_x to db_y with data and all objects. Thank you.

    Reply
  • Thank you, helped me..

    Reply
  • Mario DePedro
    July 13, 2010 9:15 pm

    I upgraded from SQL 2000 to 2005. I had a dts package that would determine the last backup of my production database. It would then restore to another DB called reportdb. How can I accomplish this in SQL 2005. This job ran nightly so that the report database was a copy of production as of the midnight before. All users would then only access the report DB and never touch production DB.

    Reply
  • Gerardo Angeles
    July 13, 2010 9:16 pm

    Hello again Dave, well my requeriment is “Take the previous database named p2010 and create a database named p2011 from p2010”, my idea is first backuped p2010 and restore this as p2011. I don’t know if this solution is the best because I don’t have idea if exists another option to do this.

    For the moment my steps are restore and backup:

    1. Backup

    BACKUP DATABASE p2010
    TO DISK = ‘C:Archivos de programaMicrosoft SQL ServerMSSQL.4MSSQLBackupsome_name.Bak’

    1.1 Add to device

    EXEC
    sp_addumpdevice ‘disk’,
    p2010,
    ‘C:Archivos de programaMicrosoft SQL ServerMSSQL.4MSSQLBackupsome_name.bak’

    (1.1.1 To correct for tests)

    exec sp_dropdevice p2010

    2. Restore

    (option 1)

    RESTORE DATABASE p2011_newName
    FROM DISK = ‘C:Archivos de programaMicrosoft SQL ServerMSSQL.4MSSQLBackupsome_name.bak’
    WITH REPLACE,
    MOVE ‘filename_Data’ TO ‘C:tempfilename_Data.MDF’,
    MOVE ‘filename_Log’ TO ‘C:tempfilename_Log.LDF’

    /* Move to change drive:path */

    (option 2)

    RESTORE FILELISTONLY FROM p2010
    RESTORE DATABASE p2011_newName
    FROM DISK = ‘C:Archivos de programaMicrosoft SQL ServerMSSQL.4MSSQLBackupp2010.bak’
    WITH RECOVERY,
    MOVE ‘filename_Data’ TO ‘C:tempfilename_Data.MDF’,
    MOVE ‘filename_Log’ TO ‘C:tempfilename_Log.LDF’

    Reply
  • can pls. send a complete tutorials of sql server database 2005 in any file format

    Reply
  • Hi

    is it possaible to Script only the Constaints in the entire database

    Reply
  • mukesh sharma
    July 20, 2010 4:07 pm

    Thanks Sir,

    my problem is solved with the help of you

    Reply
  • Hi friends,
    I need a help for converting sql script to oracle script
    i generate script from sql developer , now i want convert this script to oracle script.

    i am using ubuntu os 10.4, then i am installed oracle10g edition
    any one pls help me out for converting and how import script to oracle 10g

    pls pls urgent

    Reply
  • hi pls help out for the above problem

    Reply
  • hi dev ,
    again vinot, pls help me out for the solution for
    sql script to oracle script
    i am having sql script, using ubuntu 10 os.
    and install oracle10g
    pls help me out, and how to run script in oracle 10g

    Reply
  • hi madhi,
    i cant find above site
    could u pls help for covert or tell me is there any tools(s/w) available for converting

    Reply
  • yes i find error could u try pls

    Reply
  • Hi, I need to validate the proccess of a backup and restore, all the issues that will occur, handling errors. It’s possible use a tray & catch sentence in windows server 2000?
    Thanks in advance

    Reply
    • You cannot use Try/catch in version 2000. They are supported from version 2005 onwards

      Reply
      • Hi, I need to validate the proccess of a backup and restore, all the issues that will occur, handling errors. It’s possible use a tray & catch sentence in SQL server 2000?

        Thanks Madhivanan,

        Ok, how can I do, to handling errors on SQL Server 2k?

        Regards.

      • You need to make use of @@ERROR

        –statement
        If @@ERROR0
        –Write catch block here

  • hi i need another help,
    i want to create a schema in oracle, could u pls tell me the format with example.

    thanks.

    Reply
  • how to install oracle10g standard edition on ubuntu

    Reply
  • Dear Experts,
    I’m facing a very critical situation…and no one is here to help me…So my humble request to guide me….

    Actually we have a server running a Intranet Site having os 2000 server and SQL 2000 .data also uploading to sql server i.e. at Denmark office…now I have to upgrade server in to a new machine…at first step i have transferred all data to new server .
    now ii want to shift completely sql 2000 DB to SQL 2005 server including all security / logins store process SQL server agent jobs…everything..how will i do??I have not much idea on SQL except backup restore…and all simple activities

    i have already took database backups from SQL 2000 and restore in SQL 2005…but how can I thasfer other Login and server agent jobs or any other process that is running in SQL 2000.

    Thanking
    You
    Prabhusap

    Reply
  • Hi,
    This is great, but it just create a script with create table structure. How can i get all data in database to the scrips also.
    Thank you alot.

    Xinglee

    Reply
  • I want single table backup in particular database , if its is possible means please give the commands

    Reply
  • How can I writte a select statement to get the information on the index’s define for a table ?

    Reply
  • I am looking for a script i can run that will restore 6 DB’s from a local drive on the SQL machine to 6 DB’s on the local machine. They are all named different but i am not very versed in the SQL world. I already have a script to stop and start all the DB’s now i just need to complete the script so i dont have to manually go in and restore each single database. I have to do this to them everyday and its quite a time consuming task and was hoping for some help. I will explain in some detail the names of the DB’s and hopefully that will help in the creation of such a script.

    Train01 (Full DB name) has a backup located at d:backupTrain01.bak, I want that file to replace whats currently in the Train01 DB. I guess if i knew more SQL script code it would probably be very easy to do this but i dont. Any help or pointers would be very appreciated

    Thanks
    Joakim

    Reply
    • Gerardo Angeles
      August 20, 2010 11:32 pm

      I hope this example give to you some idea about how to do a backup/restore to specific drive:

      set ANSI_NULLS ON
      set QUOTED_IDENTIFIER ON
      go
      — =============================================
      — Author: IDS – Gerardo Angeles Nava
      — Create date: August 20, 2010
      — Description: 1. Make a backup. 2. Make a restore of backup but with another name (year + 1)
      — This stored procedure most be reside on the database server that contains the database to backup

      — HELP to ME:
      — If it’s possible that this script resides in external database server that’s no contains the database to backup,

      — =============================================
      Create PROCEDURE [dbo].[sp_Backup_RestoreWithNewName]
      @dbNameSource VARCHAR(100),
      @dbNameTarget VARCHAR(100)
      AS
      BEGIN
      SET NOCOUNT ON;
      DECLARE @dbNameToBackup AS VARCHAR(100)
      DECLARE @pathBakFile AS VARCHAR(255)

      DECLARE @dbNameToRestore AS VARCHAR(100)
      DECLARE @pathRestore AS VARCHAR(255)
      DECLARE @pathData AS VARCHAR(255)
      DECLARE @pathLog AS VARCHAR(255)

      DECLARE @data AS VARCHAR(100)
      DECLARE @log AS VARCHAR(100)

      /* Configuration path (drive): */
      SET @dbNameToBackup = @dbNameSource
      SET @pathBakFile = ‘E:dataMSSQLBACKUP’ + @dbNameToBackup + ‘.Bak’

      SET @dbNameToRestore = @dbNameTarget
      SET @pathData = ‘E:dataMSSQLBACKUP’ + @dbNameToRestore + ‘.mdf’
      SET @pathLog = ‘E:dataMSSQLBACKUP’ + @dbNameToRestore + ‘.ldf’
      SET @pathRestore = @pathBakFile

      /* Remove device, if exists */
      /*
      IF EXISTS(SELECT * FROM sys.backup_devices WHERE name = @dbNameToBackup)
      BEGIN
      exec sp_dropdevice @dbNameToBackup
      END
      */

      /* Generate Backup */
      BACKUP DATABASE @dbNameToBackup
      TO DISK = @pathBakFile

      /* Mount the sql-device, if exists */
      EXEC
      sp_addumpdevice ‘disk’,
      @dbNameToBackup,
      @pathBakFile

      /* ¿Have a backup? */
      IF EXISTS(
      SELECT DISTINCT
      S.[name]
      ,DATABASEPROPERTYEX(S.[name], ‘Status’) AS [Estatus]
      ,crdate
      ,[filename]
      FROM master..sysdatabases S
      JOIN msdb.dbo.backupset B
      ON S.[name] = B.database_name
      WHERE
      S.[name] = @dbNameSource
      and CONVERT(DATETIME, CAST(crdate AS VARCHAR(16)),120) = CONVERT(DATETIME, CAST(database_creation_date AS VARCHAR(16)),120)
      )
      BEGIN
      /* Restore the Data Base with another name (year + 1) */
      SELECT @data = [name] FROM master.dbo.sysaltfiles
      WHERE [dbid] = (SELECT [dbid] FROM master.dbo.sysdatabases WHERE [name] = @dbNameSource and fileid = 1)

      SELECT @log = [name] FROM master.dbo.sysaltfiles
      WHERE [dbid] = (SELECT [dbid] FROM master.dbo.sysdatabases WHERE [name] = @dbNameSource and fileid = 2)

      RESTORE DATABASE @dbNameToRestore
      FROM DISK = @pathRestore
      WITH REPLACE,NORECOVERY,
      MOVE @data TO @pathData,
      MOVE @log TO @pathLog

      RESTORE DATABASE @dbNameToRestore WITH RECOVERY;

      /* ¿Have a restore? */
      IF EXISTS(SELECT name FROM master..sysdatabases WHERE [name] = @dbNameToRestore)
      BEGIN
      /* Yes we have it!*/
      IF EXISTS(SELECT name FROM master..sysdatabases WHERE [name] = @dbNameToRestore AND DATABASEPROPERTYEX(name, ‘Status’) = ‘ONLINE’)
      BEGIN
      /* ¿The database is online? */
      SELECT ‘Restore: yes. On line: yes.’
      END
      ELSE
      BEGIN
      SELECT ‘Restore: yes. On line: NO.’
      RETURN
      END
      END
      ELSE
      BEGIN
      SELECT ‘Restore: No’
      RETURN
      END
      END
      ELSE
      BEGIN
      SELECT ‘Backup: No’
      RETURN
      END

      SELECT ‘Now you have a backup an restore with another name’

      END

      Reply

Leave a Reply