Рабочий образец
Образцы таблиц:
create table products (productid int primary key)
insert products select 1
insert products select 2
GO
create table ProductSupportArticles (
ProductSupportArticleID int NOT NULL primary key,
ParentArticleID int NULL references ProductSupportArticles(ProductSupportArticleID),
ProductID int NOT NULL references products (productid),
Title varchar(100) NOT NULL,
Content varchar(MAX) NOT NULL
)
GO
Функция поддержки
create function dbo.getProductSupportArticleParent(@ParentArticleID int)
returns int
with returns null on null input
as
begin
return (select ProductID from ProductSupportArticles where ProductSupportArticleID = @ParentArticleID)
end
GO
Ограничение
alter table ProductSupportArticles add check(
ParentArticleID is null or
dbo.getProductSupportArticleParent(ParentArticleID) = ProductID)
GO
Тесты
insert ProductSupportArticles select 1,null,1,3,4
insert ProductSupportArticles select 2,null,1,3,4
insert ProductSupportArticles select 3,null,2,3,4
insert ProductSupportArticles select 4,1,1,3,4
Хорошо, пока следующий разбивает его, потому что 5 - это 1, что относится к продукту 1.
insert ProductSupportArticles select 5,1,2,3,4
EDIT
Алекс указал на допустимый недостаток. Чтобы охватить этот сценарий, вам понадобится триггер UPDATE, который распространит изменения в ProductID записи на все дочерние (и дочерние) записи. Это будет простой триггер, поэтому я не буду приводить здесь код.