How To Drop Temporary SP If Exists in Sql Server 2005

w80xi6nr  于 2023-10-15  发布在  SQL Server
关注(0)|答案(3)|浏览(115)

My Question is simple! How to Drop a Temporary Stored Procedure If Exists ? This is because while I create a Temporary SP in a script, it throws error like 'There is already an object named '#sp_name' in the database' while i run the Second time. I dont want to show this message to users. Please help me. Your solution is Highly appereciated!

jjhzyzn0

jjhzyzn01#

The temp procs are dropped in the same way as permanent procs are dropped. Please see the code below:

-- Create test temp. proc
CREATE PROC #tempMyProc as
Begin
 print 'Temp proc'
END
GO
-- Drop the above proc
IF OBJECT_ID('tempdb..#tempMyProc') IS NOT NULL
BEGIN
    DROP PROC #tempMyProc
END
jbose2ul

jbose2ul2#

NOT 2005 but with modern SQL server you can do:

DROP PROCEDURE IF EXISTS  #TestTempSP;
kxeu7u2r

kxeu7u2r3#

IF EXISTS (SELECT * FROM sys.procedures WHERE name = 'baz')
DROP PROCEDURE baz

相关问题