SQL Server – How to generate sql script from a db

If you are interested in DB development, then most probably you have tried to generate a sql script from your database. In this article I will show you how to do it with Microsoft SQL Server Management Studio.

sqlserver

So, lets imagine that you have created a database and you want to generate a script, that can be used by someone else to create your DB with SQL Server. There is one trick, that should be taken into account and that is the whole reason, why I am writing the article. Let’s start.

  • You have a database with two tables like this:

db

  • The name of the db is “School” so you go to it in the Object Explorer and with the right button pressing you select Tasks and Generate Scripts:

db2

  • By clicking Next> you get to the following window:

db3

  • Where you may select whether to get the script for the entire DB or just for some of the tables. I have selected entire for the current sample. Next is the whole reason I have written the article. It is a little hidden by Microsoft, but in order to get both the data and the schema of the db in the sql, you should click on the Advanced button:

db4

  • Where you should select from the”Types of data to script” Schema and data, as in the screenshot below:

db5

  • Click ok and click “Next a couple of times to get the sql file.

That is all folks! 🙂

Tagged with: , , ,