List All the Stored Procedure Modified in Last Few Days – Interview Question of the Week #070

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.

A fresh sheet replaces an older sheet in a stack, marked by a small vermilion bookmark

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;
Original DATEDIFF query finds a recently modified uspGetBillOfMaterials
Original historical DATEDIFF example. The newer query below it in the copyable script uses a rolling window instead of counting midnight boundaries.

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?

Original procedures with equal create_date and modify_date timestamps

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.

SQL Scripts, SQL Server, SQL Stored Procedure
Previous Post
SQL SERVER – Q&A: SQL Clustering Virtual Server Name and Instance Name
Next Post
Primary Key and Null in SQL Server – Interview Question of the Week #071

Related Posts

12 Comments. Leave new

  • Arnab Roy Chowdhury
    May 8, 2016 10:33 am

    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.

    Reply
  • turjachaudhuri
    May 8, 2016 3:00 pm

    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%’

    Reply
  • 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

    Reply
  • Raj Rajaraman
    May 9, 2016 7:08 pm

    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?

    Reply
  • Bernstein S.
    May 10, 2016 7:16 pm

    How is this query done in sql server 2000? I wasn’t able to get the same information from ‘dbo.sysobjects’ table.

    Reply
  • fregatepallada
    May 13, 2016 9:09 am

    I would recommend to use [INFORMATION_SCHEMA].[ROUTINES] system view instead of directly querying sys.objects

    Reply
  • 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.

    Reply
  • 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.

    Reply
  • Dipti Thakurpti
    November 4, 2019 10:30 am

    NICE ARTICLE
    helped me today and always!!!!!

    Reply
  • This there is any way to know the last user executed the stored procedure? Thanks in advance!

    Reply
  • Yes, knowing WHO did the change would be very useful…..

    Reply
  • 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.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.