Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 546197
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T10:50:29+00:00 2026-05-13T10:50:29+00:00

I want to create a Stored procedure and Job in Ms SQL server that

  • 0

I want to create a Stored procedure and Job in Ms SQL server that would delete all tables in a schema that are more than a weeks old.

i create backups every single day and i want to automate the process by scheduling a job to delete any backups(SQL Tables) that are older than a week.

Your help would be greatly appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T10:50:30+00:00Added an answer on May 13, 2026 at 10:50 am

    You can use the sys.objects keyword within SQL Server to accomplish this.

    The query would be something like:

    USE [Your_Database_Name];
    GO
    SELECT name AS object_name 
      ,SCHEMA_NAME(schema_id) AS schema_name
      ,type_desc
      ,create_date
      ,modify_date
    FROM sys.objects
    WHERE create_date > GETDATE() - [No_Of_Days_Old]
    ORDER BY create_date;
    GO
    

    This above example is a slight variation on the first example shown on the MSDN page that details the sys.objects keyword (A. Returning all the objects that have been modified in the last N days).

    That page can be found here:

    sys.objects (Transact-SQL)

    and is a great resource for many different methods of querying the “metadata” of your database objects.

    Of course, the above will simply “SELECT” the table name that will need to be deleted and won’t actually delete (or DROP) the table from your database.

    To achieve this, you will need a mechanism to issue a DROP TABLE command. Unfortunately, the DROP TABLE command won’t accept a parameter valued table name (i.e. You can’t do DROP TABLE @tablename), but you can build a string/varchar of a complete T-SQL statement and EXECUTE it).

    To achieve this, you can use a CURSOR to loop through the results of the earlier SELECT statement, building a new T-SQL command in a string/varchar that will drop the table name. An example of this is below:

    DECLARE @tname VARCHAR(100)
    DECLARE @sql VARCHAR(max)
    
    DECLARE db_cursor CURSOR FOR 
    SELECT name AS tname
    FROM sys.objects
    WHERE create_date > GETDATE() - 7
    
    OPEN db_cursor  
    FETCH NEXT FROM db_cursor INTO @tname  
    
    WHILE @@FETCH_STATUS = 0  
    BEGIN  
           SET @sql = 'DROP TABLE ' + @tname
           --EXEC (@sql)
           PRINT @sql
    
           FETCH NEXT FROM db_cursor INTO @tname  
    END  
    
    CLOSE db_cursor  
    DEALLOCATE db_cursor
    

    Note that in the above example, I have commented out the line EXEC (@sql). This is the line that actually executes the T-SQL statement in the @sql variable, and since it’s a destructive command, I simply commented it out and used a PRINT @sql command instead (the line below). Run this as is, to see what tables you’re likely to delete, and when you’re happy, uncomment the EXEC (@sql) command and comment out the PRINT @sql command!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to create a stored procedure (on SQL Server 2005) that fetches a
I am using SQL Server 2005. I want to create a stored procedure that
I want to do the following using SQL Server 2005. Create a stored procedure
I want to create stored procedure that call recursion function, each time it will
I want to create a stored procedure on ORACLE database server and my problem
I want to create SQL statement (probably a stored procedure) to insert multiple rows
I am using SQL Server 2008 R2 and I want to create a stored
I want to create a stored procedure that takes the name of a table
I want to create a stored procedure that returns the results of a search
I would like to create a stored procedure in MySQL that took a list

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.