Not such a beautiful way to do it ... but
create procedure GetRangeBetween (@i11 int, @i12 char, @i21 int, @i22 char) AS BEGIN if object_id ('tempdb..#Test') is not null drop table #Test create table #Test (i1 int, i2 nvarchar(10), [Rank] int) insert into #Test(i1, i2) values (1, 'a'), (1, 'b'), (1, 'c'), (1, 'd'), (2, 'x'), (3, 'y'), (4, 'a'), (4, 'b'), (4, 'c') update #Test set Rank = src.[srcRank] from #Test t join (select *, row_number() over (order by i1) [srcRank] from #Test) src on t.i1 = src.i1 and t.i2 = src.i2 declare @Rank1 int = (select [Rank] from #Test where i1 = @i11 and i2 = @i12) declare @Rank2 int = (select [Rank] from #Test where i1 = @i21 and i2 = @i22) select i1, i2 from #Test where (i1 between @i11 and @i21) and ([Rank] between @Rank1 and @Rank2) END
Then you just execute it with ...
execute GetRangeBetween 1, 'c', 4, 'a'
source share