这是你在做insert into OPDrecipeMain() 的时候,这个表OPDrecipeMain已经存在主键=9149108你可以测试: select * from OPDrecipeMain where nRecipeMainID=9149108 如果有数据,说明主键等于9149108已经存在
解决这个问题是有办法的:begin tran declare @nRecipeMainID int select @nRecipeMainID=MAX(nRecipeMainID)+1 from OPDrecipeMain insert into OPDrecipeMain(nRecipeMainID.....) values(@nRecipeMainID.....)commit tran因为有可能存在多个用户同时使用这个表,所以得用事务处理
--#1.你的主键pk_opdrecipemain(nRecipeMainID)值9149108已经存在,不能重复插入。主键约束为创建一个唯一索引。 --#2.解决方案如下: --#1.加事务,加表锁,不过这种方法不知能不能达到楼主的效率要求 BEGIN TRAN --加事务 DECLARE @nRecipeMainID INT SELECT @nRecipeMainID=MAX(nRecipeMainID)+1 FROM OPDrecipeMain WITH(TABLOCKX) --加表锁 INSERT INTO OPDrecipeMain(nRecipeMainID) VALUES(@nRecipeMainID) COMMIT TRAN--#2.加一个自增列TranscationID,作为nRecipeMainID的值,但可能会出现断号 ALTER TABLE tb ADD TranscationID BIGINT NOT NULL IDENTITY(1,1) go CREATE INDEX IX_tb_TranscationID ON dbo.tb ( TranscationID ) GODECLARE @nRecipeMainID INTINSERT INTO OPDrecipeMain(nRecipeMainID) VALUES(@nRecipeMainID) SET @nRecipeMainID = SCOPE_IDENTITY()UPDATE dbo.tb SET nRecipeMainID = @nRecipeMainID WHERE TranscationID = @nRecipeMainID
的时候,这个表OPDrecipeMain已经存在主键=9149108你可以测试:
select * from OPDrecipeMain where nRecipeMainID=9149108
如果有数据,说明主键等于9149108已经存在
declare @nRecipeMainID int
select @nRecipeMainID=MAX(nRecipeMainID)+1 from OPDrecipeMain
insert into OPDrecipeMain(nRecipeMainID.....)
values(@nRecipeMainID.....)commit tran因为有可能存在多个用户同时使用这个表,所以得用事务处理
--#2.解决方案如下:
--#1.加事务,加表锁,不过这种方法不知能不能达到楼主的效率要求
BEGIN TRAN --加事务
DECLARE @nRecipeMainID INT
SELECT @nRecipeMainID=MAX(nRecipeMainID)+1 FROM OPDrecipeMain WITH(TABLOCKX) --加表锁
INSERT INTO OPDrecipeMain(nRecipeMainID)
VALUES(@nRecipeMainID)
COMMIT TRAN--#2.加一个自增列TranscationID,作为nRecipeMainID的值,但可能会出现断号
ALTER TABLE tb
ADD TranscationID BIGINT NOT NULL IDENTITY(1,1)
go
CREATE INDEX IX_tb_TranscationID ON dbo.tb
(
TranscationID
)
GODECLARE @nRecipeMainID INTINSERT INTO OPDrecipeMain(nRecipeMainID)
VALUES(@nRecipeMainID)
SET @nRecipeMainID = SCOPE_IDENTITY()UPDATE dbo.tb
SET nRecipeMainID = @nRecipeMainID
WHERE TranscationID = @nRecipeMainID
目标表可能还有历史数据,可以把目标表改成别的名字(如:OPDrecipeMain_111),然后新建一个与原名一样(即:OPDrecipeMain),表结构也一样的表,重新采集数据。nRecipeMainID这个主键是采集过来的,不能改。所以也不能定义变量,也不能改程序,所以最好办法先把原表备份,再新建一个与原表一样的结构,再采集数据