Solved

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

Posted on 2014-10-10
9
726 Views
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?

Thanks!
0
Comment
Question by:BlakeMcKenna
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 48

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.
0
 
LVL 65

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.
0
 
LVL 26

Expert Comment

by:Zberteoc
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?
0
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 

Author Comment

by:BlakeMcKenna
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.
0
 

Author Comment

by:BlakeMcKenna
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.
0
 
LVL 26

Accepted Solution

by:
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.

Done.

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.
0
 

Author Closing Comment

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

Thanks so much!
0
 
LVL 65

Expert Comment

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

Very handy... thanks for posting this.
0
 

Author Comment

by:BlakeMcKenna
ID: 40373732
It sure helped me!!!
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Audit has been really one of the more interesting, most useful, yet difficult to maintain topics in the history of SQL Server. In earlier versions of SQL people had very few options for auditing in SQL Server. It typically meant using SQL Trace …
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

856 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