Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a SQL Stored Procedure drop itself and continue execution?

Tags:

sql

Specifically, can I do this?

CREATE PROC AutoDestructiveStoredProcedure
AS
DROP PROC AutoDestructiveStoredProcedure
PRINT 'Still alive.'
GO

Is it a bad practice?

What is the expected behavior? Does it change based on implementation? What would be the difference between executing this in SQL Server, MySQL and Oracle?

like image 507
Nahuel Prieto Avatar asked Oct 18 '25 19:10

Nahuel Prieto


1 Answers

Yes, this will work -- at least in SQL Server 2008 R2. It continues executing until the end of the procedure and after that the procedure is gone.

Is it bad practice? I think so. In my mind, the main reason is that it mixes DDL with DML, imposing unexpected side effects on what is normally a well-understood operation (calling a stored procedure).

Unfortunately, I can't answer your question with respect to how it works on MySQL or Oracle.

like image 160
mwigdahl Avatar answered Oct 20 '25 08:10

mwigdahl



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!