Try the following, maybe this will get you in the right direction.
This will get you all PIDs in table 1, which have all of its' CIDs located in table 2
From what gathered from the question, the PIDs in table 1 do not necessarily have to have all CIDs that are in table 2, it just has to have CIDs that are located in table 2, and if one of its' CIDs are not in table 2 then the PID becomes invalid.
DECLARE @table1 TABLE (
pid INT,
cid INT
)
DECLARE @table2 TABLE (
cid INT
)
INSERT @table1
SELECT 901, 101 UNION ALL
SELECT 901, 102 UNION ALL
SELECT 901, 103 UNION ALL
SELECT 902, 102 UNION ALL
SELECT 902, 105 UNION ALL
SELECT 903, 105
INSERT @table2
SELECT 101 UNION ALL
SELECT 102 UNION ALL
SELECT 105
DECLARE @temp TABLE (
pid INT,
cid INT,
test INT
)
INSERT @temp
SELECT a.pid,
a.cid,
b.pid
FROM @table1 a
LEFT JOIN (
SELECT t1.pid,
t1.cid
FROM @table1 t1
LEFT JOIN @table2 t2
ON t1.cid = t2.cid
WHERE t2.cid IS NULL
) b
ON a.pid = b.pid
AND a.cid = b.cid
-- Compare the counts of nulls
SELECT a.pid
FROM (
SELECT pid,
COUNT(1) AS cnt
FROM @temp
GROUP BY pid
) a
INNER JOIN (
SELECT pid,
COUNT(1) AS cnt
FROM @temp
WHERE test IS NULL
GROUP BY pid
) b
ON a.pid = b.pid
AND a.cnt = b.cnt