程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> 其他數據庫知識 >> 更多數據庫知識 >> 擴展性很好的一個分頁存儲過程分享

擴展性很好的一個分頁存儲過程分享

編輯:更多數據庫知識

這是經常用的一個分頁存儲過程 希望大家指點不足
復制代碼 代碼如下:
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](5000)
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)

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved