What is the syntax to drop a Stored Procedure in SQL Server 2000?
Asked Answered
C

6

43

Simple question, as the title suggests:

What is the syntax to drop a Stored Procedure (SP) in SQL Server 2000, by first checking that the SP exists?

Please provide the full code.

Crisis answered 2/8, 2010 at 10:48 Comment(0)
A
65

Microsoft recommended using the object_id() function, like so:

IF EXISTS (select * from dbo.sysobjects where id = object_id(N'[dbo].[YourProcedure]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE [dbo].[YourProcedure]
GO

.
object_id() helps resolve owner conflicts. If you do
SELECT name FROM sysobjects WHERE name = 'my_procedure' , you may see many different procedures with the same name -- all for different owners.

But, SELECT * FROM sysobjects WHERE id = object_id(N'[my_procedure]') will only show you the one for the current owner/user, if more than one procedure by that name exists.

Still, always specify the object owner (default is dbo). Not only does this avoid nasty side-effects, it's a little faster too.

Alphorn answered 3/8, 2010 at 0:10 Comment(1)
I'm using MS SQL 2012, this syntax worked for me DROP PROCEDURE [dbo].[YourProcedure]Maggiemaggio
E
14

Not for SQL Server 2000, but starting with SQL Server 2016, you can use the IF EXISTS syntax:

DROP PROCEDURE IF EXISTS [sp_ProcName]
Elgar answered 3/12, 2019 at 22:33 Comment(2)
This should be the top answer; OP got his solution a decade ago, this is now the correct one. Most people reading this don't need the clunky SQL Server 2000 solution.Sauerbraten
That's not true, the question specifically asks for 2000, there are plenty of questions answering for newer versions.Fatsoluble
E
10

A slightly simpler method without going to system tables:

IF OBJECT_ID('my_procedure') IS NOT NULL DROP PROCEDURE my_procedure
GO
Elgar answered 18/9, 2013 at 13:31 Comment(0)
G
3

Like this:

IF EXISTS (SELECT name FROM sysobjects WHERE name = 'my_procedure' AND type = 'P')
DROP PROCEDURE my_procedure GO

Hope that helps!

Goshen answered 2/8, 2010 at 10:52 Comment(0)
O
0

You can do the following if you want to remove multiple Procedures. NB: This syntax works on SQL Server 2016 and later

USE [Database_name]
GO

BEGIN 
DROP PROCEDURE IF EXISTS    'my_procedure1',
                            'my_procedure2',
                            'my_procedure3',
                            'my_procedure4',
                            'my_procedure5',
END
    
Outstation answered 24/6, 2020 at 7:51 Comment(0)
P
-2

In SQL SERVER 2008, if you want to drop a stored procedure just write the below command....

DROP PROC Procedure_name
DROP PROC dbo.spInsertUser

Hope it helps..

Poleyn answered 26/3, 2016 at 4:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.