关于 Identity列的问题, 请教! 在线等待

xiaodongdehome 2004-10-22 09:31:42
小弟在写存储过程的时候, 想给一个查询的结果集加个序号,
例如结果集的查询语句为
select name from list ,
我就创建了一个临时表,
select identity(int, 1, 1) as id , name
into #tmp
from list
可是后来根据程序要求, 必须加个特殊标志 到临时表#tmp中:
insert into #tmp
value(-999, 'abc')
因为 字段 id是 identity(int, 1, 1) 类型的,
所以再添加 以上特殊记录之前时候,
添加了:
SET IDENTITY_INSERT #tmp ON
虽然我编译的时候没有什么错误,
可是在执行存储过程的时候报错:
仅当使用了列的列表,并且 IDENTITY_INSERT 为 ON 时,才能在表 '#tmp' 中为标识列指定显式值。
另外存储过程的拥有者为dbo,
请教各位大侠,这是为什么, 有什么方法解决!


...全文
102 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiaodongdehome 2004-10-23
  • 打赏
  • 举报
回复
我想了一个变通的方法:
select identity(int, 1, 1) as a, name, 0 as b
into #t
from list

update #t
set b = a

alter table #t
drop column a

也报错, 是不是临时表是不能修改表结构的?

yjdn 2004-10-22
  • 打赏
  • 举报
回复
我觉得set identity_insert #t on是不是不能入在过程中执行,你看一下我的这个语句

create proc test
as
set identity_insert #t on
insert #t (ID,con) --字段一定要填写
values(3,4)
set identity_insert #t off

create table #t(id int identity(1,1),con int)
insert into #t select 1,2
set identity_insert #t on
exec test


--把它放在过程外,是可以执行的
xiaodongdehome 2004-10-22
  • 打赏
  • 举报
回复
回复人: zjcxc(邹建) ( ) 信誉:405 2004-10-22 21:46:00 得分: 0


SET IDENTITY_INSERT #tmp ON
insert into #tmp(id , name )
value(-999, 'abc')
SET IDENTITY_INSERT #tmp OFF


朋友, 我那样也试过了, 还是有同样的错误!
xiaodongdehome 2004-10-22
  • 打赏
  • 举报
回复
楼上的朋友, #tmp是 在该存储过程中创建的, 如你所说, 是有问题的!
zjcxc 元老 2004-10-22
  • 打赏
  • 举报
回复
SET IDENTITY_INSERT #tmp ON
insert into #tmp(id , name )
value(-999, 'abc')
SET IDENTITY_INSERT #tmp OFF
yjdn 2004-10-22
  • 打赏
  • 举报
回复
SET IDENTITY_INSERT #tmp ON
再执行过程

34,590

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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