1. 程式人生 > >MSSQL的分頁查詢儲存過程

MSSQL的分頁查詢儲存過程

set ANSI_NULLS ON  
set QUOTED_IDENTIFIER ON  
go  
  
--分頁儲存過程    
CREATE PROCEDURE [dbo].[sp_Paging]   
(   
@Tables nvarchar(1000),                --表名/檢視名  
@PrimaryKey nvarchar(100),             --主鍵  
@Sort nvarchar(200) = NULL,            --排序欄位(不帶order by)  
@pageindex int = 1,                    --當前頁碼  
@PageSize int = 10,                    --每頁記錄數  
@Fields nvarchar(1000) = N'*',         --輸出欄位  
@Filter nvarchar(1000) = NULL,         --where過濾條件(不帶where)  
@Group nvarchar(1000) = NULL          --Group語句(不帶Group By)  
)   
AS     
DECLARE @SortTable nvarchar(100)   
DECLARE @SortName nvarchar(100)   
DECLARE @strSortColumn nvarchar(200)   
DECLARE @operator char(2)   
DECLARE @type nvarchar(100)   
DECLARE @prec int   
  
--設定排序語句  
IF @Sort IS NULL OR @Sort = ''       
   SET @Sort = @PrimaryKey        
IF CHARINDEX('DESC',@Sort)>0     
BEGIN           
    SET @strSortColumn = REPLACE(@Sort, 'DESC', '')           
    SET @operator = '<='       
END   
ELSE       
BEGIN                  
    SET @strSortColumn = REPLACE(@Sort, 'ASC', '')                  
    SET @operator = '>='       
END   
IF CHARINDEX('.', @strSortColumn) > 0       
BEGIN           
    SET @SortTable = SUBSTRING(@strSortColumn, 0, CHARINDEX('.',@strSortColumn))  
    SET @SortName = SUBSTRING(@strSortColumn, CHARINDEX('.',@strSortColumn) + 1, LEN(@strSortColumn))       
END   
ELSE       
BEGIN           
    SET @SortTable = @Tables           
    SET @SortName = @strSortColumn    
END   
  
--設定排序欄位型別和精度   
SELECT @type=t.name, @prec=c.prec FROM sysobjects o   
       JOIN syscolumns c on o.id=c.id   
       JOIN systypes t on c.xusertype=t.xusertype WHERE o.name = @SortTable AND c.name = @SortName  
          
IF CHARINDEX('char', @type) > 0      
   SET @type = @type + '(' + CAST(@prec AS varchar) + ')'  
     
DECLARE @strPageSize nvarchar(50)   
DECLARE @strStartRow nvarchar(50)   
DECLARE @strFilter nvarchar(1000)   
DECLARE @strSimpleFilter nvarchar(1000)   
DECLARE @strGroup nvarchar(1000)    
   
IF @pageindex <1       
   SET @pageindex = 1    
SET @strPageSize = CAST(@PageSize AS nvarchar(50))   
--設定開始分頁記錄數   
SET @strStartRow = CAST(((@pageindex - 1)*@PageSize + 1) AS nvarchar(50))    
--篩選以及分組語句  
IF @Filter IS NOT NULL AND @Filter != ''       
BEGIN           
    SET @strFilter = ' WHERE ' + @Filter + ' '   
    SET @strSimpleFilter = ' AND ' + @Filter + ' '   
END   
ELSE       
BEGIN           
    SET @strSimpleFilter = ''           
    SET @strFilter = ''     
    SET @strGroup='';    
END   
IF @Group IS NOT NULL AND @Group != ''    
   SET @strGroup = ' GROUP BY '
[email protected]
; /* --計算總記錄數 DECLARE @TotalCountSql nvarchar(1000) SET @TotalCountSql=N'SELECT @TotalCount=COUNT(*)' +N' FROM ' + @Tables + @strFilter EXEC sp_executesql @TotalCountSql,N'@TotalCount int OUTPUT',@TotalCount OUTPUT */ --執行查詢語句 declare @sql varchar(2000); declare @sql2 varchar(1000); declare @sql3 varchar(1000); declare @sql4 varchar(1000); declare @sql5 varchar(1000); set @sql = 'DECLARE @SortColumn ' + @type+';'; set @sql2 = 'SET ROWCOUNT ' + @strStartRow+';'; /* print '@strSortColumn='
[email protected]
; print '@Tables:'[email protected]; print '@strFilter:'[email protected]; print '@strGroup:'; print '@Sort:'[email protected]; */ set @sql3='SELECT @SortColumn=' + @strSortColumn + ' FROM ' + @Tables + @strFilter + ' ORDER BY ' + @Sort+';'; set @sql4='SET ROWCOUNT ' + @strPageSize+';'; set @sql5='SELECT ' + @Fields + ' FROM ' + @Tables + ' WHERE ' + @strSortColumn + @operator + ' @SortColumn ' + @strSimpleFilter + ' ORDER BY ' + @Sort + ''+';'; /* print 'sql:'
[email protected]
; print 'sql2:'[email protected]; print 'sql3:'[email protected]; print 'sql4:'[email protected]; print 'sql5:'[email protected]; */ set @sql = @[email protected][email protected][email protected][email protected]; print @sql; EXEC(@sql)