日期:2014-05-17  浏览次数:20548 次

.net中存储过程问题
USE [Resthome]
GO
/****** 对象:  StoredProcedure drop proc [dbo].[usp_leaveDel]    脚本日期: 01/26/2013 11:11:23 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[usp_leaveDel]
@oldid int 
as 
if exists (select * from sysobjects where name='LeaveInfoHistory') ---老人出院导入到历史表中
begin
insert into LeaveInfoHistory select * from LeaveInfo where oldid=@oldid
end
else 
select * into LeaveInfoHistory from LeaveInfo where oldid=@oldid
delete from LeaveInfo where oldid=@oldid
select '删除成功!'
exec usp_leaveDel 33
为什么总是出"消息 8101,级别 16,状态 1,过程 usp_leaveDel,第 6 行
仅当使用了列列表并且 IDENTITY_INSERT 为 ON 时,才能为表'LeaveInfoHistory'中的标识列指定显式值。"错呢,第一次往LeaveInfoHistory里插数据会成功,第二次以后就会出错。
急求

------解决方案--------------------
你向一个定义为“自增”的字段里插入值了,而没有让其自增。
------解决方案--------------------
养成一个好习惯,当你写 insert into 的时候,要给出目标字段列表来,不要空着不写。 
------解决方案--------------------
insert into LeaveInfoHistory select * from LeaveInfo where oldid=@oldid
这一句给自增列赋值了吧?
试试这样写
insert into LeaveInfoHistory(col1,col2,……) select col1,col2,…… from LeaveInfo where oldid=@oldid