T-SQL 判断字段是否存在的SQL语句写法
2012-12-06 16:22
405 查看
下文为您介绍的SQL语句可以实现判断字段是否存在,并判断添加列的表中是否有主键,这些SQL语句比较有实用的价值,希望可以让您对SQL语句有更多的认识。
--判断要添加列的表中是否有主键 if exists(select 1 from sysobjects where parent_obj=object_id('tb') and xtype='PK') begin print '表中已经有主键,列只能做为普通列添加' --添加int类型的列,默认值为0 alter table tb add 列名 int default 0 end else begin print '表中无主键,添加主键列' --添加int类型的列,默认值为0 alter table tb add 列名 int primary key default 0 end /**************************************************************************************/ 判断table1中是否存在name字段 if exists(select * from syscolumns where id=object_id('table1') and name='name') begin select * from people; end 判断table1中是否存在name字段且删除字段 if exists(select * from syscolumns where id=object_id('table1') and name='name') begin select * from people; alter table table1 DROP COLUMN name end
相关文章推荐
- T-SQL 判断字段是否存在的SQL语句写法
- 判断字段是否存在的SQL语句写法
- mysql通过sql语句判断某个字段在一张表中是否存在
- mysql通过sql语句判断某个字段在一张表中是否存在
- SQL语句判断数据库、表、字段是否存在
- sql的case语句中判断字段是否为null的写法
- SQL语句判断数据库、表、字段是否存在
- SQL语句判断数据库、表、字段是否存在
- SQL语句判断指定的数据库、表、字段、存储过程是否存在
- SQL语句判断数据库、表、字段是否存在
- SQL语句判断某个表是否存在某个字段
- 数据库端防止并发的SQL语句 插入时就判断是否存在
- 判断数据库中对象(表、视图等)名称是否存在的SQL语句
- 判断表中是否存在记录的SQL语句
- sql判断一个表是否存在 以及表中的字段是否存在
- 执行SQL脚本语句判断是否已经存在
- SQL判断一个字段是否为空值条件语句
- SQL——Sql_Server中如何判断表中某字段是否存在
- 判断表是否存在或表是否有数据的sql语句
- SQL判断字段列是否存在