我有一席大学和一席城市。 他们之间有小:关系,因此在另一个称为城市大学的表格中拯救了这种关系。 现在我想写一个触发点,在大学大学毕业时删除各大学和大学之间的所有关系。 但它没有工作。 在试图删除大学时,我总是收到以下错误信息:
"Msg 547, Level 16, State 0, Line 2 The DELETE statement conflicted with the REFERENCE constraint "FK_CityUniversities_Universities". The conflict occurred in database "Alumni_Dev", table "dbo.CityUniversities", column UniversityId . The statement has been terminated."
这是我已经执行并成功实施的触发因素(表大学):
USE [Alumni_Dev]
GO
/****** Object: Trigger [dbo].[deleteUni] Script Date: 05/02/2012 11:42:43 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER TRIGGER [dbo].[deleteUni]
ON [dbo].[Universities]
FOR DELETE
AS
DECLARE @UniId int
SELECT @UniId = (SELECT UniversityId FROM deleted)
BEGIN
--Remove constraints
DELETE FROM dbo.CityUniversities WHERE UniversityId = @UniId;
END
我已经把我的解决办法与几个方面进行了比较,但我找不到任何可能错误的东西。 受影响的表格中是否有错误的环境?
我很高兴听到任何建议。
我最后用以下触发因素(从X到Nikola)解决了我的问题:
ALTER TRIGGER [dbo].[deleteUni]
ON [dbo].[Universities]
INSTEAD OF DELETE
AS
DECLARE @UniId int
SELECT @UniId = UniversityId FROM deleted
BEGIN
--Remove constraints
DELETE FROM dbo.CityUniversities WHERE UniversityId = @UniId;
DELETE FROM dbo.Universities WHERE UniversityId = @UniId;
END