码迷,mamicode.com
首页 > 数据库 > 详细

sql 判断表是否存在,判断列是否存在

时间:2015-01-07 12:50:10      阅读:175      评论:0      收藏:0      [点我收藏+]

标签:

判断表是否存在:

语法:

SELECT  * FROM dbo.SysObjects where id = object_id(N‘表名‘)  

 例子:

SELECT  * FROM dbo.SysObjects where id = object_id(N‘ExchangeLog‘)

 

if exists(SELECT * FROM dbo.SysObjects where id = object_id(N‘ExchangeLog‘))
  print ‘表存在‘
else
  print ‘表不存在‘

 判断列是否存在:

语法:

select * from syscolumns where id=object_id(N‘表名‘) and name =‘列名‘

 例子

select * from syscolumns where id=object_id(N‘ExchangeLog‘) and name =‘Extype‘

 

if exists(select * from syscolumns where id=object_id(N‘ExchangeLog‘) and name =‘Extype‘)
	alter table ExchangeLog drop column ExType
else
begin
	alter table [ExchangeLog] add ExType nchar(15)
end
go

 

sql 判断表是否存在,判断列是否存在

标签:

原文地址:http://www.cnblogs.com/sjns/p/4207991.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!