Problem
I noticed that SQL Server now has a CREATE OR ALTER T-SQL statement. Can you explain what this does and how to use?
Solution
The CREATE OR ALTER statement was introduced with SQL Server 2016 and works with versions 2016 and later. This combines both the CREATE and ALTER statements functionality. If the object does not exist it will be created and if the object does exist it will be altered/updated. The CREATE OR ALTER statement works with specific types of database objects such as stored procedures, functions, triggers and views.
You do not need to add extra code to check if the object exists in the SYSOBJECTS system table and then drop and re-create the object. The CREATE OR ALTER statement will do that for you. This allows you to streamline your code and eliminate having to write additional code to check for an objects existence.
This statement acts like a normal CREATE statement by creating the database object if the database object does not exist and works like a normal ALTER statement if the database object already exists.
Assume we tried to create a stored procedure that already exists as follows.
USE MSSQLTipsDemo
GO
CREATE PROC CreateOrAlterDemo
AS
BEGIN
SELECT TOP 10 * FROM [dbo].[CountryInfoNew]
END
GOSQL Server will prevent us from creating the stored procedure since there is a database object with that name already.

We can modify the code to use the CREATE OR ALTER statement instead of just the CREATE statement. The query execution will succeed each time you run that query. It will work as an ALTER statement if the object exists or as a CREATE statement if the object does not exist.
USE MSSQLTipsDemo
GO
CREATE OR ALTER PROC CreateOrAlterDemo
AS
BEGIN
SELECT TOP 10 * FROM [dbo].[CountryInfoNew]
END
GONotes
The CREATE OR ALTER statement does not work for tables and indexes only for stored procedures, functions, triggers and views.
Although this statement is handy to allow you to create a new object or alter an existing object, caution should be used. If for some reason you end up using the same name as an existing object, the alter component will replace the existing code, so be careful how and when you use this.
When using CREATE OR ALTER, SQL Server will check to make sure the object types are compatible, so you don’t accidently replace an existing object with a different object type. So, if we had an existing stored procedure named “dbo.spTest” and tried to use CREATE or ALTER with function code using the same object name “dbo.spTest” we would get the following error:
Msg 2010, Level 16, State 1, Procedure spTest, Line 1 [Batch Start Line 5]
Cannot perform alter on 'dbo.spTest' because it is an incompatible object type.Next Steps
- Check out this tutorial to learn more about SQL Server Stored Procedures
- CREATE PROCEDURE (Transact-SQL)

Ahmad has a Bachelor’s Degree in Computer Engineering from the University of Jordan and five years of experience working as a SQL DBA, gaining valuable knowledge of database structures, practices, principles and theories. His experience also includes.NET development, working with database applications, scripting and creating SQL queries and views. His personal abilities include having very strong communication and interpersonal skills, the ability to prioritize and to make good sound decisions that benefit the company. He has experience in upgrading, configuring, securing, tuning and monitoring SQL Servers since SQL Server 2005. This includes SQL Server performance tuning, SQL Server resource governor management, SQL Server maintenance plans, SQL Server data collection (Reports) analyzing and SQL databases design, developing, indexing and query optimization. In addition, he is familiar with installing and configuring SSRS, SSIS and SSAS. When it comes to disaster recovery and high availability, he has a solid foundation in SQL backup and recovery scenarios, mirroring, replication, log shipping, SQL clustering and AlwaysOn technology.
- MSSQLTips Awards: Author Contender – 2016-2017 | Trendsetter (25+ tips) – 2016 | Rookie Contender – 2015



We’ve just started using this syntax, and it does save some coding. However, the context we most often use it in is making weekly fixes to keep two similar databases in sync. If we get a large script from our business partner, we create rollback scripts by scripting the changing sprocs as “ALTER”, so if something doesn’t work, we can run those scripts to change the database to its previous state.
With SQL Server making the decision on whether to create or alter, it’s not obvious which scripts are going to be altered, so we have to go through each sproc individually to see if it exists, and then script an alter for rollback. Is there any way for SQL to tell us which operation it’s done on an individual sproc?
interesting thing worth noting is that when i run “CREATE OR ALTER” on a stored proc I’m replicating, the proc alterations don’t get picked up by replication. If you just use “ALTER” to update a stored proc then replication picks up the changes and distributes them.