Script: How to delete Table created before some date

Posted on 2006-03-23
Last Modified: 2008-02-01

I have Database named MyDatabase.
We have to create the tables for certain reasons. Though, we have to use those table for some days only. After that the table is considered as expired.
I want to delete the tables which are created before certain date. Example would be one month ago.
All our tables starts with "MyTmp_"?

How can I do using a script. I can write scheduled job which does this once a month.

Kindly guide me.

Question by:pinaldave
    LVL 22

    Author Comment

    I have moved this question to MSSQL as it was in wrong TA.
    LVL 22

    Author Comment

    I can use this syntax to retrive all the size of the table. Can I also retrive the datecreated of the table ?

    EXEC       sp_msforeachtable 'sp_spaceused ''?'''      
    LVL 10

    Expert Comment

    u can get the datecreated from SYSOBJECTS crdate column
    LVL 10

    Accepted Solution

    i think tis shud suffice

    Set noCount on
    Declare @lcl_name varchar(100)

    Declare cur_name cursor for
    Select name
    from sysobjects
    where type = 'U'
          and crdate <= dateadd(m,1,getDate())
          and name like 'MyTmp_%'

    open cur_name
    fetch next from cur_name into @lcl_name
    While @@Fetch_status = 0
          Select @lcl_name  = 'Drop table ' +@lcl_name
          exec (@lcl_name )

    fetch next from cur_name into @lcl_name

    close cur_name
    deallocate cur_name
    Set noCount off
    LVL 10

    Expert Comment

    change it to

    and crdate <= dateadd(m,-1,getDate())
    LVL 22

    Author Comment

    Wonderful Thank you :)

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
    Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
    Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
    Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now