IF EXISTS (SELECT NAME FROM SYSOBJECTS WHERE NAME = 'sp_SQLAuthority' AND TYPE = 'P') BEGIN DROP PROCEDURE sp_SQLAuthority END GO CREATE PROCEDURE sp_SQLAuthority (@Params ...) AS BEGIN ... END GO
So, if you are deploying any script to SQL Server and you are not aware of the situation in production, whether a stored proc was already previously deployed or not. Developer need to put a check and drop if it’s already there and then create it.
Oracle has been having CREATE or REPLACE from a long time and there have been feedbacks given to the Microsoft SQL product team to do something about it.
CREATE OR ALTER statement
Create or Replace xxx
Finally, they have heard it and introduced this change in Service Pack 1 of SQL Server 2016. Here are the few objects which would allow such syntax
- Stored procedures
- User-defined functions
Unfortunately, it would not work for Objects that require storage changes like table, index. It is not allowed for CLR procedure also.
Here is the usage
CREATE OR ALTER PROCEDURE usp_SQLAuthority AS BEGIN SELECT 'ONE' END GO
Do you think this is a good enhancement? I would highly encourage to read below blog on a similar feature. SQL Server – 2016 – T-SQL Enhancement “Drop if Exists” clause
Were you aware of this? Do you want me to blog about SP1 enhancement?
Reference: Pinal Dave (https://blog.sqlauthority.com)