写了个通作的分页存储过程,top,加入了排序

USE [WebDB_TradeOrder]
GO
/****** Object:  StoredProcedure [dbo].[Boss_Proc_PagingWithOrder]    Script Date: 08/28/2013 09:55:38 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:  <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
ALTER PROCEDURE [dbo].[Boss_Proc_PagingWithOrder]
 @pageSize int,
 @pageIndex int,
 @tableName varchar(200),
 @sqlWhere varchar(2000),
 @totalCount int output,
 @orderby varchar(100)
AS
BEGIN
 SET NOCOUNT ON
 
 declare @countSql nvarchar(4000);
 declare @sql nvarchar(4000);
 declare @tempCount int;
 
 set @tempCount = @pageSize * (@pageIndex - 1);
    if (@SqlWhere ='''' or @SqlWhere='' or @sqlWhere is NULL)
  begin
    set @countSql= 'select @totalCount = COUNT(ID) from '+@tableName;
    set @sql = 'select * from (select top '+str(@pageSize)+' * from '+@tableName+' where ID not in
    (select top '+str(@tempCount)+' ID from '+@tableName+@orderby+')) as a'
  end
    else
  begin
      set @countSql= 'select @totalCount = COUNT(ID) from '+@tableName+' where '+@sqlWhere+'';
   set @sql = 'select top '+str(@pageSize)+' * from '+@tableName+' where ID not in
    (select top '+str(@tempCount)+' ID from '+@tableName+' where '+@sqlWhere+@orderby+') and '+@sqlWhere+@orderby+'';
  end
 EXEC sp_executesql @countSql,N'@totalCount int OUTPUT',@TotalCount OUTPUT
 print @TotalCount
 print @sql
 EXEC  (@sql)
END

posted @ 2013-08-28 15:47  cclient  阅读(214)  评论(0编辑  收藏  举报