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.
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.
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)
11 Comments. Leave new
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?
If I remember correctly, it’s there in Wizard.
Yes its there in Wizard. Actually my question is that, is there any way to add that IF check using this (article) trick also.
Thanks Pinal. I was not aware of this trick. I used to do “Generate Script task”. :)
Thanks Karthiga. I learned recently and shared.
No Muhammad. I don’t think so.
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.
I agree Michelle.
when generating scripts of multiple objects (tables) in SSMS, it fails. For only one object (table) it works.
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?
A big thanks to you!