Skip to content

Instantly share code, notes, and snippets.

@pavelpower
Created September 27, 2013 06:55
Show Gist options
  • Save pavelpower/6725009 to your computer and use it in GitHub Desktop.
Save pavelpower/6725009 to your computer and use it in GitHub Desktop.
рекурсивный запрос в T-SQL
WITH PCommentList ([ProductId]
,[ProductCommentId]
,[ProductCommentPId]
,[ProductCommentText]
,[ProductCommentAddDate]
,[ProductCommentAddUserId]
,[ProductCommentAddUserName]
,[IsApproved]
,[IsLocked])
AS(
SELECT *
FROM [basename].[dbo].[ProductComments]
WHERE [ProductCommentId] = 113 --- id того сообщения которого возвращаем ветку
UNION ALL
SELECT comm.*
FROM [basename].[dbo].[ProductComments] as comm
Inner JOIN PCommentList as pl
ON comm.ProductCommentPId = pl.ProductCommentId
)
SELECT * FROM PCommentList;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment