Search This Blog

Thursday, May 10, 2012

SQL 2005 - Finding and Listing ALL Procedures, Functions, and Views in SQL Server 2005

Below is the most basic coding to find out ALL of the Stored Procedures that exist within SQL Server 2005.

USE [your_database_name_here];
SELECT * FROM sys.all_objects
WHERE ([type] = 'P' OR [type] = 'FN' OR [type] = 'V')
ORDER BY [name];

This coding is based on filter by the object "type". Use the following table to determine the type of object you want returned, and then adjust the WHERE clause to only return those type(s) you wish to be returned. This listing only lists views, functions, and procedures. There are other types which you can also filter, be sure to review BOL for further information on the types.

 Type Of Object Returned
SQL Scalar Function
Inline Table Valued Function
SQL Stored Procedure
CLR Stored Procedure
SQL Table Valued Function
Extended Stored Procedure

User-Defined Stored Procedures

Now, one other field that is worthwhile to filter by is the "is_ms_shipped" field. This, obviously, determines if the SP was originally shipped by Microsoft. If the value of the field is 0 then it was NOT shipped by MS; if the value is 1 then it WAS shipped by MS. Simple enough. If you want to use it in conjunction with the above TSQL statement you will put in "AND [is_ms_shipped] = 0" to find SPs that were NOT created by MS, and most likely created by yourself or someone else with access to your database. If you ONLY want MS provided SPs then change the value from 0 to 1.
Here is a TSQL coding example of how to obtain only User-Defined SPs (using the previously mentioned "is_ms_shipped" field to obtain the listing). Note: This listing will definitely vary depending on what SPs you have installed. If you received no results then this indicates the database you are running your query on does not contain any SPs that were not created by MS...Try another database that you know you have created an SP for, or create an SP for your database and re-run the query.

USE [your_database_name_here];
SELECT * FROM sys.all_objects
WHERE ([type] = 'P' OR [type] = 'X' OR [type] = 'PC')
AND [is_ms_shipped] = 0
ORDER BY [name];

No comments: