DECLARE @articleId int SET @articleId = ? WITH graphTraversal (contentId, fromId, hops) AS( SELECT DISTINCT (CASE WHEN content_id <> @articleId THEN CONVERT(int,content_id) ELSE CONVERT(int,content_utf_id) END ) as id, (CASE WHEN content_id <> @articleId THEN CONVERT(int,content_utf_id) ELSE CONVERT(int,content_id) END) as fromId, 1 as hops FROM admin_content_translations WHERE content_id = @articleId OR content_utf_id = @articleId UNION ALL SELECT (CASE WHEN tpp.content_id <> tx.contentId THEN CONVERT(int,content_id) ELSE CONVERT(int,content_utf_id) END ) as id, (CASE WHEN content_id <> tx.contentId THEN CONVERT(int,content_utf_id) ELSE CONVERT(int,content_id) END) as fromId, tx.hops + 1 AS hops FROM admin_content_translations AS tpp INNER JOIN graphTraversal AS tx ON (tpp.content_utf_id = tx.contentId AND tx.fromId <> tpp.content_id ) OR (tpp.content_id = tx.contentId AND tx.fromId <> tpp.content_utf_id ) ) SELECT DISTINCT contentId AS ID FROM graphTraversal WHERE contentid <> @articleId