Automatically create scripts for Tables & Stored Procedures in SQL Server 2008?

Posted on 2014-10-10
Last Modified: 2014-10-10
I know when making modifications to Tables or SP's, you generally right click on the entity and select Modify. There is also a menu option in the popup menu that says "Script Table as..." or "Script Stored Procedure as..." and then the sub-menu item of "Create In...". When you select "Create In...", it creates an editable SQL script. Is it possible to automate that for each Table/SP rather than doing it one at a time for each one?

Question by:BlakeMcKenna
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
  • +1
LVL 51

Expert Comment

by:Vitor Montalvão
ID: 40372950
Yes. Instead of right-click on the table, try it on the database, then chose Tasks / Generate Scripts...
Now you can chose which objects you want to be scripted. Just follow the wizard.
LVL 66

Expert Comment

by:Jim Horn
ID: 40372963
Not that I'm aware of in SQL 2008, but using Visual Studio 2010 you can create a SQL Data Project
Visual Studio 2010, SQL Server Database Project
Then go to Schema Compare, point to any database as a source, then point to any other database as a destination, and you can with one button-click script out all objects from source to destination.
LVL 27

Expert Comment

ID: 40373040
What do you mean by automate? Do you want to do this at some interval and save the output in some files? Is it for some kind of source control purpose?
10 Questions to Ask when Buying Backup Software

Choosing the right backup solution for your organization can be a daunting task. To make the selection process easier, ask solution providers these 10 key questions.


Author Comment

ID: 40373293
Let's say you right click on a Table Object and the menu pops up. You select "Script Table as"/"Create To"/"File". That will produce SQL Code to a file of your choice. Instead of doing that for each object, is there a way to be able to do that for all Objects within a specific Database?

It's essentially scripting out all the Tables, Stored Procedures, etc. into a File, Clibboard, etc.

Author Comment

ID: 40373324
Hi Jim,

I think I've almost got it. My problem is that there are no other DB's I can choose as a destination and I don't have "Create" privileges to create one.
LVL 27

Accepted Solution

Zberteoc earned 500 total points
ID: 40373430
In Management Studio Object Explorer:

Right Click on the database name > Tasks > Generate Scripts > Next > Leave on "Select Entire Database and all database objects" (if you choose the other oprion you can specify group and/or individual objects) > Next > Leave on "Save to File" and "Single File", choose a file name and location > Next Next> Finish or Close.


You may want to go into Advance settings before you save that file and under the table preferences make sure the indexes, unique keys, triggers are set to True.

Author Closing Comment

ID: 40373618
Wow...that was awesome!!!!

Thanks so much!
LVL 66

Expert Comment

by:Jim Horn
ID: 40373645
>Right Click on the database name > Tasks > Generate Scripts > Next > ...

Very handy... thanks for posting this.

Author Comment

ID: 40373732
It sure helped me!!!

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is basically a blog post I wrote recently. I've found that SARGability is poorly understood, and since many people don't read blogs, I figured I'd post it here as an article. SARGable is an adjective in SQL that means that an item can be fou…
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question