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 2005 Forums
 Transact-SQL (2005)
 Delete Tables

Author  Topic 

shapper
Constraint Violating Yak Guru

450 Posts

Posted - 2008-02-26 : 18:40:19
Hello,

To delete all tables from my database I use:
SELECT 'DROP table ' + table_NAME
FROM INFORMATION_SCHEMA.tables

To get the list of the tables. Then I run that list:
DROP table Folders
DROP table Files
...

Because of relationships I need to run it various times until all the tables disappear. Is there a way to everything in one command?

Thank You,
Miguel


SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-02-26 : 18:44:46
You could try something like this
WHILE EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES)
EXEC sp_msforeachtable 'drop table ?'



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

shapper
Constraint Violating Yak Guru

450 Posts

Posted - 2008-02-26 : 19:11:23
It worked fine.

Just one more thing.
Is it possible to do the same for the functions and triggers?

For functions and procedures I am trying the following

WHILE EXISTS (SELECT * FROM INFORMATION_SCHEMA.Routines WHERE ROUTINE_TYPE = 'FUNCTION')
EXEC sp_msforeachfunction 'drop function ?'

For procedures I would use "procedure" as routine type.

I know this isn't working because I there is no such procedure as sp_msforeachfunction.

How could I do it?
Probably just deleting all routines?

And where can I find information about the procedures you are using:
sp_msforeachtable, ...?

Thank You,
Miguel

Go to Top of Page

rmiao
Master Smack Fu Yak Hacker

7266 Posts

Posted - 2008-02-26 : 21:25:52
Sp_msforeachtable is non-documented, and you can use cursor to drop functions and triggers.
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-02-27 : 02:08:16
Refer this
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=65341

Change truncate and delete to DROP and remove other stuffs

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -