分頁帶查詢的存儲過程示例

CREATE PROCEDURE [dbo].[edunet_select_view_OrganResource]
    @start int=0,
    @limit int=100000,
    @colums varchar(2000) = '*',
    @OrderByStr varchar(1000)='',
    @recordCount int=0 output,
    @CountPage int=0 output,
----------------------------------------------------------------------------------------------------------------------------------------------
    @p_max_OrderID bigint = null,
    @p_min_OrderID bigint=null,
    @p_max_ResourceID int = null,
    @p_min_ResourceID int=null,
    @p_max_OrgResourceTypeId bigint = null,
    @p_min_OrgResourceTypeId bigint=null,
    @p_max_OrganID int = null,
    @p_min_OrganID int=null,
    @p_OrgResourceTypeName varchar(100) = null,
    @p_max_SysTypeID int = null,
    @p_min_SysTypeID int=null,
    @p_max_UserSourceTypeID int = null,
    @p_min_UserSourceTypeID int=null,
    @p_ResourceName varchar(50) = null,
    @p_DownloadUrl varchar(500) = null,
    @p_PlayUrl varchar(500) = null,
    @p_ResourcePath varchar(50) = null,
    @p_Remark varchar(4000) = null,
    @p_max_Score float = null,
    @p_min_Score float=null,
    @p_max_EduB float = null,
    @p_min_EduB float=null,
    @p_max_UploadTime datetime = null,
    @p_min_UploadTime datetime=null,
    @p_max_ResourceSize float = null,
    @p_min_ResourceSize float=null,
    @p_picurl varchar(300) = null,
    @p_max_ShowIndex int = null,
    @p_min_ShowIndex int=null
----------------------------------------------------------------------------------------------------------------------------------------------
AS
    if(@start is null)
        set @start = 0
    if(@limit is null or @limit = 0)
        set @limit = 100000
    if(@colums is null)
        set @colums = '*'
    if @OrderByStr <> ''
        set @OrderByStr = ' order by ' + @OrderByStr
    else
        set @OrderByStr = ' order by orderid'
----------------------------------------------------------------------------------------------------------------------------------------------
    declare @FdName varchar(100)
    declare @FromStr  varchar(1000)
    set @FdName = 'OrderID'
    set @FromStr = 'view_OrganResource'
    declare @WhereStr varchar(2000)
    declare @Id1 varchar(20),@Id2 varchar(20)
    declare @CountSql nvarchar(4000)
    declare @TmpStr varchar(2000)
    declare @PageCurrent int

    set @PageCurrent = FLOOR((@start+@limit -1) / @limit) + 1
    set  @WhereStr = ' where 1=1'
