这是我的 SQL 语句的一部分:
MERGE INTO Scyk.dbo.Threads AS d
USING (SELECT Id,Question,QuestionTitle, Answer, Category FROM Homework.dbo.Zadanes) AS s
ON 1 = 2
WHEN NOT MATCHED THEN
INSERT (CurrentValue, NumberOfPosts, TotalValue, Views, Title, DateCreated, LastPostAuthor, LastPostDateTime, IsDeleted, Restricted, RestrictWrite, Sticked, CategoryName, AuthorUserName)
VALUES (0, 2, 0, 0, s.QuestionTitle, CURRENT_TIMESTAMP, 'zadania', DATEADD(second, 1, CURRENT_TIMESTAMP), 0, 0, 0, 0, s.Category, 'zadania')
这是我得到的错误:
MERGE 语句与 FOREIGN KEY 约束“FK_dbo.Threads_dbo.Categories_CategoryName”冲突。冲突发生在数据库“Scyk”、表“dbo.Categories”、“名称”列中。
所以我做的第一件事就是检查外键完整性:
SELECT Distinct(Category) FROM [Homework].[dbo].[Zadanes]
where not exists
(select distinct(Name) from Scyk.dbo.Categories)
但这什么也没返回,这意味着外键是好的。那么这里发生了什么?