我正在尝试编写一个查询而不是错误:
INSERT语句与FOREIGN KEY约束冲突...`
我可以有自己的错误。类似的东西:
<价值>并不存在于< table_name>
TEST TABLE1: TEST TABLE2
Col1 PK tinyint Col2 PK tinyint
Col2 FK tinyint
我将以下触发器作为测试:
CREATE TRIGGER [dbo].[CHECK_INSERT]
FOR INSERT
ON TABLE1
AS
BEGIN
DECLARE @loop AS TINYINT
DECLARE @col1 AS TINYINT
DECLARE @col2 AS TINYINT
SELECT @loop = MIN (col1) FROM INSERTED
SELECT @col1 = col1 FROM INSERTED WHERE col1 = @loop
SELECT @col2 = col2 FROM INSERTED WHERE col1 = @loop
IF NOT EXISTS (SELECT col2 FROM TABLE2 WHERE col2 = @col2)
AND ERROR_NUMBER() = 547
BEGIN
RAISERROR('%s does not exists on Table2', 16, 1, CAST(@col2 AS VARCHAR(3))
ROLLBACK TRANSACTION
END
END
我该如何处理此错误?谢谢。
答案 0 :(得分:0)
你可以在try / catch中添加insert语句,但我想首先你需要INSTEAD OF INSERT触发器。