tags:

views:

42

answers:

1

Hi,

I have a large database that were used to archive tables before implementing structural change on it.

We had this database for years, I want to create a dynamic script to check today's date and drop any table in this database that were created 3 years or older.

Thanks

A: 

For MS SQL Below script may serve your purpose

select 
    'drop table  [' + s.name +'].[' + t.name +']' , t.create_date 
from 
    sys.tables t
inner join
    sys.schemas s
    on
    s.schema_id = t.schema_id
where 
    create_date< DATEADD(year,-3, GETDATE())
     and type='U'
Aamod Thakur
If there are any foreign key constraints on any of the tables, they would need to be dropped before this script would work. Otherwise, you have to drop the tables in the correct order to satisfy the existing referential integrity.
Joe Stefanelli
exactly! thnx for the comment :)
Aamod Thakur