Monday, April 29, 2013

SQL Server – 2008 – Generate Script of Database, Generate script to copy whole database schemas and All other object in database, like Tables with data, SPs, Views, Triggers, Functions, Database constraints etc.


While we are working on any project and uses SQL Server as a database, we often need to copy database schema either with data or without data to create database on other server. 
Here I will explain step by step process with screen shot that how we can copy database schema with all the database objects.
First of all open SQL Server Management Studio and expand databases node like below images step 1.
Step 1
Now select the database for which you want to generate SQL Script as like below image Step 2. Here I have selected pubs database for demo. Right click on selected database, click on Tasks option and then click Generate Scripts...  I have marked option with red circle in image.
Step 2
Once you click on Generate Scripts option from above image it will show up the below image step 3
Just click on the next button from below image step 3
Step 3
Now in below image you can see the list of all the available databases, by default on which database you have right clicked it is selected but you can change here if you want to generate script for other database. In Step 4 image there is one check box labelled with ‘Script all objects in the selecteddatabase’.  Check this check box if you want to generate script for all the objects from database, if you will not check this check box it will ask on next step for database object selection.
Step 4
After selecting the database and checking check box it will show up the below image step 5.
Here you can select what you want to include in script, like script table with data or without data, you want to add script like if exist then drop the object first then create it etc. For scripting table with all the data select true in script data option as I have selected in below image. Then click on next button.
Step 5
Now this step 6 has some output file type and file location related options. I have selected 3rd radio button in below image for generating a script in New Query Window but if you don’t want to generate script in new query window and want to generate script into clipboard then select 2nd radio button labelled with Script to Clipboard. If you want to generate script in a file then select 1st radio button Script to file, once you select this option it will ask file name and directory to save the file on hard drive.
I have marked files to generate option as a 3 and then 3.1 and 3.2 in the red colour. If you want to generate whole script in one single file then selectsingle file (3.1), by default it is selected. If you want to generate different file per object then select 3.2 option. If you select 3.2 option and suppose you have 10 tables in database then it will generate 10 different SQL file.

Step 6
Below image is just a summary page, showing all the options you have selected till step. Just click on the finish button to proceed.
Step 7 showing the progress for generating database script
Step 8 showing the successfully completion of script generation process.
Step 9 
shows that all database object has been scripted in new query window for pubs database. Now your script is ready...

No comments:

Post a Comment