dcsimg
Database Journal
MS SQL Oracle DB2 Access MySQL PostgreSQL Sybase PHP SQL Etc SQL Scripts & Samples Tips Database Forum Rss Feed

» Database Journal Home
» Database Articles
» Database Tutorials
MS SQL
Oracle
DB2
MS Access
MySQL
» RESOURCES
SQL Scripts & Samples
Tips
» Database Forum
» Slideshows
Free Newsletters:



News Via RSS Feed

Rss Feed

Database Journal |DBA Support |SQLCourse |SQLCourse2
 

Featured Database Articles

MS SQL

Posted April 6, 2020

Creating or Altering an Objects with a Single Statement

By Gregory Larsen

Do you get tired of having your CREATE PROCEDURE statement failing if the stored procedure already exists? If you are like me then you probably hate this as much as I do. I get tired of writing a drop then create statement like below:

IF OBJECT_ID('dbo.YourObject','U') IS NOT NULL
DROP PROCEDURE dbo.YourObject;
CREATE PROCEDURE dbo.YourObject AS _

Or modifying my CREATE PROCEDURE statement to be an ALTER PROCEDURE after my initial creation.  Well now there is a better way.

With the introduction of SQL Server 2016 SP1 Microsoft introduced the CREATE OR ALTER statement. Yes, you read that right the "CREATE OR ALTER" statement. This is not two statements; it is just a single statement. If the object doesn't exist it will create it. If the object does exist it will alter it. This statement works on stored procedures, views, functions, and triggers. If you are running SQL Server 2016 SP1 or above, you can now create or alter a stored procedure using a statement similar to below.

CREATE OR ALTER PROCEDURE dbo.YourObject AS _

# # #

» See All Articles by Columnist Gregory A. Larsen



MS SQL Archives




Latest Forum Threads
MS SQL Forum
Topic By Replies Updated
SQL 2005: SSIS: Error using SQL Server credentials poverty 3 August 17th, 07:43 AM
Need help changing table contents nkawtg 1 August 17th, 03:02 AM
SQL Server Memory confifuration bhosalenarayan 2 August 14th, 05:33 AM
SQL Server Primary Key and a Unique Key katty.jonh 2 July 25th, 10:36 AM