新手一个,写的最简单的存储过程分页,上代码吧,还请大神们多多调教!
1 USE [BMS] 2 GO 3 /****** Object: StoredProcedure [dbo].[Pages] Script Date: 12/21/2015 11:48:57 ******/ 4 SET ANSI_NULLS ON 5 GO 6 SET QUOTED_IDENTIFIER ON 7 GO 8 ALTER PROCEDURE [dbo].[Pages] 9 @page int, --当前页 10 @rows int, --每页显示行数 11 @sidx varchar(50), --排序字段 12 @sord varchar(10), --排序规则 13 @tablename varchar(50), --表名 14 @where varchar(200), --查询条件 15 @id varchar(50) --表的主键字段名 16 AS 17 declare @sqlstr varchar(500) --拼接sql语句 18 BEGIN 19 SET NOCOUNT ON; 20 set @sqlstr=‘select top ‘+CAST(@rows AS varchar)+‘ * from ‘+@tablename+‘ where 1=1 ‘+@where 21 +‘and ‘+@id+‘ not in (select top ‘+CAST(((@page-1)*@rows) AS varchar)+‘ ‘+@id+‘ from ‘+@tablename+‘ order by ‘+@sidx+‘ ‘+@sord+‘) order by ‘+@sidx+‘ ‘+@sord+‘‘; 22 --print @sqlstr 23 EXEC(@sqlstr) 24 END;
时间: 2024-10-13 01:52:05