I have a minor, one line change (fixing a typo in a string), to a stored procedure that I would like to deploy to our production SQL Server 2005 server as soon as possible.
The worry I have is what happens if at the exact time run the alter statement to update my stored procedure, it happens that something calls that stored procedure at the same time?
Does it run with the previous copy of the stored procedure, or can it result to some corruption or errors?
Considering the ACID nature of SQL Server, I would expect that it is safe. The chances of it running at the exact same time, especially since the SP is quite small are extremely low, but I just prefer to make sure, and I am also interested in the answer, just for educational purposes.
Arguably, ServerFault would be a better place for this, sorry if it is misposted.
Thank you.
Transact-SQL stored procedures cannot be modified to be CLR stored procedures and vice versa. If the previous procedure definition was created using WITH ENCRYPTION or WITH RECOMPILE, these options are enabled only if they are included in the ALTER PROCEDURE statement.
To recompile the stored procedure, DBA opened the stored procedure in SSMS and recreated it by using the alter statement.
The ALTER PROCEDURE statement allows changes to be made to an existing stored procedure.
I've just tested this in SQL Server 2008 R2
I started with:
CREATE PROCEDURE dbo.Stupid AS WAITFOR DELAY '0:00:10' SELECT TOP 5 * FROM dbo.UniqueId GO
I then did the following SQL Server Query Window 1:
EXEC dbo.Stupid
SQL Server Query Window 2, while query in Query Window 1 was running:
ALTER PROCEDURE dbo.Stupid AS WAITFOR DELAY '0:00:05' SELECT TOP 5 * FROM dbo.UniqueId WHERE ID > 5 GO EXEC dbo.Stupid
SQL Server Query Window 3, while queries in Query Window 1 and Query Window 2 were running:
EXEC dbo.Stupid
Results:
What happens:
When using ALTER for the procedure, a schema modification lock is set. The SP still exists, but clients will have to wait until the ALTER is executed. The same applies for ALTER, it will wait until the SP isn't used by clients.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With