Let’s say we have a database with 1 one-column table. It contains 1000 same records. Could you please give at least 1 solution to help get records between line 5 and 7. No line number, row id or index etc
...全文
35710打赏收藏
一道面试题!
Let’s say we have a database with 1 one-column table. It contains 1000 same records. Could you please give at least 1 solution to help get records between line 5 and 7. No line number, row id or index etc
insert into Test values('1')
insert into Test values('1')
insert into Test values('1')
insert into Test values('1')
insert into Test values('1')
insert into Test values('1')
insert into Test values('1')
insert into Test values('1')
insert into Test values('1')
insert into Test values('1')
--开始行
declare @RowStart int
select @RowStart=5
--结束行
declare @RowEnd int
select @RowEnd=7
declare @i int
declare @RowCount int
select @RowStart=@RowStart+1
select @RowCount=@RowEnd-@RowStart
select @i=0
declare my_cursor SCROLL cursor
for
select col1 from Test
--Open Cursor
open my_cursor
fetch ABSOLUTE @RowStart from my_cursor
while @i<@RowCount
begin
select @i=@i+1
fetch Next from my_cursor
end
close my_cursor
deallocate my_cursor