declare @s varchar(100) = '1 2 5 22 4 6 -1 -5 -7 6 18 16 -9 2 5 -8 6 9 3 -7';
declare @t int = 15;
with i as
(
select cast(left(@s,charindex(' ',@s+' ')-1) as int) x,
stuff(@s,1,charindex(' ',@s+' '),'') as s
union all
select cast(left(s,charindex(' ',s+' ')-1) as int) x,
stuff(s,1,charindex(' ',s+' '),'') as s
from i
where s <> ''
)
,z as
(
select x,rank() over(order by x) r
from i
)
select distinct a.x, b.x
from z a, z b
where a.x+b.x = @t
and a.r<b.r