Delete rows automatically in a table

LillaMy 21 Reputation points
2022-03-30T15:45:42.057+00:00

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.
14,124 questions
0 comments No comments
{count} votes

Accepted answer
  1. AmeliaGu-MSFT 13,991 Reputation points Microsoft Vendor
    2022-03-31T03:02:01.167+00:00

    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()  
    AS  
    BEGIN  
    DELETE FROM Table  
    WHERE DateColumn < GETDATE()  
    END  
    

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

    Best Regards,
    Amelia


    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,741 Reputation points
    2022-03-30T15:54:28.77+00:00

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

    0 comments No comments

  2. Guoxiong 8,206 Reputation points
    2022-03-30T16:13:19.833+00:00

    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.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.