4个回答
展开全部
可以
不过不用游标也可以
select * from (select *,row_number() over(order by 排序列) as rownum from table) a
where rownum>=n and rownum<=m
不过不用游标也可以
select * from (select *,row_number() over(order by 排序列) as rownum from table) a
where rownum>=n and rownum<=m
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
--用游标
DECLARE @COLUMN1 VARCHAR(10)
DECLARE @LineNum INT
SET @LineNum = 1
DECLARE CUR_TEST CURSOR FOR
SELECT 语句
OPEN CUR_TEST
FETCH NEXT FROM CUR_TEST INTO @COLUMN1,...
WHILE @@FETCH_STATUS = 0
BEGIN
IF (@LineNum >= N and @LineNum <= M)
BEGIN
SELECT @COLUMN1,...
END
SET @LineNum = @LineNum + 1
END
CLOSE CUR_TEST
DEALLOCATE CUR_TEST
--用双TOP直接取出,从前M条倒序取出前(M-N)条,等同于取出了N到M之间的
SELECT TOP (M-N) * FROM (SELECT TOP M * FROM 表名 WHERE 语句 ORDER BY COLUMNname DESC)
DECLARE @COLUMN1 VARCHAR(10)
DECLARE @LineNum INT
SET @LineNum = 1
DECLARE CUR_TEST CURSOR FOR
SELECT 语句
OPEN CUR_TEST
FETCH NEXT FROM CUR_TEST INTO @COLUMN1,...
WHILE @@FETCH_STATUS = 0
BEGIN
IF (@LineNum >= N and @LineNum <= M)
BEGIN
SELECT @COLUMN1,...
END
SET @LineNum = @LineNum + 1
END
CLOSE CUR_TEST
DEALLOCATE CUR_TEST
--用双TOP直接取出,从前M条倒序取出前(M-N)条,等同于取出了N到M之间的
SELECT TOP (M-N) * FROM (SELECT TOP M * FROM 表名 WHERE 语句 ORDER BY COLUMNname DESC)
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
SQL Server 游标这样用
DECLARE @N INT,@M INT
SELECT @N=5,@M=10
DECLARE Cur1 SCROLL CURSOR FOR
SELECT * FROM sysobjects
OPEN Cur1
FETCH ABSOLUTE 5 FROM Cur1
DECLARE @i INT
SET @i=1
WHILE @@FETCH_STATUS=0
BEGIN
SET @i=@i+1
IF @i<=@M-@N+1
FETCH NEXT FROM Cur1
ELSE
GOTO Roy
END
Roy:
CLOSE Cur1
DEALLOCATE Cur1
DECLARE @N INT,@M INT
SELECT @N=5,@M=10
DECLARE Cur1 SCROLL CURSOR FOR
SELECT * FROM sysobjects
OPEN Cur1
FETCH ABSOLUTE 5 FROM Cur1
DECLARE @i INT
SET @i=1
WHILE @@FETCH_STATUS=0
BEGIN
SET @i=@i+1
IF @i<=@M-@N+1
FETCH NEXT FROM Cur1
ELSE
GOTO Roy
END
Roy:
CLOSE Cur1
DEALLOCATE Cur1
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
当然可以,但如果想查第N条到第M条记录会有很多比游标更方便快速的方法,具体的可Hi我谈谈
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |