How to remove temporary SP if it exists on Sql Server 2005

My question is simple! How to delete a temporary stored procedure if exists? This is due to the fact that while I create a temporary SP in a script, it throws an error, for example: "There is already an object with the name" #sp_name "in the database, while I run it a second time. I do not want to show this message to users Please help me. Your solution is highly appereciated!

+6
source share
2 answers

Temporary procs are discarded in the same way as permanent procs are discarded. See 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 
+14
source
 IF EXISTS (SELECT * FROM sys.procedures WHERE name = 'baz') DROP PROCEDURE baz 
-1
source

Source: https://habr.com/ru/post/891948/


All Articles