sql 添加列到固定位置

dengchenlu 2010-04-21 05:06:35
例如现在的SQL表中字段: UserId UserName Score
我想添加一个 Age列到他们中间,结果要变成:UserId UserName Age Score

在Mysql的话可以
alter table tablename add Age int before Score
或者
alter table tablename add Age int after UserName

但在SQL里有没有类似的方法呢?网上的说可以写个存储过程添加到某个位置
但我这不一定添加到哪一列 ,我可能添加到UserName之前,UserName之后
列也不一定多少个,所以我想根据字段来判断,不要根据插入到第几列来判断
也别说重建表,我表里20W数据以上
...全文
704 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhangsong841 2010-04-23
  • 打赏
  • 举报
回复
加一列在最后面,
再按需要的顺序建个视图
dengchenlu 2010-04-22
  • 打赏
  • 举报
回复
貌似大家都没什么好办法么
既然MYSQL可以做到,怎么微软就不弄下呢
这样的操作好多时候会用到啊
dawugui 2010-04-21
  • 打赏
  • 举报
回复
[Quote=引用楼主 dengchenlu 的回复:]
例如现在的SQL表中字段: UserId UserName Score
我想添加一个 Age列到他们中间,结果要变成:UserId UserName Age Score

在Mysql的话可以
alter table tablename add Age int before Score
或者
alter table tablename add Age int after UserNam……
[/Quote]

select UserId ,UserName ,Age ,Score into newtb from tb
drop table tb
select UserId ,UserName ,Age ,Score into tb from newtb
drop table newtb


或者参考如下:
create table t1(C int,A int,B int)
sp_configure 'allow updates',1

RECONFIGURE WITH OVERRIDE

update syscolumns

set colid=(select count() from syscolumns a where a.name=syscolumns.name and a.id=syscolumns.id)

where id=object_id('t1')

sp_configure 'allow updates',0

RECONFIGURE WITH OVERRIDE

select from t1


zhengduan964532 2010-04-21
  • 打赏
  • 举报
回复

查询的时候可以缓过来,或者把老数据插入到临时表,然后重新建表。
htl258_Tony 2010-04-21
  • 打赏
  • 举报
回复
ws_hgo 2010-04-21
  • 打赏
  • 举报
回复
查询顺序即可
htl258_Tony 2010-04-21
  • 打赏
  • 举报
回复
位置没关系,在SELECT列表中调整就可以了。
--小F-- 2010-04-21
  • 打赏
  • 举报
回复
没什么好办法.
王向飞 2010-04-21
  • 打赏
  • 举报
回复
只能重建表
bancxc 2010-04-21
  • 打赏
  • 举报
回复
只能这样了 你说了想根据字段来判断 不想根据顺序判断
SQL77 2010-04-21
  • 打赏
  • 举报
回复
你查询的时候再写一下就OK了,
bancxc 2010-04-21
  • 打赏
  • 举报
回复
位置没关系了

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