请邹建大哥进来指点下你的帖子里有关identity的问题

FashionT 2004-12-20 07:35:06
zjcxc(邹建)大哥原帖:
要将已有的字段改为identity是不行了,如果你怕数据丢失,就用临时表来解决:

begin tran
--备份你的原来数据
select * into #temp from 你的表

--重建要设置为identity的字段
alter table 你的表 drop id
alter table 你的表 add id int identity(1,1)

--恢复数据
set identity_insert 你的表 on
insert into 你的表select * from #temp
set identity_insert 你的表 off

--删除临时表
drop table #temp

我的代码:
select * into #temp from identable
alter table identable drop column key_col
exec addcolumn 'identable','key_col','int identity',1 ---在key_col原来的位置上插入
delete from identable
set identity_insert identable on
insert into identable select * from #temp
set identity_insert identable off
drop table #temp

错误提示:
Server: Msg 8101, Level 16, State 1, Line 6
An explicit value for the identity column in table 'identable' can only be specified when a column list is used and IDENTITY_INSERT is ON.

为什么会提示说identity_insert没有打开?我是想把原来表identable中key_col字段改成自增字段,且原表中key_col有数据存在.zjcxc(邹建)大哥帮帮忙指点指点,应该怎么修改
...全文
109 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
FashionT 2004-12-20
  • 打赏
  • 举报
回复
真是惭愧啊,错误信息都看不懂,问题解决,谢谢NinGoo(宁哥),结帖
xiaoxiangqing 2004-12-20
  • 打赏
  • 举报
回复
select * into #temp from identable
alter table identable drop column key_col
exec addcolumn 'identable','key_col','int identity',1 ---在key_col原来的位置上插入
delete from identable
set identity_insert identable on
insert into identable(col1,col2,col3...) select col1,col2,col3... from #temp
set identity_insert identable off
drop table #temp
NinGoo 2004-12-20
  • 打赏
  • 举报
回复
An explicit value for the identity column in table 'identable' can only be specified when a column list is used and IDENTITY_INSERT is ON.

这个错误的意思是说,要在identity的列上显式的插入值,需要两个条件
1.明确的写出要插入的列名
2.set identity_insert table_name on

显然,你违反了第一条


这样改写即可
set identity_insert identable on
insert into identable(column1,column2,...) select column1,column2,... from #temp
set identity_insert identable off

27,579

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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