Select Case when B.ID IS NULL then A.ID else B.ID END ID, A.ColA, A.ColB, B.colX, B.colY
from
(Select ID, (Select ID From (Select ROW_NUMBER() over(order by ID) row,* from TA) A where A.row = temA.row +1) nextID, ColA, ColB from
(Select ROW_NUMBER() over(order by ID) row,* from TA) temA ) A
LEFT join TB B ON A.ID = B.ID
union
Select Case when B.ID IS NULL then A.ID else B.ID END ID, A.ColA, A.ColB, B.colX, B.colY
from
(Select ID, (Select ID From (Select ROW_NUMBER() over(order by ID) row,* from TA) A where A.row = temA.row +1) nextID, ColA, ColB from
(Select ROW_NUMBER() over(order by ID) row,* from TA) temA ) A
join TB B
ON (B.ID >A.ID AND B.ID < A.nextID ) OR (A.nextID IS NULL AND B.ID >= A.ID )
from
(Select ID, (Select ID From (Select ROW_NUMBER() over(order by ID) row,* from TA) A where A.row = temA.row +1) nextID, ColA, ColB from
(Select ROW_NUMBER() over(order by ID) row,* from TA) temA ) A
LEFT join TB B ON A.ID = B.ID
union
Select Case when B.ID IS NULL then A.ID else B.ID END ID, A.ColA, A.ColB, B.colX, B.colY
from
(Select ID, (Select ID From (Select ROW_NUMBER() over(order by ID) row,* from TA) A where A.row = temA.row +1) nextID, ColA, ColB from
(Select ROW_NUMBER() over(order by ID) row,* from TA) temA ) A
join TB B
ON (B.ID >A.ID AND B.ID < A.nextID ) OR (A.nextID IS NULL AND B.ID >= A.ID )