The other way to script objects of your database in SQL Server 2005/2008 is to use the Microsoft SQL Server Database Publishing Wizard 1.1.
However if you need to do the same task programmatically using T-SQL, then here's the T-SQL that will help you do so:
USE NORTHWIND
SELECT obj.Name as SPName,
modu.definition as SPDefinition,
obj.create_date as SPCreationDate
FROM sys.sql_modules modu
INNER JOIN sys.objects obj
ON modu.object_id = obj.object_id
WHERE obj.type = 'P'
This query will list down all the stored procedures and their definitions of your database. In order to save the results of this SELECT statement to a text file, check this post of mine
Save SELECT query output to a text file
If You want to script an entire database for the purpose of source code control (e.g. Subversion), try out ScriptDB : http://www.codeplex.com/scriptdb .
ReplyDeleteIt generate scripts in a folder structure similar to the tree structure in Management Studio, and it can also be used from the command line.
Thanks Andreas for letting us know about ScriptDB!
ReplyDeleteAnother way to script procedures,triggers,etc to each file is
ReplyDeletehttp://beyondrelational.com/blogs/madhivanan/archive/2009/10/26/script-out-procedures-to-seperate-files.aspx
Madhivanan
SELECT SPECIFIC_SCHEMA + '.' + [Routine_Name] AS ProcedureName, ROUTINE_DEFINITION
ReplyDeleteFROM [INFORMATION_SCHEMA].[ROUTINES]
WHERE [ROUTINE_TYPE] = 'PROCEDURE'
Amazingly useful piece of code. I would suggest
ReplyDeletemodu.definition + 'GO' as SPDefinition
however, so that you can then copy+paste the results into SSMS and run them all (or make changes like I did).