----------------------------------------------------------------------------------------------------------------------------------------------
    if(@p_max_OrderID is not null and @p_max_OrderID <> 0)
     set  @WhereStr =  @WhereStr + ' and OrderID <= ' + cast (@p_max_OrderID as varchar(20))
     if(@p_min_OrderID is not null and @p_min_OrderID <> 0)
     set  @WhereStr =  @WhereStr + ' and OrderID >= ' + cast (@p_min_OrderID as varchar(20))
    if(@p_max_ResourceID is not null and @p_max_ResourceID <> 0)
     set  @WhereStr =  @WhereStr + ' and ResourceID <= ' + cast (@p_max_ResourceID as varchar(20))
     if(@p_min_ResourceID is not null and @p_min_ResourceID <> 0)
     set  @WhereStr =  @WhereStr + ' and ResourceID >= ' + cast (@p_min_ResourceID as varchar(20))
    if(@p_max_OrgResourceTypeId is not null and @p_max_OrgResourceTypeId <> 0)
     set  @WhereStr =  @WhereStr + ' and OrgResourceTypeId <= ' + cast (@p_max_OrgResourceTypeId as varchar(20))
     if(@p_min_OrgResourceTypeId is not null and @p_min_OrgResourceTypeId <> 0)
     set  @WhereStr =  @WhereStr + ' and OrgResourceTypeId >= ' + cast (@p_min_OrgResourceTypeId as varchar(20))
    if(@p_max_OrganID is not null and @p_max_OrganID <> 0)
     set  @WhereStr =  @WhereStr + ' and OrganID <= ' + cast (@p_max_OrganID as varchar(20))
     if(@p_min_OrganID is not null and @p_min_OrganID <> 0)
     set  @WhereStr =  @WhereStr + ' and OrganID >= ' + cast (@p_min_OrganID as varchar(20))
    if(@p_OrgResourceTypeName is not null and @p_OrgResourceTypeName<>'')
        set  @WhereStr =  @WhereStr + ' and OrgResourceTypeName like ''%' + @p_OrgResourceTypeName +   '%'''
    if(@p_max_SysTypeID is not null and @p_max_SysTypeID <> 0)
     set  @WhereStr =  @WhereStr + ' and SysTypeID <= ' + cast (@p_max_SysTypeID as varchar(20))
     if(@p_min_SysTypeID is not null and @p_min_SysTypeID <> 0)
     set  @WhereStr =  @WhereStr + ' and SysTypeID >= ' + cast (@p_min_SysTypeID as varchar(20))
    if(@p_max_UserSourceTypeID is not null and @p_max_UserSourceTypeID <> 0)
     set  @WhereStr =  @WhereStr + ' and UserSourceTypeID <= ' + cast (@p_max_UserSourceTypeID as varchar(20))
     if(@p_min_UserSourceTypeID is not null and @p_min_UserSourceTypeID <> 0)
     set  @WhereStr =  @WhereStr + ' and UserSourceTypeID >= ' + cast (@p_min_UserSourceTypeID as varchar(20))
    if(@p_ResourceName is not null and @p_ResourceName<>'')
        set  @WhereStr =  @WhereStr + ' and ResourceName like ''%' + @p_ResourceName +   '%'''
    if(@p_DownloadUrl is not null and @p_DownloadUrl<>'')
        set  @WhereStr =  @WhereStr + ' and DownloadUrl like ''%' + @p_DownloadUrl +   '%'''
    if(@p_PlayUrl is not null and @p_PlayUrl<>'')
        set  @WhereStr =  @WhereStr + ' and PlayUrl like ''%' + @p_PlayUrl +   '%'''
    if(@p_ResourcePath is not null and @p_ResourcePath<>'')
        set  @WhereStr =  @WhereStr + ' and ResourcePath like ''%' + @p_ResourcePath +   '%'''
    if(@p_Remark is not null and @p_Remark<>'')
        set  @WhereStr =  @WhereStr + ' and Remark like ''%' + @p_Remark +   '%'''
    if(@p_max_Score is not null and @p_max_Score <> 0)
     set  @WhereStr =  @WhereStr + ' and Score <= ' + cast (@p_max_Score as varchar(20))
     if(@p_min_Score is not null and @p_min_Score <> 0)
     set  @WhereStr =  @WhereStr + ' and Score >= ' + cast (@p_min_Score as varchar(20))
    if(@p_max_EduB is not null and @p_max_EduB <> 0)
     set  @WhereStr =  @WhereStr + ' and EduB <= ' + cast (@p_max_EduB as varchar(20))
     if(@p_min_EduB is not null and @p_min_EduB <> 0)
     set  @WhereStr =  @WhereStr + ' and EduB >= ' + cast (@p_min_EduB as varchar(20))
    if(@p_max_UploadTime is not null)
     set  @WhereStr =  @WhereStr + ' and UploadTime <= ''' + cast (@p_max_UploadTime as varchar(20)) + '''' 
     if(@p_min_UploadTime is not null)
     set  @WhereStr =  @WhereStr + ' and UploadTime >= ''' + cast (@p_min_UploadTime as varchar(20)) + ''''
    if(@p_max_ResourceSize is not null and @p_max_ResourceSize <> 0)
     set  @WhereStr =  @WhereStr + ' and ResourceSize <= ' + cast (@p_max_ResourceSize as varchar(20))
     if(@p_min_ResourceSize is not null and @p_min_ResourceSize <> 0)
     set  @WhereStr =  @WhereStr + ' and ResourceSize >= ' + cast (@p_min_ResourceSize as varchar(20))
    if(@p_picurl is not null and @p_picurl<>'')
        set  @WhereStr =  @WhereStr + ' and picurl like ''%' + @p_picurl +   '%'''
    if(@p_max_ShowIndex is not null and @p_max_ShowIndex <> 0)
     set  @WhereStr =  @WhereStr + ' and ShowIndex <= ' + cast (@p_max_ShowIndex as varchar(20))
     if(@p_min_ShowIndex is not null and @p_min_ShowIndex <> 0)
     set  @WhereStr =  @WhereStr + ' and ShowIndex >= ' + cast (@p_min_ShowIndex as varchar(20)) 
----------------------------------------------------------------------------------------------------------------------------------------------
    declare @PageSql nvarchar(max)
    set @PageSql = 'WITH OrderedResults AS (SELECT '
    + @colums
    + ', ROW_NUMBER() OVER ('
    + @OrderByStr
    + ' ) as RowNumber FROM dbo.view_OrganResource '
    + @WhereStr
    + ')  SELECT '
    + @colums
    + ' FROM OrderedResults WHERE RowNumber between '
    + cast(@start +1 as varchar(10)) + ' and ' 
    + cast(@start + @limit as varchar(10))
    exec sp_executesql @PageSql
----------------------------------------------------------------------------------------------------------------------------------------------
    set @CountSql = 'select @a=count(1)  from '+@FromStr+@WhereStr
    exec sp_executesql @CountSql,N'@a int output',@recordCount output
    -----返回總頁數
    if @recordCount <= @limit
        set @CountPage = 1
    else
    begin
        set @CountPage = @recordCount / @limit
        if (@recordCount % @limit) > 0
           set @CountPage = @CountPage + 1
    end
    select @countpage as countPage,@recordCount  as recordCount
----------------------------------------------------------------------------------------------------------------------------------------------


發佈了56 篇原創文章 · 獲贊 6 · 訪問量 23萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章