October 26, 2010 at 2:54 am
how to drop all user tables with respect to a particular database
October 26, 2010 at 3:56 am
Please take a FULL database backup before dropping the tables.
You may use the below query to delete USER tables from the database.
DECLARE @TABLENAME VARCHAR(256)
DECLARE @QRY VARCHAR(256)
DECLARE TABCURSOR CURSOR FOR
SELECT NAME FROM SYS.TABLES
OPEN TABCURSOR
FETCH NEXT FROM TABCURSOR INTO @TABLENAME
WHILE @@FETCH_STATUS=0
BEGIN
SET @QRY= 'DROP TABLE ' + @TABLENAME
PRINT @QRY
EXEC @QRY
FETCH NEXT FROM TABCURSOR INTO @TABLENAME
END
CLOSE TABCURSOR
DEALLOCATE TABCURSOR
Pradeep Adiga
Blog: sqldbadiaries.com
Twitter: @pradeepadiga
October 26, 2010 at 5:01 am
Adiga's code example should be tweaked just a little bit; if that was run in master(where way to many user tables get accidentally created), you could delete some spt^ tables that the system uses.
the cursor should avoid tables where is_ms_shipped=1
select name from sys.tables where is_ms_shipped=0
Lowell
Viewing 3 posts - 1 through 3 (of 3 total)
You must be logged in to reply to this topic. Login to reply