删除字段
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' ]
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' ]
解决方案 »
- 计算员工的离职后前的天数
- 求一清除数据库表中重复记录的sql文
- 在线等!请高手帮忙!一个SQL时间加减的问题!分不多啦!谢谢1
- 紧急求sql: 日期以“YYYY-MM-DD”格式保存在一个字符型字段中。如何取出今天的所有记录?getdate 和 now 我没办法格式化呀!
- 请教删除含储存过程问题
- sql server 2005 用sql语句更新表格 和 直接在“打开表”中更新有什么区别
- 初学者的菜鸟问题,很菜,请各位多多帮忙呀!!!!感激不尽!关于用代码实现与数据库的连接
- 关于用触发器删除关联表的数据;
- 如何禁用和重新使能触发器?
- 求助一个简单的存储过程语句,求大神指点
- 急!如何按日期分组(Group by)? 100分在线等!
- 日志文件的问题,详情请进。
declare @s varchar(8000)
set @s=''
select @s=@s+'
alter table ['+b.name+'] drop constraint ['+d.name+']'
from syscolumns a
join sysobjects b on a.id=b.id
join syscomments c on a.cdefault=c.id
join sysobjects d on c.id=d.id
where b.name='Tabel1'
and a.name in('a','b','c')
exec(@s)--再修改字段类型
alter table table1 alter column a money
alter table table1 alter column b money
alter table table1 alter column c money--为字段加回默认值
set @s='alter table table1 add constraint
[df__table1__a__'+cast(newid() as varchar(36))
+'] default (0) for a'
exec(@s)
set @s='alter table table1 add constraint
[df__table1__b__'+cast(newid() as varchar(36))
+'] default (0) for b'
exec(@s)
set @s='alter table table1 add constraint
[df__table1__c__'+cast(newid() as varchar(36))
+'] default (0) for c'
exec(@s)
--或者这样处理:--原字段改名
exec sp_rename 'table1.a','a_bak'
exec sp_rename 'table1.b','b_bak'
exec sp_rename 'table1.c','c_bak'
go--添加新字段
alter table table1 add a money default 0
alter table table1 add b money default 0
alter table table1 add c money default 0
go--旧列数据复制到新列
update table1 set a=a_bak,b=b_bak,c=c_bak
go--删除旧列字段
alter table table1 drop column a_bak
alter table table1 drop column b_bak
alter table table1 drop column c_bak
alter table table1 drop column a_bak
alter table table1 drop column b_bak
alter table table1 drop column c_bak
这个执行失败,原因是它有默认值,不可以删除,不过这样也可以用了,谢谢。结贴。