扩展性很好的一个分页存储过程分享

这是经常用的一个分页存储过程 希望大家指点不足
<div class="codetitle"><a style="CURSOR: pointer" data="23606" class="copybut" id="copybut23606" onclick="doCopy('code23606')"> 代码如下:

<div class="codebody" id="code23606">
USE [a6756475746]
GO
/** Object: StoredProcedure [dbo].[tbl_order_SearchWhereAndPage] Script Date: 11/01/2011 09:37:39 **/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[tbl_order_SearchWhereAndPage]
@AllCount int OUTPUT,
@PageIndex int,
@PageSize int,
@minDate datetime,
@maxDate datetime
AS
begin
DECLARE @PageLower int
set @PageLower=@PageSize @PageIndex
DECLARE @PageUpper int
set @PageUpper= @PageLower + @PageSize - 1 DECLARE @Searchsql nvarchar(4000)
set @Searchsql='SELECT
,( ROW_NUMBER() OVER (ORDER BY [ID] DESC) -1 ) AS RowNumber FROM tbl_order WHERE (1=1) '
DECLARE @SearchsqlCount nvarchar(4000)
set @SearchsqlCount='SELECT @count=Count() FROM tbl_order WHERE (1=1) '
declare @Result varchar
set @Result='' if @minDate>convert(datetime,'1900-1-2')
begin
set @Result=@Result+' and oDeliveryDate >= '''+convert(varchar(20),@minDate)+''''
end
if @maxDate > convert(datetime,'1900-1-2')
begin
set @Result=@Result+' and oDeliveryDate <= '''+convert(varchar(20),dateadd(dd,1,@maxDate))+''''
end
set @SearchsqlCount=@SearchsqlCount+@Result
set @Searchsql=@Searchsql+@Result SET @Searchsql = 'WITH t AS (' + @Searchsql +' )
SELECT
FROM t
WHERE [RowNumber] BETWEEN '+ convert(varchar(50),@PageLower) +' AND '+ convert(varchar(50),@PageUpper) + '
ORDER BY RowNumber ' exec (@Searchsql) exec sp_executesql @SearchsqlCount,N'@count as int out',@AllCount out print @Searchsql print @AllCount
end
exec (@Result)

相关文章

SELECT a.*,b.dp_name,c.pa_name,fm_name=(CASE WHEN a.fm_n...
if not exists(select name from syscolumns where name=&am...
select a.*,pano=a.pa_no,b.pa_name,f.dp_name,e.fw_state_n...
要在 SQL Server 2019 中设置定时自动重启,可以使用 Window...
您收到的错误消息表明数据库 &#39;EastRiver&#39; 的...
首先我需要查询出需要使用SQL Server Profiler跟踪的数据库标...