SQL Server Articles, SQL Server Tips, SQL Server Tutorials, SQL Server Tuning, SQL Server DBA, SQL Server Basics, Training, etc - MyTechMantra.com

DROP IF EXISTS SQL Server T-SQL Enhancement in SQL Server 2016

Previous Page.. Begin Tutorial.. Next Page..

DROP IF Exists SQL Server | DROP IF Exists Table | DROP IF Exists Procedure

SQL Server 2016 introduces a new DROP IF EXISTS SQL Server statement to DROP objects such as tables, columns, indexes, stored procedures, schemas, triggers and user-defined functions.

DROP IF EXISTS statement can be used to check whether the object exists before it is dropped or removed from the database.

This is Part 3 of 10 Part T-SQL Enhancements in SQL Server 2016 for Developers and DBAs. Click here to read it from the beginning….

There are times when before executing the TSQL code, we need to check if the stored procedure exists on SQL Server or if the table exists in SQL Server and if so, first, we need to drop them before executing the code. This article introduced the DROP IF EXISTS Statement, making it easier for you to DROP TABLE or DROP PROCEDURE if it exists on the SQL Server Database.

Drop If Exists SQL Server Clause

Prior to SQL Server 2016 you would write the T-SQL script similar to one below to check whether the object existed before it could be dropped from the database.



Use <DATABASENAME>
GO

IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID(N'[SchemaName].[TableName]') 
AND [type] IN (N'U')) 

DROP TABLE [SchemaName].[TableName]
GO

TSQL Query To Drop Table if Exists SQL Server | TSQL Query To Drop Procedure if Exists SQL Server

Example DROP IF EXISTS in SQL Server 2016

Starting SQL Server 2016, you could achieve the same functionality by executing the below simplified T-SQL script.

Use <DATABASENAME>
GO

DROP TABLE IF EXISTS [SchemaName].[TableName]
GO

DROP PROCEDURE IF EXISTS [SchemaName].[ProcedureName]
GO

Clicking Next Page button to continue reading the topics and click on the Previous Page button to revisit the previous topic.




Previous Page.. Begin Tutorial.. Next Page..

Ashish Mehta

Ashish Kumar Mehta is a database manager, trainer and technical author. He has more than a decade of IT experience in database administration, performance tuning, database development and technical training on Microsoft SQL Server from SQL Server 2000 to SQL Server 2014. Ashish has authored more than 325 technical articles on SQL Server across leading SQL Server technology portals. Over the last few years, he has also developed and delivered many successful projects in database infrastructure; data warehouse and business intelligence; database migration; and upgrade projects for companies such as Hewlett-Packard, Microsoft, Cognizant and Centrica PLC, UK. He holds an engineering degree in computer science and industry standard certifications from Microsoft including MCITP Database Administrator 2005/2008, MCDBA SQL Server 2000 and MCTS .NET Framework 2.0 Web Applications.

Add comment

Newsletter Signup! Join 15,000+ Professionals




Be Social! Like & Follow Us

Follow us

Don't be shy, get in touch. We love meeting interesting people and making new friends.

Advertisement