关于SQL中RowNum分页

USE [TIBCOSTG]
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:        <Tomas Shao>
-- Create date: <2011/04/22>
-- Description:    <Fetch the result of DWOrders filter by customer>
-- =============================================
ALTER PROCEDURE [dbo].[WS_RC_GetDWOrdersByFilter]
(       
      @queryDate int = NULL,    -- Whether query adddate  1 do  0 not
      @startDate varchar(25) = NULL,    -- Start Date for the Invoice Range Filter
      @endDate varchar(25) = NULL,        -- End Date for the Invoice Range Filter
      @endUserID varchar(33) = NULL,    -- EU ID for the EU Filter
      @endUserName nvarchar(200) = NULL,-- EU Name for the EU Filter
      @orderID varchar(33) = NULL,        -- Order ID for the InvoiceID Filter
      @poNumber varchar(33) = NULL,    -- custPO for the Invoice# Filter
       
        @fldName varchar(255),
        @PageSize   int ,          -- page size
        @PageIndex  int ,         -- page
        @Sort varchar(255),      --asc or desc
        @DoCount int = null
)
AS
declare @tblName varchar(255)
declare @fldNameTemp varchar(255)
declare @strWhere  varchar(1500)
declare @strSQL   varchar(5000)      
declare @strTmp   varchar(110)       
declare @strOrder varchar(400)       
declare @strGetFields varchar(1000)

set @fldNameTemp = @fldName
set @tblName = 'dbo.DW_Order ord
        LEFT JOIN dbo.DW_Organisation org ON ord.customerId = org.commonId
        LEFT JOIN dbo.DW_Person p ON ord.salesRep = p.commonId'
set @strGetFields = 'ord.commonId as commonId, ord.addDate, ord.custPO, ord.orderNumber, org.name AS customerName, p.firstName + '' '' + p.lastName AS salesRep, ord.totalPrice'
    if @queryDate = 1
        BEGIN
            set @strWhere = ' CONVERT(VARCHAR, ord.addDate, 120) BETWEEN ''' + @startDate + ''' AND ''' + @endDate + ''''
        END
    else
        BEGIN
            set @strWhere = '1 = 1 '
        END
       
    if @endUserID != ''
        set @strWhere = @strWhere + ' AND org.commonId = ''' + @endUserID + ''''
    else if @endUserName != ''
        set @strWhere = @strWhere + ' AND org.name = ''' + @endUserName + ''''
    else if @poNumber != ''
        set @strWhere = @strWhere + ' AND ord.custPO = ''' + @poNumber + ''''
    else if @orderID != ''
        set @strWhere = @strWhere + ' AND ord.commonId = ''' + @orderID + ''''

    if @fldName = 'salesRep'
        set @fldNameTemp = 'p.firstName + '' '' + p.lastName'
    else if @fldName = 'customerName'
        set @fldNameTemp = 'org.name'

if @doCount != 0
    begin
    if @strWhere != ''''
    set @strSQL = 'select count(ord.commonId) as Total from ' + @tblName + ' where '+@strWhere
    else
    set @strSQL = 'select count(ord.commonId) as Total from ' + @tblName + ''
    end
else
    begin
    SET   ANSI_NULLS   OFF
    if @PageIndex = 1
        set @strSQL = 'select * from (SELECT ROW_NUMBER() OVER(ORDER BY '+@fldNameTemp+' '+@Sort+') AS RowNum, '+@strGetFields +
        ' from '+@tblName+ ' where ' + @strWhere + ') as t where t.RowNum between 1 and ' + str(@PageSize)
    else
        set @strSQL = 'select * from (SELECT ROW_NUMBER() OVER(ORDER BY '+@fldNameTemp+' '+@Sort+') AS RowNum, '+@strGetFields +
        ' from '+@tblName+ ' where ' + @strWhere + ') as t where t.RowNum between '+str(@PageIndex*@PageSize+1) +' and ' + str((@PageIndex+1)*@PageSize)
end

exec (@strSQL)
--print(@strSQL)
RETURN

 

 

注:ORDER BY中的数据库字段不用使用别名的字段,否则排序无效。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值