违反PRIMARY KEY约束'PK_readytoship'。无法在对象'readytoship'中插入重复键

时间:2017-09-20 13:08:02

标签: sql-server

我们的SQL系统管理员在这个问题上走了出来,所以它被丢弃在我的腿上。我不是很多关于SQL的知识,但我得到了。他将SQL 2012移植到SQL2000服务器,我们得到了一个失败的工作查询部分。我将作业查询粘贴到SSMS中并获得了Violation KEY约束。查询和错误如下。任何帮助表示赞赏。谢谢。

INSERT INTO [dbo].[readytoship]
           ([delivery]
           ,[so]
           ,[netvalue]
           ,[createdate]
           ,[resonorder]
           ,[reason]
           ,[shipto]
           ,[insertdate]
           ,[delta]
           ,[shpt])
(select vl06cst.delivery,
vl06cst.salesorder,
netvalue as netvalue,
 createdate,

case when vl06cst.insertdate<maxdte and vl06cst.insertdate>mindte then 'P'
when vl06cst.insertdate=mindte and vl06cst.insertdate<maxdte then 'O'
else 'C' end as resonord,resoncode,

shiptoparty, vl06cst.insertdate, dbo.ElapsedTime(createdate,getdate()) as delta,shippoint
from vl06cst join vbak on right(vbak.salesorder,9)=vl06cst.salesorder join vl06minmaxdte on vl06cst.delivery=vl06minmaxdte.delivery
where isnumeric(vbak.salesorder)>0
group by vl06cst.delivery,vl06cst.salesorder,createdate, resoncode, shiptoparty,vl06cst.insertdate, netvalue,resonorder,mindte,maxdte,shippoint)
Result:
Msg 2627, Level 14, State 1, Line 1
Violation of PRIMARY KEY constraint 'PK_readytoship'. Cannot insert duplicate key in object 'readytoship'.
The statement has been terminated.

(0 row(s) affected)

1 个答案:

答案 0 :(得分:0)

为了保持数据的完整性,您无法在表中插入重复数据,阅读代码我相信您可以使用它来解决它。

where isnumeric(vbak.salesorder)>0 AND 
NOT EXISTS (SELECT 1 FROM [dbo].[readytoship] A where vl06cst.delivery=A.delivery)

您需要了解您的代码是否正在尝试在表readytoship上插入现有数据,这是最佳解决方案