Google Groups Home
Help | Sign in
Message from discussion DROP all user-defined objects from a database
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Dan Guzman  
View profile  
 More options Oct 5 2002, 1:38 pm
Newsgroups: microsoft.public.sqlserver.programming
From: "Dan Guzman" <danguz...@nospam-earthlink.net>
Date: Sat, 5 Oct 2002 12:32:02 -0500
Local: Sat, Oct 5 2002 1:32 pm
Subject: Re: DROP all user-defined objects from a database
The script below will user drop tables, views, procedures and functions.
If you have schema-bound objects, you'll need to run this multiple times
until you get no errors.

--drop all user objects

DECLARE @DropStatement nvarchar(1000)
DECLARE @LastError int
DECLARE @TablesDropped int

IF DB_NAME() IN ('master', 'msdb', 'model')
BEGIN
 PRINT 'Not for use on system databases'
 GOTO Done
END

--drop views
DECLARE DropStatements
 CURSOR LOCAL FAST_FORWARD FOR
SELECT
 N'DROP ' +
  TABLE_TYPE +
  N' ' +
  QUOTENAME(TABLE_SCHEMA) +
  N'.' +
  QUOTENAME(TABLE_NAME)
FROM INFORMATION_SCHEMA.TABLES
WHERE
 TABLE_TYPE = 'VIEW' AND
 OBJECTPROPERTY(OBJECT_ID(QUOTENAME(TABLE_SCHEMA) +
  N'.' +
  QUOTENAME(TABLE_NAME)), 'IsMSShipped') = 0
OPEN DropStatements
WHILE 1 = 1
BEGIN
 FETCH NEXT FROM DropStatements INTO @DropStatement
 IF @@FETCH_STATUS = -1 BREAK
 RAISERROR (@DropStatement, 0, 1) WITH NOWAIT
 EXECUTE sp_ExecuteSQL @DropStatement
 SET @LastError = @@ERROR
 IF @LastError <> 0 AND @LastError <> 3729
 BEGIN
  CLOSE DropStatements
  DEALLOCATE DropStatements
  GOTO Done
 END
END
CLOSE DropStatements
DEALLOCATE DropStatements

--drop procedures and functions
DECLARE DropStatements
 CURSOR LOCAL FAST_FORWARD FOR
SELECT
 N'DROP ' +
  ROUTINE_TYPE +
  N' ' +
  QUOTENAME(ROUTINE_SCHEMA) +
  N'.' +
  QUOTENAME(ROUTINE_NAME)
FROM INFORMATION_SCHEMA.ROUTINES
WHERE
 OBJECTPROPERTY(OBJECT_ID(QUOTENAME(ROUTINE_SCHEMA) +
  N'.' +
  QUOTENAME(ROUTINE_NAME)), 'IsMSShipped') = 0
OPEN DropStatements
WHILE 1 = 1
BEGIN
 FETCH NEXT FROM DropStatements INTO @DropStatement
 IF @@FETCH_STATUS = -1 BREAK
 RAISERROR (@DropStatement, 0, 1) WITH NOWAIT
 EXECUTE sp_ExecuteSQL @DropStatement
 SET @LastError = @@ERROR
 IF @LastError <> 0
 BEGIN
  CLOSE DropStatements
  DEALLOCATE DropStatements
  GOTO Done
 END
END
CLOSE DropStatements
DEALLOCATE DropStatements

--drop foreign keys
DECLARE DropStatements
 CURSOR LOCAL FAST_FORWARD FOR
SELECT
 N'ALTER TABLE ' +
  QUOTENAME(TABLE_SCHEMA) +
  N'.' +
  QUOTENAME(TABLE_NAME) +
  N' DROP CONSTRAINT ' +
  QUOTENAME(CONSTRAINT_NAME)
FROM
 INFORMATION_SCHEMA.TABLE_CONSTRAINTS
WHERE
 CONSTRAINT_TYPE = 'FOREIGN KEY' AND
 OBJECTPROPERTY(OBJECT_ID(QUOTENAME(TABLE_SCHEMA) +
  N'.' +
  QUOTENAME(TABLE_NAME)), 'IsMSShipped') = 0
OPEN DropStatements
WHILE 1 = 1
BEGIN
 FETCH NEXT FROM DropStatements INTO @DropStatement
 IF @@FETCH_STATUS = -1 BREAK
 RAISERROR (@DropStatement, 0, 1) WITH NOWAIT
 EXECUTE sp_ExecuteSQL @DropStatement
 SET @LastError = @@ERROR
 IF @LastError <> 0
 BEGIN
  CLOSE DropStatements
  DEALLOCATE DropStatements
  GOTO Done
 END
END
CLOSE DropStatements
DEALLOCATE DropStatements

--drop tables
DECLARE DropStatements
 CURSOR LOCAL FAST_FORWARD FOR
SELECT
 N'DROP TABLE ' +
  QUOTENAME(TABLE_SCHEMA) +
  N'.' +
  QUOTENAME(TABLE_NAME)
FROM INFORMATION_SCHEMA.TABLES
WHERE
 TABLE_TYPE = 'BASE TABLE' AND
 OBJECTPROPERTY(OBJECT_ID(QUOTENAME(TABLE_SCHEMA) +
  N'.' +
  QUOTENAME(TABLE_NAME)), 'IsMSShipped') = 0
OPEN DropStatements
WHILE 1 = 1
BEGIN
 FETCH NEXT FROM DropStatements INTO @DropStatement
 IF @@FETCH_STATUS = -1 BREAK
 RAISERROR (@DropStatement, 0, 1) WITH NOWAIT
 EXECUTE sp_ExecuteSQL @DropStatement
 SET @LastError = @@ERROR
 IF @LastError > 0
 BEGIN
  CLOSE DropStatements
  DEALLOCATE DropStatements
  GOTO Done
 END
END
CLOSE DropStatements
DEALLOCATE DropStatements

Done:
GO

--
Hope this helps.

Dan Guzman
SQL Server MVP

-----------------------
SQL FAQ links (courtesy  Neil Pike):

http://www.ntfaq.com/Articles/Index.cfm?DepartmentID=800
http://www.sqlserverfaq.com
http://www.mssqlserver.com/faq
-----------------------

"Work Work Work" <w...@night.com> wrote in message
news:wsun9.10055$lV3.930364@newsread1.prod.itd.earthlink.net...

> I want to write a T-SQL program to identify and drop all user-defined
> objects in a SQL 2000 database (tables, indexes, views, triggers,
roles,
> procedures, functions, etc).

> Does such a system function or procedure already exist, or does
someone have
> one they're willing to share?

> As near as I can determine this query identifies all user-defined
objects:
> SELECT * FROM sysobjects WHERE CATEGORY = 0

> The 'xtype' column in sysobjects indicates the object type, which I
can use
> to iterate through the objects and drop them

> Is there another reliable way to identify and drop all user-defined
objects?

> Thanks


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google