分頁SQL 和Oracle 預存程序

來源:互聯網
上載者:User

CREATE PROCEDURE [dbo].[sp_GetPagingList](
@Table nvarchar(1000),          --表名
@Field nvarchar(1000) = '*',        --讀取欄位
@Where  nvarchar(500) = NULL,       --Where條件
@GroupBy nvarchar(500) = NULL,      --分組
@OrderBy nvarchar(500),       --排序欄位必須
@PageNumber int = 1,            --開始頁碼
@PageSize int = 10,             --頁大小
@IsCount bit = 0            --是否返回記錄總數
)
AS
BEGIN
------------------------------------------------------------------------------------------------
DECLARE @strWhere nvarchar(500)                     --Where 條件
IF @Where IS NOT NULL AND @Where != ''              --Where 條件
BEGIN
SET @strWhere = ' WHERE ' + @Where + ' '
END
ELSE
BEGIN
SET @strWhere = ''
END
----------------------------------------------------------------------------------------------------
DECLARE @strGroupBy nvarchar(500)                   --GroupBy 條件
IF @GroupBy IS NOT NULL AND @GroupBy != ''          --GroupBy 條件
BEGIN
SET @strGroupBy = ' GROUP BY ' + @GroupBy + ' '
END
ELSE
BEGIN
SET @strGroupBy = ''
END
----------------------------------------------------------------------------------------------------
DECLARE @strOrderBy nvarchar(500)                   --OrderBy 條件

SET @strOrderBy = ' ORDER BY ' + @OrderBy

----------------------------------------------------------------------------------------------------
DECLARE @strSql nvarchar(max)   --Sql 語句

----------------------------------------------------------------------------------------------------
IF @PageNumber < 1                                  --第一頁提高效能
BEGIN   
SET @PageNumber = 1
END

IF @PageNumber = 1                          
BEGIN
SET @strSql = 'SELECT TOP ' + str(@PageSize) +  ' ' + @Field + ' FROM ' + @Table + 
   @strWhere + @strGroupBy + @strOrderBy
EXEC sp_executesql @strSql
END 
----------------------------------------------------------------------------------------------------
--根據 SqlServer 2005 協助得到下面的語句 
ELSE
BEGIN 
DECLARE @STARTID nvarchar(50)
DECLARE @ENDID nvarchar(50)
SET @STARTID = convert(nvarchar(50),(@PageNumber - 1) * @PageSize + 1)
SET @ENDID = convert(nvarchar(50),@PageNumber * @PageSize)
SET @strSql = 'WITH MYTABLE AS (SELECT ROW_NUMBER() OVER (' + @strOrderBy + ')
   AS RowNumber,' + @Field + ' FROM '+ @Table +  @strWhere + @strGroupBy + ') 
   SELECT * FROM MYTABLE 
   WHERE RowNumber BETWEEN ' + @STARTID + ' AND ' + @ENDID

  --oracle 分頁語句文法。
  -- select * from (select A.* ,ROWNUM RN from(select * from tb_test)A where ROWNUM<=@ENDID)where RN >=@STARTID
EXEC sp_executesql @strSql
END
--------------------------------------------------------------------------------------------------
--計算總行數
IF @IsCount = 1
BEGIN
SET @strSql= 'SELECT  Count (*) AS RecordCount FROM ' + @Table + @strWhere + @strGroupBy
EXEC sp_executesql @strSql   
END 
RETURN
END
return

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.