預存程序實現分頁,預存程序分頁

來源:互聯網
上載者:User

預存程序實現分頁,預存程序分頁

USE [HDIS]
GO
/****** Object:  StoredProcedure [dbo].[AspNetPager]    Script Date: 12/30/2014 09:00:35 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
Create procedure [dbo].[AspNetPager]
(@tablename nvarchar (1000), --表名
@filedname nvarchar (4000), --查詢的欄位
@startIndex int, --起始記錄數
@endIndex int, --結束記錄數
@where  nvarchar (4000), --條件 (不包含where)
@orderfiled nvarchar (100), --排序欄位 (CreateDate desc)
@PageSize int,
@prmkeyName nvarchar (100),
@pageIndex int,
@docount bit)
as
begin
declare @date varchar(50),@sql nvarchar (4000) ,@i int

select @date =CONVERT(nvarchar(50), serverproperty('productversion'))
--if(CONVERT(int, SUBSTRING(@date,0,3))>8)  ------sql2000以上
-- begin
--  if(@docount=1)
--   set @sql = 'select count(*) from ' + @tablename +' where ' + @where 
--  else
--   begin
--    set @sql ='
--    with temptbl as (
--    SELECT ROW_NUMBER() OVER (ORDER BY '+ @orderfiled +' )AS Row, * from '+ @tablename +' where '+ @where +')
--    SELECT '+ @filedname  +' FROM temptbl where Row between '+CONVERT(nvarchar(100),@startIndex) +' and '+CONVERT(nvarchar(100),@endIndex )
--   END
--  exec (@sql)
-- end
--else
 begin   -------sql2000
  if(@docount=1)
   set @sql = 'select count(*) from ' + @tablename +' where ' + @where 
  else
   begin
   
    set @i= CONVERT(nvarchar(100),@PageSize)*(CONVERT(nvarchar(100),@pageIndex)-1)
    set @sql = 'SELECT TOP '+ CONVERT(nvarchar(100),@PageSize) +' *
    FROM ' + @tablename +' WHERE ('+@where +' and'+@prmkeyName+' NOT IN
    (SELECT TOP '+CONVERT(nvarchar(100),@i)+' ' +@prmkeyName +'
     FROM ' + @tablename +' WHERE ' + @where+' ORDER BY '+ @orderfiled +')) ORDER BY '+ @orderfiled

   end
   
  --print(@sql)
 exec (@sql)
end
end

相關文章

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.