T-SQL在SQL Server中判断表、字段、索引、视图、触发器、Synonym等是否存在

SQL Server创建或者删除表、字段、索引、视图、触发器前判断是否存在。

目录

[1. SQL Server创建表之前判断表是否存在](#1. SQL Server创建表之前判断表是否存在)

[2. SQL Server新增字段之前判断是否存在](#2. SQL Server新增字段之前判断是否存在)

[3. SQL Server删除字段之前判断是否存在](#3. SQL Server删除字段之前判断是否存在)

[4. SQL Server新增索引之前判断是否存在](#4. SQL Server新增索引之前判断是否存在)

[5. SQL Server判断视图是否存在并创建](#5. SQL Server判断视图是否存在并创建)

[6. SQL Server判断触发器是否存在并创建](#6. SQL Server判断触发器是否存在并创建)

[7. 创建SYNONYM 之前判断是否存在](#7. 创建SYNONYM 之前判断是否存在)


1. SQL Server创建表之前判断表是否存在

sql 复制代码
IF OBJECT_ID('[dbo].[Documents]', 'U') IS  NULL  
begin
    -- This table used to save recent 2 years documents
    CREATE TABLE [dbo].[Documents] (
        [Id]                        INT             NOT NULL,
        [DocumentGroupId]           INT             NOT NULL,
        [FileName]                  VARCHAR (200)   NULL,
        [FilePath]                  VARCHAR (250)   NULL,
        [CreateDate]                DATETIME        NOT NULL,
        [ModifyDate]                DATETIME        NOT NULL,
        [DocumentStatus]            VARCHAR (2)     NULL
    )


END
GO

2. SQL Server新增字段之前判断是否存在

sql 复制代码
--[dbo].[AgencyDocuments] table name
--EffectiveDateTest column name. 此处不可以用[EffectiveDateTest]
if COL_LENGTH('[dbo].[AgencyDocuments]','EffectiveDateTest') is null
begin
	alter table [dbo].[AgencyDocuments] add [EffectiveDateTest] DATETIME NULL
end
go

3. SQL Server删除字段之前判断是否存在

sql 复制代码
--[dbo].[AgencyDocuments] table name
--EffectiveDateTest column name. 此处不可以用[EffectiveDateTest]
if COL_LENGTH('[dbo].[AgencyDocuments]','EffectiveDateTest') is NOT null
begin
	alter table [dbo].[AgencyDocuments] drop column [EffectiveDateTest] 
end
go

4. SQL Server新增索引之前判断是否存在

sql 复制代码
IF NOT EXISTS (SELECT top 1 1 FROM sys.indexes  WHERE name='IX_AgencyDocument_Id' AND object_id = OBJECT_ID('[dbo].[AgencyDocuments]'))
BEGIN    
    CREATE NONCLUSTERED INDEX IX_AgencyDocument_Id ON [dbo].[AgencyDocuments] ([Id])
	    INCLUDE ([DocumentGroupId],[FileName],[FilePath],[CreateDate],[DocumentStatus])
END
GO

5. SQL Server判断视图是否存在并创建

sql 复制代码
-- View存在则删除
IF OBJECT_ID('vw_AgencyDocuments') IS NOT NULL
begin
	drop view  vw_AgencyDocuments
end
GO
--创建View
create view vw_AgencyDocuments as 
select Id from [dbo].[AgencyDocuments]
go

6. SQL Server判断触发器是否存在并创建

sql 复制代码
-- 判断是否存在,存在则删除
IF OBJECT_ID ('[dbo].[TRIGGER_INSERT_AGENCYDOCUMENTS]', 'TR') IS NOT NULL 
begin
	drop trigger [dbo].[TRIGGER_INSERT_AGENCYDOCUMENTS]  
end
GO

-- 创新新的触发器
CREATE TRIGGER [dbo].[TRIGGER_UPDATE_AGENCYDOCUMENTS] ON [dbo].[Document] AFTER UPDATE
AS 
BEGIN
    SET NOCOUNT ON;

    UPDATE m
        SET [ModifyDate] = n.[ModifyDate]  
        ,[Status] = n.[Status]      
        ,[DocumentStatus] = n.[DocumentStatus] 
    FROM [dbo].[AgencyDocuments] m JOIN INSERTED n on m.Id=n.Id 

    SET NOCOUNT OFF;
END

GO

7. 创建SYNONYM 之前判断是否存在

sql 复制代码
if not exists (select * from sys.synonyms where object_id=OBJECT_ID('[dbo].[AgencyDocuments]'))
begin
    CREATE SYNONYM [dbo].[AgencyDocuments] FOR [ABC_REPL].[dbo].[AgencyDocuments]
end
go
相关推荐
q***448119 小时前
postgresql链接详解
数据库·postgresql
菜鸟‍19 小时前
【后端学习】MySQL数据库
数据库·后端·学习·mysql
污斑兔19 小时前
腾讯云 CloudBase 数据库 CRUD 完整指南
数据库·云计算·腾讯云
tuokuac19 小时前
批量新增操作为什么要加@Transactional注解
数据库
q***9941 天前
Redis的Spring配置
数据库·redis·spring
S***y3961 天前
MySQL视频
数据库·mysql
周杰伦fans1 天前
[特殊字符] 代理模式超详细讲解 ——.NET
数据库·c#·代理模式
TDengine (老段)1 天前
TDengine 转换函数 TO_JSON 用户手册
android·大数据·数据库·json·时序数据库·tdengine·涛思数据
2301_800256111 天前
第七章 空间存储与索引 知识点梳理3(空间填充曲线)
数据库·笔记·sql·postgresql
冰封剑心1 天前
MiniCPM-V-2_6 (4-bit 量化)使用
java·前端·数据库