Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 726
Saajid Ismail Avatar asked Aug 02 '10 10:08

Saajid Ismail


People also ask

How do I drop a stored procedure in SQL Server?

Using SQL Server Management Studio Expand Databases, expand the database in which the procedure belongs, and then expand Programmability. Expand Stored Procedures, right-click the procedure to remove, and then click Delete.

What Is syntax for dropping procedure?

The syntax to a drop a procedure in MySQL is: DROP procedure [ IF EXISTS ] procedure_name; procedure_name. The name of the procedure that you wish to drop.


2 Answers

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.

like image 146
Brock Adams Avatar answered Oct 01 '22 01:10

Brock Adams


A slightly simpler method without going to system tables:

IF OBJECT_ID('my_procedure') IS NOT NULL DROP PROCEDURE my_procedure GO 
like image 42
Metaphor Avatar answered Oct 01 '22 01:10

Metaphor