logo
  • Jobs
  • About Me
  • Contact
  • Home
« Karvonen Formula
Merry Christmas! »

Drop all stored procedures

Posted December 15th, 2004 by Matt Berther

Ever want to drop all of the stored procedures in a database? I typically use this technique to do database updates (drop them all, and then recreate them) to make sure that I dont have any left over procedures and so I dont have to maintain different scripts for creating and updating my databases.

Try this script…

USE myDatabase
GO
 
declare @procName sysname
 
declare someCursor cursor FOR
    SELECT name FROM sysobjects WHERE type = 'P' AND objectproperty(id, 'IsMSShipped') = 0
 
open someCursor
fetch next FROM someCursor INTO @procName
while @@FETCH_STATUS = 0
begin
    exec('drop proc ' + @procName)
    fetch next FROM someCursor INTO @procName
end
 
close someCursor
deallocate someCursor
go

For the love of God, make sure that you dont run this against your master database. :)

Update: Changed declaration of @procName to sysname as per Raymond’s comment.

6 Comments

This entry was posted on Wednesday, December 15th, 2004 at 4:08 pm and is filed under Uncategorized. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Raymond Lewallen
December 16th, 2004

This is a petty comment, but here it is anyways :)

Object names in Sql Server are stored as Unicode. Specifically, object names are stored using the UDT “sysname”, which is the equivalent of NVARCHAR(128). So no need to declare any variable longer than 128 characters, because the name won’t exist at a length greather than that in sysobjects. Also, varchar() is non-unicode, whereas nvarchar (and sysname for that matter) are unicode data types. I can’t remember off the top of my head where I had a problem with this in the past, but I was doing a convert to varchar from sysname and ended up with unexpected results, so use nvarchar, or better yet sysname because it enforces the 128 character limit, just to be safe. Chances are, it won’t make a hill of beans difference though. I’m just bored and wanted to post something.

Hassan
June 6th, 2007

Thanks a lot…

Abhilash
July 29th, 2009

Nice Post and very useful!

Simon
August 10th, 2009

Fab script – saved me a good deal of trouble. One minor amend – some of my upsized procedures have dashes in the title so I made a minor change to the exec statement within the begin…end loop to add quotes around the SP name:

exec(’drop proc ‘ + ‘”‘ + @procName + ‘”‘)

Thanks again Matt. Still useful, 5 years after it was published :-)

Pedrero
October 20th, 2009

Thank you man

Amr ElGarhy
December 15th, 2009

Helped me too much, nice script, did my job in no time :)

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>
-->

flag
Favorite Charity
wounded warrior project
Search
Social
  • mattberther on twitter
  • mattberther on linkedin
Syndication
Archives
  • January 2010
  • September 2009
  • July 2009
  • June 2009
  • February 2009
  • January 2009
  • December 2008
  • November 2008
  • September 2008
  • August 2008
  • June 2008
  • May 2008
  • April 2008
  • March 2008
  • February 2008
  • January 2008
  • December 2007
  • November 2007
  • October 2007
  • September 2007
  • August 2007
  • July 2007
  • June 2007
  • May 2007
  • April 2007
  • March 2007
  • February 2007
  • January 2007
  • December 2006
  • November 2006
  • October 2006
  • September 2006
  • August 2006
  • July 2006
  • June 2006
  • May 2006
  • April 2006
  • March 2006
  • February 2006
  • January 2006
  • December 2005
  • November 2005
  • October 2005
  • September 2005
  • August 2005
  • July 2005
  • June 2005
  • May 2005
  • April 2005
  • March 2005
  • February 2005
  • January 2005
  • December 2004
  • November 2004
  • October 2004
  • September 2004
  • August 2004
  • July 2004
  • June 2004
  • May 2004
  • April 2004
  • March 2004
  • February 2004
  • January 2004
  • December 2003
  • November 2003
  • October 2003
  • September 2003
  • August 2003
  • July 2003
  • June 2003
  • May 2003
  • April 2003
  • March 2003
mattberther.com © 2003 - 2010