INSERT语句与FOREIGN KEY约束冲突

时间:2016-09-19 20:03:42

标签: sql sql-server insert

使用此FK对另一个表(ApplicationTasks)执行INSERT时出现以下错误:

  

INSERT语句与FOREIGN KEY约束冲突   " FK_ApplicationTasks_Application__ApplicationId&#34 ;.冲突   发生在数据库"状态",表" dbo.Application",列中   '的applicationID'

这种情况一直发生,但我们仍然将记录插入到ApplicationTasks表中。插入物可以每秒多次。我没想到会有任何类型的阻塞\锁定会导致这种情况。它引用的ApplicationId已存在于Application表中。

有什么想法会引导我解决这个问题?

编辑:我无法在我的帖子中添加图片,所以这是我能为查询\数据做的最好的事情:

申请表:

SELECT [ApplicationId]
  ,[ApplicationName]
  FROM [Status].[dbo].[Application]
  WHERE ApplicationId = 498

结果:

ApplicationId   ApplicationName

498             Tst ESB PowerEnterprise.PO.MG

ApplicationTasks表:

SELECT TOP 10 [TaskId]
  ,[Task]
  ,[ApplicationId]
  ,[RunTime]
  FROM [Status].[dbo].[ApplicationTasks]
  WHERE ApplicationId = 498

结果:

TaskId      Task        ApplicationId   RunTime
769893930   Dummy Task  498             2016-04-25 07:45:21.080
769894958   Dummy Task  498             2016-04-25 08:30:10.857
769895327   Dummy Task  498             2016-04-25 08:45:15.517
769895328   Dummy Task  498             2016-04-25 08:45:16.063
769896677   Dummy Task  498             2016-04-25 09:45:13.427
769896678   Dummy Task  498             2016-04-25 09:45:13.473
769897019   Dummy Task  498             2016-04-25 10:00:17.103
769897020   Dummy Task  498             2016-04-25 10:00:17.743
769897356   Dummy Task  498             2016-04-25 10:15:19.423
769897703   Dummy Task  498             2016-04-25 10:30:13.023

正如您所看到的,498的ApplicationId位于Application表中,并且有针对此ApplicationId已插入ApplcationTasks表的记录。但它仍然会抛出这个错误。可能是因为它经常运行吗?

再次编辑:

ApplicationTasks上的FK

ALTER TABLE [dbo].[ApplicationTasks]  WITH CHECK ADD  CONSTRAINT [FK_ApplicationTasks_Application__ApplicationId] FOREIGN KEY([ApplicationId])
REFERENCES [dbo].[Application] ([ApplicationId])
ON DELETE CASCADE
GO

ALTER TABLE [dbo].[ApplicationTasks] CHECK CONSTRAINT [FK_ApplicationTasks_Application__ApplicationId]
GO

代码调用存储过程:

ALTER Procedure [dbo].[sp_applicationtasks_insert](
@applicationkey INT,
  @task VARCHAR(40),
  @userid VARCHAR(40),
  @runtime DATETIME,
  @taskstatus VARCHAR(20),
  @detail VARCHAR(500),
  @applicationstate VARCHAR(20),
  @host VARCHAR(40) 
)
AS

BEGIN

  BEGIN TRANSACTION

INSERT INTO [applicationtasks]
(
  [ApplicationId],
  [Task], 
  [UserId],
  [RunTime], 
  [Status], 
  [Detail],
  [ApplicationState], 
  [Host]
) VALUES (
  @applicationkey,
  @task,
  @userid,
  @runtime,
  @taskstatus,
  @detail,
  @applicationstate,
  @host    
)
  COMMIT TRANSACTION

  RETURN @@Identity
END

定期

失败的示例语句
EXECUTE @RC = [dbo].[sp_applicationtasks_insert] 
   @applicationkey = 498
  ,@task = "Dummy Task" 
  ,@userid = ''
  ,@runtime = getdate()
  ,@taskstatus = ''
  ,@detail = ''
  ,@applicationstate = ''
  ,@host = 'SERVERNAME'

1 个答案:

答案 0 :(得分:0)

您的查询正在尝试在ApplicationTasks表中插入不存在的applicationid。

示例:

假设您的dbo.Application表包含以下数据:

applicationid - description - data
  1       -    test         - 09192016 
  2       -    test2        - 09192016
  3       -    test3        - 09192016

如果您尝试在ApplicationTasks表中插入4个或更多的applicationid,那么您将收到此错误,因为您破坏了关系。