Wednesday, March 7, 2012

Generate SPs as text

Is there an easy way to loop through every stored procedure in a
database and create a file containing all of the SP code?
lq> Is there an easy way to loop through every stored
> procedure in a database and create a file containing
> all of the SP code?
> lq

I would suggest one of three ideas (in order of ease of use and
compatibility across versions of SQL Server):

1. Enterprise Manager can do this for you

2. You can write a program using SQLDMO to do this. If you've never used
SQLDMO before, you'll want to loop through the StoredProcedure objects in
the Database object. You'll also want to look up the SystemObject property
and the Script method. In addition, we've found that it's easier to have
SQLDMO script the actual proc, while we handle adding script for dropping
the procedure before creating and for granting permissions (but this will
depend on how you control SP's and their permissions).

3. See the technique I mentioned in the thread "Search contents of stored
procedures?" for a down-and-dirty (not to mention fragile) technique.

Craig

No comments:

Post a Comment