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
 General SQL Server Forums
 Script Library
 Drop All Database Objects

Author  Topic 

JCamburn
Starting Member

31 Posts

Posted - 2006-02-03 : 15:33:45
Does anyone happen to have a script that will drop all database objects?

I'm looking for something generic that will work with any SQL Server 2000 database. I can write one myself, but I thought I would check here first.

Thanks in advance

John

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2006-02-03 : 15:53:11
I have one to drop stored procedures, udfs, views, and triggers. It only does the drop for very specific prefixes in the objects names though. Here it is:


SET NOCOUNT ON

DECLARE @objName varchar(80)
DECLARE @objType char(2)
DECLARE cur CURSOR FOR
SELECT name, type
FROM SYSOBJECTS
WHERE (
(type = 'P' AND (name LIKE 'usp[_]%' OR name like 'isp[_]%'))
OR
(type = 'FN' AND name LIKE 'udf[_]%')
OR
(type = 'TF' AND name LIKE 'udf[_]%')
OR
(type = 'U')
OR
(type = 'V' AND name LIKE 'v[_]%')
OR
(type = 'TR')
)
AND
uid = 1
AND
status > -1

OPEN cur
FETCH NEXT FROM cur
INTO @objName, @objType

WHILE @@FETCH_STATUS = 0
BEGIN
IF @objType IN ('TF', 'FN')
EXEC ('DROP FUNCTION dbo.' + @objName)

IF @objType = 'P'
EXEC ('DROP PROC dbo.' + @objName)

IF @objType = 'V'
EXEC ('DROP VIEW dbo.' + @objName)

IF @objType = 'TR'
EXEC ('DROP TRIGGER dbo.' + @objName)

FETCH NEXT FROM cur
INTO @objName, @objType
END

CLOSE cur
DEALLOCATE cur

GO



Perhaps you can modify it to your liking.

Tara Kizer
aka tduggan
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2006-02-03 : 15:53:13
USE Master
GO

DROP DATABASE <dbname>
GO

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

Add yourself!
http://www.frappr.com/sqlteam
Go to Top of Page
   

- Advertisement -