SQL Server Management Studio (SSMS) is a great tool for managing instances of your SQL Server databases. Sometimes, though, the fastest way to retrieve information you are looking for isn’t through the SSMS GUI, but through direct SQL queries.
Case in point: if you want to search for specific information about stored procedures in your database. Rather than look one by one through the code, you can query the SQL Server sys.objects and sys.procedures tables to find exactly what you are looking for.
To search for stored procedures that were created or modified on a specific date, you can directly search either the sys.objects and sys.procedures tables.
Note: I have written and tested the example queries below in SQL Server 2005 and SQL Server 2008. The syntax for earlier or later platforms may vary. For example SQL Server 2000 does not support the sys.objects and sys.procedures tables.
Using the sys.objects Table
To query the sys.objects table and order by the most recently modified stored procedures use the following syntax:
USE DatabaseName GO SELECT modify_date, name, create_date FROM sys.objects WHERE type = 'P' ORDER BY Modify_date desc GO
It’s important in this example to remember to include the where clause filter for type=p so that only stored procedures are returned.
For reference purposes here are the types that the sys.objects table holds:
Using the sys.procedures Table
You can run exactly the same query against the sys.procedures table. Since this table is dedicated to storing detailed information about your database’s stored procedures you do not need to include a type filter with your query.
USE DatabaseName GO SELECT modify_date,name,create_date FROM sys.procedures ORDER BY modify_date DESC GO
Clearly, the sys.procedures table is quite useful, but you can use it to query more than just the name and create/modify dates.
For example, you can query the sys.prodedures table for stored procedures that contain specific text. This is really a handy function that can be useful if you are combing through an unfamiliar database or want to find very specific code quickly. You can even return the text of the procedure directly in your query so that you can quickly analyze each of the returned results quickly without having to find and open the procedure in SSMS.
Here is an example query that filters stored procedures to return only ones that contain the SQL CHARINDEX function:
USE DatabaseName GO SELECT modify_date,name,create_date FROM sys.procedures WHERE OBJECT_DEFINITION(OBJECT_ID) LIKE '%charindex%' ORDER BY modify_date DESC GO
Is it Possible to See the Create/Modify Date in SQL Server 2000?
SQL Server 2000 uses a system table called sysobjects to store information about stored procedures, views, tables, etc. This table offers a more rudimentary set of information than the sys.objects table does in SQL Server 2005 and up.
So to answer the question; yes, you can see the create date of a stored procedure in the SQL Server 2000 sysobjects table. However you cannot query the modify date since this is not tracked.
SQL Server 2000′s sysobjects table contains two date columns called crdate and refdate. You can query the crdate to see the actual create date, but refdate does not show information that is different from the crdate column. Online help for SQL Server 2000 indicates that the refdate is unsupported and for informational purposes only, so I would suggest not putting too much trust into that field.
A manual solution to store the modify date in SQL Server 2000 would be to script each procedure with a drop statement and then to create it anew along with a historical comment indicating the update date as well as the initial create date. Thus the crdate column would show the latest modification date, and the associated comments would fill in the historical information.
Keeping a manually maintained log of changes and the reasons behind them in comment form in the header of a stored procedure is a good idea in general even in more modern versions of SQL Server. Such comments can be used as documentation to track modifications and the business or technical reason for the modification.
Here is an Example of How to Query the sysobjects Table in SQL Server 2000:
SELECT name,crdate FROM sysobjects WHERE type = 'P' ORDER BY crdate DESC
As you can see, SQL Server offers many useful functions for quickly searching through information in your database. Although SSMS offers a friendly and easy to use UI, it is often more efficient to query information about stored procedures in your database directly via system tales such as the sys.objects and sys.procedures tables.
Similar functionality exists for other SQL Server system objects such as views, tables, or functions. I hope to cover these and more in future articles.