日期:2014-05-18 浏览次数:20796 次
--查询数据表第几行至第几行数据的SQL语句 --如N行到M行(M>N) select top (M-N) * from tablename where id not in (select top N id from tablename) --id 为字段名,N为整型循环变量 --稍作变形即可 select top 1 * from tablename where id not in (select top N id from tablename)
------解决方案--------------------
CREATE TABLE TEST (ID INT IDENTITY(1,1) ,NAME VARCHAR(20)) INSERT INTO TEST SELECT 'ZHANG' UNION ALL SELECT 'LI' UNION ALL SELECT 'CHEN' DECLARE @NUM INT SET @NUM=1 WHILE(@NUM<=100) BEGIN SELECT * FROM (SELECT *,ROW_NUMBER() OVER(ORDER BY GETDATE()) AS ROWID FROM TEST) TB WHERE ROWID=@NUM SET @NUM=@NUM+1 END
------解决方案--------------------
变通一下啊:
if exists(select 1 from sysobjects where name = 'PageRecord' and xtype = 'p') drop procedure PageRecord go CREATE procedure [dbo].[PageRecord] @Sql nvarchar(4000), --查询字符串 @PageIndex int, --第n页 设置为第几行即可! @PageSize int --每页行数 设置为1即可! as set nocount on declare @p1 int, --p1是游标的id @rowcount int exec sp_cursoropen @p1 output,@Sql,@scrollopt=1,@ccopt=1,@rowcount=@rowcount output set @PageIndex=(@PageIndex-1)*@PageSize+1 exec sp_cursorfetch @p1,16,@PageIndex,@PageSize select @rowcount --select ceiling(1.0*@rowcount/@pagesize) as 总页数--,@rowcount as 总行数,@currentpage as 当前页 exec sp_cursorclose @p1 set nocount off GO --每次读取1条数据 EXEC [PageRecord] "SELECT * FROM USERINFO",1,1 go EXEC [PageRecord] "SELECT * FROM USERINFO",2,1 go