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

Quite often I get the request regarding how to copy all the objects – including schema and data from any database and re-create it on another instance.

SQL Server 2008 and SQL Server 2008 R2 has script generator wizard which does it for us. I ask you to pay special attention to image #5.

After the script is generated, the next challenge often users face is how to execute this large script as SQL Server Management Studio does not open the file. One can use SQLCMD for the same. See that in the last image of this post.

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 dswizard1

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 dswizard2

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 dswizard3

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 dswizard4

Pay attention to the option Types of data to script – select option ‘Schema and data’

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 dswizard5

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 dswizard6

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 dswizard7

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 dswizard8

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 dswizard9

As the file with data will be very large, use SQLCMD to execute the large script which will create database with schema & data.

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 dswizard10

Please let me know if you have any question or suggestion on this blog post.

Watch SQL in Sixty Seconds Episode on same subject.

[youtube=http://www.youtube.com/watch?v=lSh3fq-MikE]

Reference : Pinal Dave (https://blog.sqlauthority.com)

SQL Scripts, SQL Utility
Previous Post
SQL SERVER – Video – Best Practices Analyzer using Microsoft Baseline Configuration Analyzer
Next Post
SQL SERVER – What Kind of Lock WITH (NOLOCK) Hint Takes on Object?

Related Posts

122 Comments. Leave new

  • Sameer Panchangam
    May 7, 2011 10:44 am

    Why use SQLCMD in the command prompt in the end? Any alternatives/issues with the UI way?

    Reply
  • What I personally prefer as a method of transferring schema objects and data across databases or even across instances is the scripting method above together with the SSIS.
    The method of scripting you mention above is quite good, and I use it to generate the script of the schema. It is very flexible, since I get to chose which objects and what granularity of detail to script. The scripting in SSMS also checks for dependencies and scripts the objects in a particular order.
    For moving data, though, I find it very clumsy, especially with larger amount of data.
    The best way to do it is to script the schema and then use SSIS to load the data. SSIS is very easy to use and it offers isolation and lock handling which speed up the process of the data loading.

    Feodor

    Reply
  • Sunil Somani
    May 7, 2011 5:46 pm

    how could we do same thing in sqlserver 2005

    Reply
  • @Sunil: the scripting of data is new for SQL 2008. In SQL 2005 you can script only the schema. Scripting the schema is sufficient, though; look at my comment above and you will see that you can use the scripted schema and then SSIS to move the data.

    Feodor

    Reply
  • Thanks Pinal And Feodor for the explanation on Scripting and loading of the data.

    Reply
  • Thanks for sharing. Nice picture guide.

    Reply
  • Hi,
    This is good way but if you have altered the table those schema is not coming when you use Generate Scripts. I’m using this only for generating the schema but many times the table that i altered are skipped(i.e. new columns or datatype changes are not reflected). I don’t know the reason But whethere I go to Design editor and alter or use command its not reflected. Have any of you faced this problem???

    Reply
  • All,

    I have generated .sql scripts from both the Studio Express tool as well as from a dos prompt and now I’m creating a .bat file to execute the .sql scripts. One script drops the database, the next script (schema/database) creates the database and tables and the last script is a data only script to insert the data back into the database/tables, when I execute the .bat the scripts all run, the database gets dropped, database & tables get created and it appears the data load script runs however the tables don’t get populated, I created a bat_log.txt file that captured the dos stuff and in there it tells me that XX rows affected (the xx number matches the number of rows I expect in each tables) however the data doesn’t get into the database tables, any ideas why?

    thanks,

    ~Scott.

    Reply
  • We just perform a backup of the source db & then restore it to the target server.

    About all that’s left to do then is clean up the list of user accounts and reset permissions.

    It’s not especially elegant, but it’s fast, and it works every time.

    Mike

    Reply
  • if i want new Database not same name. can i change name on script ?

    Reply
  • Thanks, I was losing my mind over the “ADVANCED” button !!!

    Reply
  • What about doing the opposite? Can we not script the database with all objects from SQLCMD?

    Reply
  • Thanks.

    Reply
  • How can I apply filter on table to create data script of selected data?

    –Shalini

    Reply
  • I am using SQL Server 2008 but the Choose Objects page in Generate Scripts does not display DDL Triggers as shown above, and these are what I want to script. Any ideas??

    Reply
  • It appears there is no longer any option to script the actual Create Database statement?

    Reply
  • (& wow thats one cranky commenting system)

    Reply
  • I have learned something new, and this is very good as far as it went. However, I’m missing something. I used the generate script to copy the users and schemas for one DB on a server to a clone on another server. I used the drop and create option. After it executed, the hundreds of securables for the users where I generated the script are missing from my target database.How can I copy this data for the database users?

    Reply
  • I am working on an instance of SQL Server Standard Edition and the Script Wizard Choose Object Types screen only has Tables and Users. The other object types are not there to be selected. Any idea why this is?

    Reply
  • Hi Pinal,
    I am genertaing Sql Script for all objects like Tables, Storedprocedure,Triggers, Function etc. Its Generating Scuccessfully and showing all the objects as scuccess in the final report but some of the tables are missing in the script. I had selected all the objects and aslo showing those objects scuccess in the report. Can u please Help me to find the reason that why this happening.

    Reply
    • Hello, this is happening to me too :(.
      Dear Pinal, do you know why could it happen? We are dealing with a big database and many objects are missing :(

      Reply

Leave a Reply