Script: How to delete Table created before some date


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.

LVL 21
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

pinaldaveAuthor Commented:
I have moved this question to MSSQL as it was in wrong TA.
pinaldaveAuthor Commented:
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 ''?'''      
u can get the datecreated from SYSOBJECTS crdate column
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
change it to

and crdate <= dateadd(m,-1,getDate())
pinaldaveAuthor Commented:
Wonderful Thank you :)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.