March 12, 2009

List all the Stored Procedures of a Database and their Definitions using T-SQL in SQL Server 2005/2008

One way to Script the Stored Procedures of your SQL Server 2008 database is to use the SQL Server Management Studio (SSMS). Open SSMS > Your Database > Programmability > Stored Procedures > Right Click Stored Procedure Name > 'Script Stored Procedure As' > 'Create To'

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:



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

Did you like this post?
kick it on
subscribe via rss subscribe via e-mail
print this post follow me on twitter

About The Author

Suprotim Agarwal, ASP.NET Architecture MVP works as an Architect Consultant and provides consultancy on how to design and develop Web applications.

Suprotim is also the founder and primary contributor to DevCurry, DotNetCurry and SQLServerCurry. He has also written an EBook 51 Recipes using jQuery with ASP.NET Controls.

Follow him on twitter @suprotimagarwal



5 Responses to "List all the Stored Procedures of a Database and their Definitions using T-SQL in SQL Server 2005/2008"
  1. Andreas Paulsson said...
    March 20, 2009 at 1:45 AM

    If You want to script an entire database for the purpose of source code control (e.g. Subversion), try out ScriptDB : .

    It generate scripts in a folder structure similar to the tree structure in Management Studio, and it can also be used from the command line.

  2. Suprotim Agarwal said...
    March 20, 2009 at 9:24 PM

    Thanks Andreas for letting us know about ScriptDB!

  3. Madhivanan said...
    February 3, 2010 at 6:36 AM

    Another way to script procedures,triggers,etc to each file is


  4. Howard said...
    February 25, 2014 at 7:45 AM

    SELECT SPECIFIC_SCHEMA + '.' + [Routine_Name] AS ProcedureName, ROUTINE_DEFINITION

  5. David said...
    July 31, 2015 at 1:39 AM

    Amazingly useful piece of code. I would suggest
    modu.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).


Copyright © 2009-2014 All Rights Reserved for by Suprotim Agarwal | Terms and Conditions