DECLARE @VAL_START INT = 6
DECLARE @VAL_CON NVARCHAR(20) ='XX'
DECLARE @TBIDX AS TABLE
(
IDX INT,
ID INT,
NAME NVARCHAR(10)
)
INSERT INTO @TBIDX
SELECT ROW_NUMBER() OVER (ORDER BY ID ASC) AS IDX, ID, NAME FROM YYY WHERE NAME <> @VAL_CON
DECLARE @IDX INT
SELECT @IDX = IDX FROM @TBIDX WHERE ID = @VAL_START
SELECT * FROM @TBIDX WHERE IDX BETWEEN (@IDX - 5) AND (@IDX +5)
SELECT * FROM (SELECT TOP 6 * FROM YYY WHERE NAME <> 'XX' AND ID >= 6 ORDER BY ID ASC) AS TBF
UNION
SELECT * FROM ( SELECT TOP 6 * FROM YYY WHERE NAME <> 'XX' AND ID <= 6 ORDER BY ID DESC) AS TBL