判断表和列是否存在

来源:互联网 发布:无代码编程 编辑:程序博客网 时间:2024/06/07 02:26
判断表和列是否存在

   C = CHECK 约束 
   D = 默认值或 DEFAULT 约束 
   F = FOREIGN KEY 约束 
   L = 日志 
   FN = 标量函数 
   IF = 内嵌表函数 
   P = 存储过程 
   PK = PRIMARY KEY 约束(类型是 K) 
   RF = 复制筛选存储过程 
   S = 系统表 
   TF = 表函数 
   TR = 触发器 
   U = 用户表 
   UQ = UNIQUE 约束(类型是 K) 
   V = 视图 
   X = 扩展存储过程 

临时表

if object_id('tempdb..#tempTable') is not null   

--判断某个表或对象是否存在
if (not exists (select 1 from sys.objects where object_id = object_id('student') and xtype='u'))
if (not exists (select 1 from sysobjects where name = 'classes' and xtype='u'))
if (object_id('student', 'U') is null)
 
--判断该列名是否存在,如果不存在就添加
if (not exists (select 1 from information_schema.columns where table_name = 'student' and column_name = 'tel'))
if (not exists (select 1 from sys.columns where object_id = object_id('ICSubContract') and name = 'FIsSecond'))
alter table ICSubContract add FIsSecond int NOT NULL DEFAULT ((0))
0 0
原创粉丝点击