Monday, June 16, 2008

When was a SQL Server SP or Table or View last altered?

You can get the created date and last modified date of Stored Procedures, user created tables and views using the following queries in SQL Server 2005.

Stored Procedures:
SELECT [name], create_date, modify_date
FROM SYS.Objects WHERE type='P'

Alternatively you get can use the below query to get the same information
SELECT [name], create_date, modify_date FROM sys.Procedures


Tables:
SELECT [name], create_date,
modify_date from SYS.Objects WHERE type='U'

An alternate query
SELECT [name], create_date, modify_date FROM sys.tables


Views:
SELECT [name], create_date,
modify_date FROM SYS.Objects WHERE type='V'

An alternate query
SELECT [name], create_date, modify_date FROM sys.views


Of course, in all the above queries you can add a WHERE clause to limit the results. Something like

SELECT [name], create_date, modify_date
FROM sys.views WHERE modify_date > DATEADD(day,-30,GETDATE()) Order By modify_date DESC


Remember catalog views were first introduced in SQL Server 2005, so these queries will not work in SQL Server 2000.

No comments: