Taking Scripts From SQL Server

In this article, you will learn about taking scripts from SQL Server.

Taking scripts by right clicking each object

We can take the script by right clicking an object (database, tables, stored procedures etc) and selecting the script [object name], as screenshots given below.

  • For database

    SQL Server

    As you can see above, the screen shows you can have database script for CREATE, DROP, DROP and CREATE.
  • For Table

    SQL Server

    As you can see the above screen shows you can have table script for CREATE, DROP, DROP And CREATE, SELECT, INSERT, UPDATE, DELETE
  • For stored procedure

    SQL Server

    As you can see above, the screen shows stored procedures can have a script for CREATE,ALTER, DROP, DROP And CREATE, EXECUTE.

All of them can be taken inside Query Editor Window or we can save as file or we can copy the script without opening or saving the script by selecting Clipboard or we can schedule this script as an agent job.

Taking scripts by selecting generate script option

We can also generate the script by right clicking the database -> Tasks -> Generate Scripts. The screen is shown below.

SQL Server

After clicking Generate scripts, you can see a pop up coming, as shown below.

SQL Server

From this popup, click Next or select Choose Objects. Afterwards, you can see the screen, as shown below.

SQL Server

From this screen, we can select what objects we need to take to script. You can select particular objects by selecting “Select specific database objects” radio button, then we can see the below list is enabled. From there, you can select what you want to take to the script, or you can select above radio button “Script entire database and all database objects” to take all the database scripts except built in objects. By default, this radio button is selected.

After selecting your option, click Next and you can see the screen.

SQL Server

In this screen, you can see there are two output types, select first radio button “Save scripts to a specific location”. Now, you can see the group given below. There are 3 options of how we want this script. Second and third are already discussed. First one is “save to file”. Under this, we can see so many options.

  1. Files to generate

    Here we have two options

  • Single file
    Using this option, we can generate scripts in one file

  • Single file per object
    Using this option, we can generate scripts for each object in a separate file

  1. File name

    Here we can browse a location, where you wanted to save. If you select single file option from Files to generate, then you can give the script name as well.
  1. Save as

    Here, we can specify which text format, your script would be- Unicode text or ANSI text.

In this screen, you can see “Advanced” button. In this, we can choose some advanced options. For example, we can select in the script, where we need only structure (here schema) of the table, procedure, functions etc. or if we need only table data or both, the screen is shown below.

SQL Server

After selecting your desired options, click OK to save and close this Window, followed by clicking Next. Now, you can see the screen shown below.

SQL Server

It will tell you, what you selected. Click Next to start taking the script.

The screen is shown below.

SQL Server

After some time, you can see the result of this progress is shown below.

SQL Server

Click Finish to close this Window. Now, you can see your script file is ready in the location you chose.