Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Removing all tables
Message
 
To
29/02/2008 11:16:35
General information
Forum:
Microsoft SQL Server
Category:
Other
Miscellaneous
Thread ID:
01297747
Message ID:
01297749
Views:
19
>Recently, I have started a thread about removing all tables from a database. The approach of sp_MsForEachTable was proposed. However, occasionnaly, my server doesn't recognize this stored procedure. I need to have something bullet proof. Another thread I created today was about the ability to check the existence of one particular table and remove it from the database if found. This is also good. Now, I need some code to select all tables from the database and loop delete each of them. Is such a script possible?
USE YourDataBase
DECLARE @TableName AS varchar(8000)
DECLARE @sql       AS varchar(8000)
SELECT @TableName = MIN(Table_Name) FROM INFORMATION_SCHEMA.Tables
WHILE @TableName IS NOT NULL
      BEGIN
          SET @sql = 'DROP TABLE '+@TableName
          EXEC (@sql)
          SELECT @TableName = MIN(Table_Name) FROM INFORMATION_SCHEMA.Tables WHERE Table_Name > @TableName
      END
Against Stupidity the Gods themselves Contend in Vain - Johann Christoph Friedrich von Schiller
The only thing normal about database guys is their tables.
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform