Tech Insight !

Technical blog on ASP.Net, PHP, Web Development, Web hosting , Database Programming
Home » SQL server 2005 » How to make SQL Server 2005 database empty using cursor & sys.objects (Delete All Tables, stored procedures, views & UDF’s)

How to make SQL Server 2005 database empty using cursor & sys.objects (Delete All Tables, stored procedures, views & UDF’s)

February 24th, 2010 Posted in SQL server 2005 Tags: , ,

Understanding sys.objects:

Sys.objects is a system VIEW in SQL Server 2005, for each SQL database there is a separate sys.object view which gets stored within databse itself.

Using Sys.objects returns list of all database objects and its types, type can be either of given below:

DB OBJECT TYPES

F     FOREIGN_KEY_CONSTRAINT

IT    INTERNAL_TABLE

PK    PRIMARY_KEY_CONSTRAINT

S     SYSTEM_TABLE

SQ    SERVICE_QUEUE

U     USER_TABLE

V     VIEW

How to DELETE all User Tables , stored procedures , UDF’s and Views using cursor

Use [database name]

declare @q as
nvarchar(max)

declare @name nvarchar(max);

declare @type nvarchar(max);

declare cur cursor
for select name ,type from sys.objects where type in(‘p’,‘fn’,‘v’,‘u’);

open cur;

fetch next from cur into @name,@type

while @@fetch_status = 0

begin

if(@type=‘p’)

begin

set @q=N‘drop procedure ‘ + @name;

end

if(@type=‘fn’)

begin

set @q=N‘drop function ‘ + @name;

end

if(@type=‘v’)

begin

set @q=N‘drop view ‘ + @name;

end

if(@type=‘u’)

begin

set @q=N‘drop table ‘ + @name;

end

exec sp_executesql @q;

fetch next from cur into @name,@type

end

close cur;

deallocate cur;

NOTE :

If you are in need of any Web Development feel free to Inquire us . Dhanashree Inc. Expertise in Asp.net Development, Php Development, Website designing, Open Source customisation. Dhanashree Inc can be our offshore development company / outsourcing web development company, hire dedicated web programmers.

Above information is for knowledge sharing if you have problem / issue / suggestion please intimate us with details for proper and prompt action.

One Response to “How to make SQL Server 2005 database empty using cursor & sys.objects (Delete All Tables, stored procedures, views & UDF’s)”


  1. MedicamentSpot.com. Canadian Health&Care.Best quality drugs.No prescription online pharmacy.Special Internet Prices. Low price drugs. Order drugs online

    Buy:Female Cialis.Aricept.Wellbutrin SR.Benicar.SleepWell.Acomplia.Female Pink Viagra.Nymphomax.Zetia.Amoxicillin.Zocor.Lipothin.Ventolin.Lipitor.Buspar.Cozaar.Prozac.Seroquel.Lasix.Advair….

Leave a Reply