Delete rows automatically in a table

LillaMy 21 Reputation points

I I want the rows where the EndDate column has passed today's date to be automatically removed from a table in SQLServer.

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
11,549 questions
0 comments No comments
{count} votes

Accepted answer
  1. AmeliaGu-MSFT 13,936 Reputation points Microsoft Vendor

    Hi LillaMy-9261,

    Agree with others.
    You can use SQL Server agent job and write a stored procedure that deletes every rows that date is passed today's date.
    For example:

    CREATE  PROCEDURE DeleteRows()  
    DELETE FROM Table  
    WHERE DateColumn < GETDATE()  

    Please check Create a Job and Schedule a Job which might be helpful.

    Best Regards,

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Tom Phillips 17,681 Reputation points

    You would have to create a SQL Agent job to run every day to delete those records.

    0 comments No comments

  2. Guoxiong 8,126 Reputation points

    Any idea why the data of the EndDate column passed today's date have to be deleted? How do you check the historical data if you do? I would suggest to add a flag column to indicate those data, i.e., Is_Active = 0 or Is_Completed = 1.