How to generate scripts automatically in SQL Server

20. December 2013 SQL Server 0

In this article I’m going to explain how you can generate CRUD (Create, Retrieve, Update, and Delete) statements and other queries such as create database, tables, and etc. automatically using SQL Server Management Studio.

There are different ways to achieves these. Imagine we have a simple table called Students as below.

 

Generate CRUD queries 

Generate Create and Drop Table queries

Generate Alter query

Generate create database script

 

 

Generate CRUD queries

Right click on the desired table, Script Table as, SELECT To, and choose whether you want so generate the script in new Query Editor Window, save to file, or copy in clipboard.

I chose New Query Editor Window so SQL Server will be generating the select statement for me.

 

Generate INSERT, UPDATE, DELETE will follows the same steps.

Generate Create and Drop Table queries

In the same way you as generating CRUD queries you can generate Drop and Create table queries which for our Student table would be as below:

Generate Alter query

 

Generating Alter queries are a bit different. To do so open your table in design mode (right click on table name, select Design).

Rename one of the columns or change its Data Type.

After the changes right click on the column and select Generate Change Script.

A new Window will be opening which contains the Alter script.

You can check the Automatically generate change script on every save so SQL Server will prompt this window on every change.

In the same way you can generate Alter query for adding or removing columns.

Add a new column Phone of type nchar(20).

Right click on any column and select Generate Change Script. You will get the Alter query.

In the same you can get queries for deleting a column:

Set a column to be Primary Key.

You can generate queries on any other changes in the same way.

Generate create database script

SQL Server allows you to generate query for the whole database. That is to create all tables (or selected tables) and the data they contain.

To do so right click on the database, select Tasks, Generate Scripts.

A new window which helps what kind of query you want to generate will be appearing. Select Next on the welcome screen.

On the Choose Objects screen you can proceeds to generate script for the entire database (includes Tables, Views, Stored Procedures, Users, and etc.) by choosing Script entire database and all database objects, or select the object (Table, View, etc.) which you want to generate script by selecting the option Select specific database objects option.

Let’s select the second option and only create script for the Student table.

Click Next.

On the next screen you can choose how to generate the script. Whether to save into a file, copy in clipboard, or open in new query window. Choose the last option.

By default SQL Server will only generate script for you schema, that is in this example will only be getting the create tables and columns query, but the data we have. We need to tell SQL Server we want query for data as well. To do so click Advanced button.

Scroll down to Types of data to script.

The default value is Schema only. You can choose other options:

  • Data Only: will be generating script which includes INSERT statements of all the records in the table.
  • Schema and data: will be generating script which includes the table creating script as well as INSERT statements of all the records in the table
  • Schema Only: will only be generating script for table definition.

Select the Schema and data for our sample and press OK. Press Next, Next, and Finish.

Your query is ready:


Leave a Reply

Your email address will not be published. Required fields are marked *