Question: How do I list stored procedures modified in the last seven days? Query their catalog timestamps in the target database, using an explicit time window and schema names.

I like this question because I have used the query outside interviews. During a security incident at a consulting client, recently modified procedures helped us find suspicious code. A timestamp was a starting point for investigation, not a record of who made the change.
-- Original calendar-boundary test, retained for historical comparison:
SELECT name,modify_date,create_date FROM sys.objects
WHERE type='P' AND DATEDIFF(day,modify_date,GETDATE())<7;
-- Rolling seven-day window, with one fixed current-time value:
DECLARE @Now datetime=GETDATE();
SELECT SCHEMA_NAME(schema_id) AS SchemaName,name,modify_date,create_date
FROM sys.procedures
WHERE is_ms_shipped=0
AND modify_date>=DATEADD(day,-7,@Now) AND modify_date<=@Now
ORDER BY modify_date DESC,SchemaName,name;
-- Matching timestamps, not proof of an unchanged lifetime:
SELECT SCHEMA_NAME(schema_id) AS SchemaName,name,modify_date,create_date
FROM sys.procedures
WHERE is_ms_shipped=0 AND modify_date=create_date
ORDER BY SchemaName,name;
Choose what “last seven days” means
DATEDIFF(day,…) counts day boundaries. It is not a precise elapsed-hours test. The revised predicate uses a rolling seven-day window ending at one captured GETDATE value. SQL Server’s catalog timestamps are local server times, so compare them consistently.
sys.procedures includes SQL and CLR procedures; the original type=’P’ query selected T-SQL procedures only. Metadata visibility follows your permissions. An empty result means no visible matching rows, not proof that no procedure changed.
What if creation and modification dates are equal?

The final query preserves the original follow-up exercise, but describe its result accurately: the timestamps are equal. That alone does not prove a procedure has never been modified during its entire lifetime. Objects can be dropped and recreated, and a last-modified field is not a change history.
For the incident scenario, compare the actual definition with a trusted deployment copy and examine retained audit evidence. modify_date does not identify an actor, explain intent or store earlier procedure text.
DATEDIFF documents the boundary calculation. Do you have a diagnostic script you use often? Share it in the comments or on Twitter.
Published by Pinal Dave on SQLAuthority. More of my work at pinaldave.com.
Discover more from SQL Authority with Pinal Dave
Subscribe to get the latest posts sent to your email.





12 Comments. Leave new
I always use the first script to get a list of stored proc which recently modified. You wrote about this in past and I found that article very useful. Thanks.
I use a script in my daily tasks to find the list of places a particular text is used :
SELECT DISTINCT o.name AS Object_Name,o.type_desc
FROM sys.sql_modules m
INNER JOIN sys.objects o
ON m.object_id=o.object_id
WHERE m.definition Like ‘%TextToSearch%’
This website new UI is simply superb.
I typically use the following query to get the last modified stored procedures..
SELECT *
FROM sys.procedures
WHERE DATEDIFF(D,modify_date, GETDATE())>7
ORDER BY modify_date DESC
I have a question. I like to know if any stored procedures not being used or called so that I can cleanup them as we have thousands of SP in a database. Do we have any scripts to find them?
How is this query done in sql server 2000? I wasn’t able to get the same information from ‘dbo.sysobjects’ table.
I would recommend to use [INFORMATION_SCHEMA].[ROUTINES] system view instead of directly querying sys.objects
The modified date is also updated when the SP or function is set to be recompiled the next time it is executed.
(..sp_recompile ”)
Therefore the modified date does not always give you a true reflection of when actual code changes where made to the SP or function. This is unfortunate, since a recompile is sometime required for performance purposes, while it is not an actual code change.
How can we can find last 5 modification history for a particular procedure since sys.objects will show only the one which was modified last.
NICE ARTICLE
helped me today and always!!!!!
This there is any way to know the last user executed the stored procedure? Thanks in advance!
Yes, knowing WHO did the change would be very useful…..
Hi Team,
How can we write trigger on system tables like sys.procedures.
have tried many times but unable to write, getting error invalid for this operation.