A '부모'기록은이 [parent_task_id]
TASK ID | CLIENT ID | PARENT TASK ID | TITLE
1 | 7 | NULL | wash the car
(SELECT * FROM TABLE WHERE [parent_task_id] is null
당신의 부모의 모든 레코드를 찾으려면)
A '아이'레코드가 [parent_task_id]를 필요가 필요가 없을 것입니다 [client_id]가 아닙니다 (규정대로 부모가 부모와 동일한 클라이언트를 가지고 있기 때문입니다).
TASK ID | CLIENT ID | PARENT TASK ID | TITLE
2 | NULL | 1 | vacuum carpent
3 | NULL | 1 | wipe dashboard
이렇게하면 자기 참조 외래 키가 모두 필요한 제약 조건입니다. 상위 레코드의 [client_id]를 위해 하위 레코드의 모든 [client_id] 값이 무시되므로 하위 레코드의 [client_id]와 관련된 제약 조건/규칙은 필요하지 않습니다. 예를 들어
, 당신은 [CLIENT_ID] 자식 레코드 무엇인지 알고 싶다면 :
SELECT
c.task_id,
p.client_id,
c.title
FROM
table p --parent
INNER JOIN table c --child
ON p.task_id = c.parent_task_id
UPDATE (그랜드 아이의 클라이언트 ID를 조회하는 방법) 그것은 CLIENT_ID를 보유하고 가장 높은 수준의 부모의 발견 -이 예에서
--Create and populate your table (using a table var in this sample)
DECLARE @table table (task_id int, client_id int, parent_task_id int, title varchar(50))
INSERT INTO @table VALUES (1,7,NULL,'wash the car')
INSERT INTO @table VALUES (2,NULL,1,'vacuum carpet')
INSERT INTO @table VALUES (3,NULL,1,'wipe dashboard')
INSERT INTO @table VALUES (4,NULL,2,'Step 1: plug-in the vacuum')
INSERT INTO @table VALUES (5,NULL,2,'Step 2: turn-on the vacuum')
INSERT INTO @table VALUES (6,NULL,2,'Step 3: use the vacuum')
INSERT INTO @table VALUES (7,NULL,2,'Step 4: turn-off the vacuum')
INSERT INTO @table VALUES (8,NULL,2,'Step 5: empty the vacuum')
INSERT INTO @table VALUES (9,NULL,2,'Step 6: put-away the vacuum')
INSERT INTO @table VALUES (10,NULL,3,'Step 1: spray cleaner on the rag')
INSERT INTO @table VALUES (11,NULL,3,'Step 2: use the rag')
INSERT INTO @table VALUES (12,NULL,3,'Step 3: put-away the cleaner')
INSERT INTO @table VALUES (13,NULL,3,'Step 4: toss the rag in the laundry bin')
--Determine which grandchild you want the client_id for
DECLARE @task_id int
SET @task_id = 8 -- grandchild's ID to use to find client_id
--Create your CTE (this is the recursive part)
;WITH myList (task_id, client_id, parent_task_id, title)
AS
(
SELECT a.task_id, a.client_id, a.parent_task_id, a.title
FROM @table a
WHERE a.task_id = @task_id
UNION ALL
SELECT a.task_id, a.client_id, a.parent_task_id, a.title
FROM @table a
INNER JOIN myList m
ON a.task_id = m.parent_task_id
)
--Query your CTE
SELECT task_id, client_id, title FROM myList WHERE client_id is not null
, 나는 granchild의 TASK_ID ('빈 진공'8)을 사용했다.
각 부모, 부모의 부모 등을 첫 번째 부모의 레코드까지 보려면 WHERE 절을 마지막 단계에서 제거 할 수 있습니다.
테이블을 다시 디자인 할 수 있습니까? 아니면 현재 구조를 유지해야하는 기존 응용 프로그램 /보고가 있습니까? – BateTech
예, 여전히 스키마를 레이아웃하고 있습니다. – user3536970