Generating Script in SQL Server 2012

This article demonstrates how to generate a script in a SQL Server database. In this article, we use a database containing tables and stored procedures and that database is used to another database in SQL Server using the Generate Script option. So let's take a look at a practical example of how to generate a script in SQL Server into SQL Server 2012. The example is developed in SQL Server 2012 using the SQL Server Management Studio. There are some simple steps to do that as described here.

Generating Script in SQL Server

Suppose we have a database named Test. Now right-click on the Test database and select the "Generate script..." option.

database Name -> Tasks -> Generate scripts....

Generate- scripts-option-in-Sql-Server.jpg

Now click on Generate scripts. The Generate scripts wizard will be opened.

Generate- scripts-Wizard-in-Sql-Server.jpg

Now click on the Next Button and select tables and stored procedures.

Select-Objects-from-generate-script-Wizard-in-Sql-Server.jpg

Now click on the Next Button and give the proper name with path of the file.

Creatie-Script-file-location-in-Sql-Server.jpg

Now click on the Next Button and review your source and target location.

source-and-target-path-generate-script-Wizard-in-Sql-Server.jpg

Now click on the Next Button.

Successfully-generate-script-window-in-Sql-Server.jpg

Now finally click on the Finish button.

The script file has been generated for the Test database. To see the generated script file, select the location of the file in your computer.

generated-script-file-in-Sql-Server.jpg

Creating a Database in SQL Server

These are the following steps to create a new database:

    1. Press F8 to open the Object Browser in SQL Server Management Studio and expend it
    2. Database -> right-click-> select New database
    3. This would open the "New database" window
    4. Now enter a database name to create a database
    5. Now click on the OK button to create the database. The new database will be shown in the Object Explorer

    Now the database, named DatabaseTest, has been created.

    New-Database-in-Sql-Server.jpg

     Copy Database Schema and Data to Other Database

    Now right-click on the script file and open it in Notepad and copy all the data and paste it in the query window in SQL Server. Looks as in the following:

    Default-Database-name-in-query-window-in-Sql-Server.jpg

    Now only change the the database name test to DatabaseTest.

    Query-window-with-change-database-name-in-Sql-Server.jpg

    Now press F5 to execute the script and expand the databaseTest to see the schema and data.

    Object-Explorer-in-Sql-Server.jpg


    Similar Articles