SQL SERVER – SSMS – Script Out Multiple Objects

Have you ever driven your car out in the direction of your office by mistake when it was supposed to be a day off? Have you ever done things unconsciously because you are getting trained for something that is common? This is what I call as human nature and how our brain works. Our brain is a complex organ and it used patterns and preset paths of execution when it is pre-occupied. This is the path of least resistance and it may sometimes not be the best option too. In this blog post we will learn how to script out multiple objects in SQL Server.

If you are wondering why I am talking about this. Let me give you an example on a tool that I used almost every single day – SQL Server Management Studio (SSMS). I use the Object Explorer and multiple other windows that are available. I get intrigued how this tool works because there is something to learn or unlearn when it comes to working with SSMS.

I was talking with a junior DBA and he wanted to script out multiple objects. My initial reaction was to guide him and he would learn. I told him to do it using SSMS. After an hour he came back and told me it is not possible. It was important for me to show him how. I asked him to do the steps on my PC.

SQL SERVER - SSMS - Script Out Multiple Objects ssms-script-multiple-objects-01

He opened the Object explorer and said he could right click each object and script out. He could use a wizard etc. But he said it was not an easy step. He claimed to have worked with some 3rd party tool in the past that did help him. But I insisted there is something he was missing and I did have an old trick that I wanted to show him.

I opened in the same window the “Object Explorer Details” (F7 shortcut). In this window, I selected multiple objects (based on where the cursor is in Object Explorer and showed how we can export the script.

SQL SERVER - SSMS - Script Out Multiple Objects ssms-script-multiple-objects-02

As shown above, it is a simple script that got generated in one step and he was surprised it was hidden in SSMS. He was trying to use the CTRL, SHIFT keys on the Object Explorer pane to select multiple objects and it failed. He assumed now that it was not possible. He said he is going to explore SSMS further and there were areas that he was not aware.

Have you ever used this feature before? Been there with SSMS for years now. I find it handy and felt this was lesser appreciated feature.

Reference: Pinal Dave (https://blog.sqlauthority.com)

, ,
Previous Post
SQL SERVER – Installation Error – [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
Next Post
SQL SERVER – Database Mirroring Login Attempt Failed with Error ‘Connection Handshake Failed’

Related Posts

11 Comments. Leave new

  • Muhammad Idrees
    January 5, 2017 12:34 pm

    Thanks Pinal for sharing this trick, I was thinking of this I there could be any possibility for scripting multiple objects but don’t know that it already exists. Although we can also generate script by right click on database > Tasks > Generate scripts etc… but that process is a bit lengthy. One question if you could suggest me something, there is an option if you want to add an IF check if that object is not already exists. Is there any way we can add that check here?

    Reply
  • Thanks Pinal. I was not aware of this trick. I used to do “Generate Script task”. :)

    Reply
  • No Muhammad. I don’t think so.

    Reply
  • Michelle Haarhues
    January 12, 2017 7:02 pm

    Thanks for the tip! It is so simple yet one of the best little secrets in SQL Server. This is a great tool to know.

    Reply
  • when generating scripts of multiple objects (tables) in SSMS, it fails. For only one object (table) it works.

    Reply
  • I think what kevin is noting is that right clicking on an individual table and using SCRIPT AS –> CREATE or DROP AND CREATE will get you all the options for the table (e.g. the primary key definitions, etc.). If you do this for tables in Object Explorer Details, it will only give you a simple CREATE TABLE statement, without any of the objects that control/constrain the table. Take a look Pinal – if you see the same thing, maybe let Microsoft know? Granted, I am using SQL 2012, so maybe this was fixed, but see if the same happens for you?

    Reply
  • A big thanks to you!

    Reply

Leave a Reply

Menu