原文:
SQL Server 2016新特性:DROP IF EXISTS
在我们写T-SQL要删除某个对象(表、存储过程等)时,一般会习惯先用IF语句判断该对象是否存在,然后DROP,比如:
旧版本:
IF OBJECT_ID('dbo.PERSON','U') IS NOT NULL DROP TABLE PERSON IF EXISTS (SELECT * FROM sys.objects where name = 'PERSON') DROP TABLE PERSON
SQL Server 2016简化了这一操作,通过下面一句简洁的SQL即可实现:
新版本(SQL Server 2016):
DROP TABLE IF EXISTS PERSON
DROP IF EXISTS的语法如下:
DROPobject_type IFEXISTSobject_name
能够用于DROP的object_type,如Tables, Database, Function, Trigger, Stored Procedure, Column, User, Type, View, Schema,皆可套用,比如:
ALTER TABLE PERSON DROP COLUMN If EXISTS NAME