SQL Server 2016新特性:DROP IF EXISTS
阿新 • • 發佈:2018-02-01
obj 宋體 fff article 特性 這一 ble family base 原文: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
SQL Server 2016新特性:DROP IF EXISTS