Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 How can I drop multiple tables from SQL 2008?

Author  Topic 

misterdeey
Starting Member

19 Posts

Posted - 2012-03-30 : 14:38:21
I need to do a server cleanup ro drop any table that has not been used or updated for over 6 months.

To determine all impacted tables I'm using the below query:

SELECT OBJECT_ID,NAME,TYPE,CREATE_DATE AS CREATION_DATE,MODIFY_DATE AS DATE_MODIFIED
FROM SYS.TABLES
WHERE MODIFY_DATE<=GETDATE()-180
ORDER BY 5

Now how can I drop all these tables at once?

Thank you.

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2012-03-30 : 14:39:22
SELECT 'DROP TABLE ' + NAME
FROM SYS.TABLES
WHERE MODIFY_DATE<=GETDATE()-180
ORDER BY 5

Are you sure these aren't needed? Be sure you have good (restorable) backups!

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

misterdeey
Starting Member

19 Posts

Posted - 2012-03-30 : 15:03:28
I do have a protected flag on another local table and from which a filter will be used to avoid dropping the good tables :)


Thanks much for your help.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2012-03-30 : 15:09:57
You're welcome, glad to help.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2012-03-30 : 15:46:47
How does modify_date when a row has been inserted updated or deleted?

Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page

GilaMonster
Master Smack Fu Yak Hacker

4507 Posts

Posted - 2012-03-30 : 16:41:08
quote:
Originally posted by X002548

How does modify_date when a row has been inserted updated or deleted?


It doesn't.

From Books Online:

quote:
modify_date

Date the object was last modified by using an ALTER statement. If the object is a table or a view, modify_date also changes when a clustered index on the table or view is created or altered.


--
Gail Shaw
SQL Server MVP
Go to Top of Page

GilaMonster
Master Smack Fu Yak Hacker

4507 Posts

Posted - 2012-03-30 : 16:42:53
quote:
Originally posted by misterdeey

I need to do a server cleanup ro drop any table that has not been used or updated for over 6 months.

To determine all impacted tables I'm using the below query:

SELECT OBJECT_ID,NAME,TYPE,CREATE_DATE AS CREATION_DATE,MODIFY_DATE AS DATE_MODIFIED
FROM SYS.TABLES
WHERE MODIFY_DATE<=GETDATE()-180
ORDER BY 5

Now how can I drop all these tables at once?


That query will get you tables that have not been altered (ALTER TABLE) in the last 6 months. It could easily return the most active table in your database, the one that got several thousand inserts in the last 2 hours.

Sure that's what you want?

--
Gail Shaw
SQL Server MVP
Go to Top of Page

misterdeey
Starting Member

19 Posts

Posted - 2012-03-30 : 17:20:55
X002548 => That's where our local table that host all protected tables' names will come in play :)
Go to Top of Page

GilaMonster
Master Smack Fu Yak Hacker

4507 Posts

Posted - 2012-03-30 : 17:48:12
Are you looking for tables that have not had any data modification or access in a period of time or tables that have not had a schema (definition change) in a period of time (ignoring any protected table lists)?

--
Gail Shaw
SQL Server MVP
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2012-03-30 : 19:45:20
What an incredibly BAD idea

Good freakin luck

I got an idea..talk to all the people that you granted access to...oh wait..do they all use sa?

In any case, then ask them waht they are using

O..another idea..let's ASS U ME that you only allow Sproc access to the database...then just use sp_depends

No?

Wild West?

Good Luck

Sorry...rough day at work dealing with peipke with NO CLUE

Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page

Vinnie881
Master Smack Fu Yak Hacker

1231 Posts

Posted - 2012-03-30 : 20:29:23
Misterdeey,

Dropping tables that haven't been used in 180 days does not make much sense unless the database is filled with just arbitrary data that has no correlation to anything, and even then there are many questions as to WHY/How did you get so many tables that have no use and need to be dropped?

There is likely a much better approach to cleaning up your data/tables than what you have presented. Can you shed some light on why you have so many tables that nothing ties to or will need, and the logic behind why it will be OK to drop them if they have not been updated in 180 days?

Odds are someone here can point you in a better direction, but it sounds like a VERY RISKY way to clean up your data, and I'd suggest holding off until you get more information, and post the issue you are having along with how you have so many unnecessary tables in your database that you know are safe to remove..


Success is 10% Intelligence, 70% Determination, and 22% Stupidity.
\_/ _/ _/\_/ _/\_/ _/ _/- 881
Go to Top of Page
   

- Advertisement -