sql2005带参数的分页存储过程

USE [CustomerManagementCenter]
GO
/****** Object:  StoredProcedure [dbo].[sp_SearchCustomers]    Script Date: 11/01/2010 13:51:23 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:  <Radeon Ling>
-- Create date: <2010-10-27>
-- Description: <Search the customers for customer management page>
-- =============================================
ALTER PROCEDURE [dbo].[sp_SearchCustomers]
 -- Add the parameters for the stored procedure here
(
 @SortColumns VARCHAR(100)
 , @GroupName VARCHAR(50)
 , @StartRow INT
 , @PageSize INT
 , @CustomerLevel VARCHAR(50)
 , @DataSource VARCHAR(50)
 , @CustomerName VARCHAR(50)
 , @City VARCHAR(50)
 , @MobileNumber VARCHAR(50)
)
AS
BEGIN
 -- SET NOCOUNT ON added to prevent extra result sets from
 -- interfering with SELECT statements.
 SET NOCOUNT ON;
 DECLARE @CountRows INT;

 WITH PagedResults AS
 (
  SELECT CB.CustomerLevel
    ,CB.Customerid
    ,CB.DataSource
    ,CB.CustomerName
    ,CB.City
    ,CB.MobileNumber
    ,ROW_NUMBER() OVER (ORDER BY @SortColumns) AS ResultSetRowNumber 
  FROM tb_Customers_Basic AS CB LEFT JOIN tb_Message_CustomerInGroup AS MCIG ON MCIG.Customerid = CB.Customerid
   LEFT JOIN dbo.tb_Message_CustomerGroup_Basic AS MCGB ON MCIG.GroupID = MCGB.GroupID
  WHERE CB.ActiveStatus=1
   AND
   (
    (@GroupName<>'' and MCGB.GroupName = @GroupName)
    OR (@GroupName='' and MCGB.GroupName  is null )
    OR (@GroupName='' and MCGB.GroupName  is not null )
    OR (@GroupName IS NULL AND MCGB.GroupName  is not null)
   )
   AND
   (
    (@CustomerLevel<>'' and CB.CustomerLevel = @CustomerLevel)
    OR (@CustomerLevel='' and CB.CustomerLevel  is null )
    OR (@CustomerLevel='' and CB.CustomerLevel  is not null )
    OR (@CustomerLevel IS NULL AND CB.CustomerLevel is not null)
   )
   AND
   (
    (@DataSource<>'' and CB.DataSource = @DataSource)
    OR (@DataSource='' and CB.DataSource  is null )
    OR (@DataSource='' and CB.DataSource  is not null )
    OR (@DataSource IS NULL AND CB.DataSource  is not null)
   )
   AND
   (
    (@CustomerName<>'' and CB.CustomerName = @CustomerName)
    OR (@CustomerName='' and CB.CustomerName  is null )
    OR (@CustomerName='' and CB.CustomerName  is not null )
    OR (@CustomerName IS NULL AND CB.CustomerName  is not null)
   )
   AND
   (
    (@City<>'' and CB.City = @City)
    OR (@City='' and CB.City is null )
    OR (@City='' and CB.City is not null )
    OR (@City IS NULL AND CB.City  is not null)
   )
   AND
   (
    (@MobileNumber<>'' and CB.MobileNumber = @MobileNumber)
    OR (@MobileNumber='' and CB.MobileNumber is null )
    OR (@MobileNumber='' and CB.MobileNumber is not null )
    OR (@MobileNumber IS NULL AND CB.MobileNumber  is not null)
   )
 )
 
 SELECT *
 FROM PagedResults
 WHERE PagedResults.ResultSetRowNumber > @StartRow
  AND PagedResults.ResultSetRowNumber <= (@StartRow + @PageSize);
  
 WITH PagedResults AS
 (
  SELECT CB.CustomerLevel
    ,CB.DataSource
    ,CB.CustomerName
    ,CB.City
    ,CB.MobileNumber
    ,ROW_NUMBER() OVER (ORDER BY @SortColumns) AS ResultSetRowNumber 
  FROM tb_Customers_Basic AS CB LEFT JOIN tb_Message_CustomerInGroup AS MCIG ON MCIG.Customerid = CB.Customerid
   LEFT JOIN dbo.tb_Message_CustomerGroup_Basic AS MCGB ON MCIG.GroupID = MCGB.GroupID
  WHERE CB.ActiveStatus=1
   AND
   (
    (@GroupName<>'' and MCGB.GroupName = @GroupName)
    OR (@GroupName='' and MCGB.GroupName  is null )
    OR (@GroupName='' and MCGB.GroupName  is not null )
    OR (@GroupName IS NULL AND MCGB.GroupName  is not null)
   )
   AND
   (
    (@CustomerLevel<>'' and CB.CustomerLevel = @CustomerLevel)
    OR (@CustomerLevel='' and CB.CustomerLevel  is null )
    OR (@CustomerLevel='' and CB.CustomerLevel  is not null )
    OR (@CustomerLevel IS NULL AND CB.CustomerLevel is not null)
   )
   AND
   (
    (@DataSource<>'' and CB.DataSource = @DataSource)
    OR (@DataSource='' and CB.DataSource  is null )
    OR (@DataSource='' and CB.DataSource  is not null )
    OR (@DataSource IS NULL AND CB.DataSource  is not null)
   )
   AND
   (
    (@CustomerName<>'' and CB.CustomerName = @CustomerName)
    OR (@CustomerName='' and CB.CustomerName  is null )
    OR (@CustomerName='' and CB.CustomerName  is not null )
    OR (@CustomerName IS NULL AND CB.CustomerName  is not null)
   )
   AND
   (
    (@City<>'' and CB.City = @City)
    OR (@City='' and CB.City is null )
    OR (@City='' and CB.City is not null )
    OR (@City IS NULL AND CB.City  is not null)
   )
   AND
   (
    (@MobileNumber<>'' and CB.MobileNumber = @MobileNumber)
    OR (@MobileNumber='' and CB.MobileNumber is null )
    OR (@MobileNumber='' and CB.MobileNumber is not null )
    OR (@MobileNumber IS NULL AND CB.MobileNumber  is not null)
   )
 )
 SELECT count(CustomerName) FROM PagedResults;
 
END

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值