您现在的位置是:网站首页> 编程资料编程资料
SQL判断字段列是否存在的方法_MsSql_
2023-05-26
459人已围观
简介 SQL判断字段列是否存在的方法_MsSql_
增加字段
复制代码 代码如下:
alter table docdsp add dspcode char(200)
删除字段
复制代码 代码如下:
ALTER TABLE table_NAME DROP COLUMN column_NAME
修改字段类型
复制代码 代码如下:
ALTER TABLE table_name ALTER COLUMN column_name new_data_type
改名
sp_rename
更改当前数据库中用户创建对象(如表、列或用户定义数据类型)的名称。
语法
复制代码 代码如下:
sp_rename [ @objname = ] 'object_name' ,
[ @newname = ] 'new_name'
[ , [ @objtype = ] 'object_type' ]
--假设要处理的表名为: tb
--判断要添加列的表中是否有主键
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
您可能感兴趣的文章:
相关内容
- SQL Server高级内容之case语法函数概述及使用_MsSql_
- oracle忘记sys/system/scott用户密码的解决方法_MsSql_
- SQL SERVER 触发器介绍_MsSql_
- mysql与mssql的md5加密语句_MsSql_
- case 嵌套查询与连接查询你需要懂得_MsSql_
- sql server创建复合主键的2种方法_MsSql_
- SQL Server遍历表中记录的2种方法(使用表变量和游标)_MsSql_
- 简化SQL Server备份与还原到云工作原理及操作方法_MsSql_
- 一条select语句引起的瓶颈问题思考_MsSql_
- sql server 入门语句总结_MsSql_
