Geoffrey Emery
Tech Goodness

Drop All Tables, Views, and Stored Procedures in a database Via Tsql

January 13, 2009 17:02 by gemery

Don’t ask why  I had to do this, but I think some other people might find this helpfull expsecially those who have hosted sql server.

Drop All Tables Views and Stored Procedures in a database SQL

   1: --delete our tables first
   2: declare @table_count int;
   3: declare @iter int;
   4: -- we know we're going to have tables the first time through
   5: set @table_count = 1 ;
   6: set @iter = 0;
   7: -- check to see how many tables we have in the database
   8: while @table_count > 0
   9: begin
  10: exec sp_MSforeachtable "DROP TABLE ? PRINT '? dropped' ";
  11: SET @table_count = (select COUNT(*) from sysobjects where type = 'U'and name <> 'dtproperties');
  12: SET @iter = @iter + 1;
  13: end
  14: print 'iterations: ' + CAST(@iter as varchar);
  15:  
  16: -- variable to object name
  17: declare @name varchar(100)
  18: -- variable to hold object type
  19: declare @xtype char(1)
  20: -- variable to hold sql string
  21: declare @sqlstring nvarchar(1000)
  22:  
  23: declare SPViews_cursor cursor for
  24: SELECT sysobjects.name, sysobjects.xtype
  25: FROM sysobjects
  26: join sysusers on sysobjects.uid = sysusers.uid
  27: where OBJECTPROPERTY(sysobjects.id, N'IsProcedure') = 1
  28: or OBJECTPROPERTY(sysobjects.id, N'IsView') = 1 and sysusers.name =
  29: 'USERNAME'
  30:  
  31: open SPViews_cursor
  32:  
  33: fetch next from SPViews_cursor into @name, @xtype
  34:  
  35: while @@fetch_status = 0
  36: begin
  37: -- test object type if it is a stored procedure
  38: if @xtype = 'P'
  39: begin
  40: set @sqlstring = 'drop procedure ' + @name
  41: exec sp_executesql @sqlstring
  42: set @sqlstring = ' '
  43: end
  44: -- test object type if it is a view
  45: if @xtype = 'V'
  46: begin
  47: set @sqlstring = 'drop view ' + @name
  48: exec sp_executesql @sqlstring
  49: set @sqlstring = ' '
  50: end
  51:  
  52: -- get next record
  53: fetch next from SPViews_cursor into @name, @xtype
  54: end
  55:  
  56: close SPViews_cursor
  57: deallocate SPViews_cursor

Be Careful She’s a dozy


Tags:
Categories: T-SQL
Actions: E-mail | Permalink | Comments (5) | Comment RSSRSS comment feed

Related posts

Comments

October 31. 2009 23:55

online personal loans

Thank you for your help!

online personal loans

November 9. 2009 12:38

pay day loans

I guess there's always an easier way ...

pay day loans

November 10. 2009 03:02

cash loans

Like your writing! Still you can do some things to improve it.

cash loans

November 10. 2009 08:44

extra money

I am not much of a guy who thinks in so deeply about web design but I think your post had some valid points in it. Like designers are forced to design stuff within the limited code available and not go beyond it, their innovation is somewhat limited but still I think Web Design won't die! I agree that Amazon and other some big sites won't have a blog but now a days it's very important to have some sort of option available so people can quickly communicate their thoughts. I think Amazon if wants to shift it to that, they can get a customized CMS for themselves.

extra money

November 10. 2009 11:22

make perpetual motion machine

I was very pleased to find this site.I wanted to thank you for this great read!! I definitely enjoying every little bit of it and I have you bookmarked to check out new stuff you post.

make perpetual motion machine

Comments are closed