SQL Server 2000如何用SQL语句在在指定位置插入列

来源:互联网 发布:logback 性能优化 编辑:程序博客网 时间:2024/06/04 23:42

使用alter table可以在表中插入列,插入的列都是放在最后,但有的时候需要将列插入到其他列的前面,用企业管理器可以处理,如果用SQL脚本该如何做呢?

首先参考如下代码:
create table test (A char(10),C char(10),D char(10))
insert into test values ('Col A','Col C','Col D')
select * from test

--允许系统标更新
exec sp_configure 'allow updates','1'
go
reconfigure with override
go
--添加D列
alter table test add B int

--更新C,D列顺序
update syscolumns set colid=colid+1 where colid>=2 and id = object_id('test')

--更新B列顺序
update syscolumns set colid=2 where name='B' and id=object_id('test')

--禁用系统标更新
exec sp_configure 'allow updates','0'
go
reconfigure with override
go

select * from test
drop table test

原创粉丝点击