1 create proc GetItems 2 ( 3 @id int 4 ,@cid int 5 ,@PageIndex int = 0 6 ,@PageSize int = 20 7 ,@TotalRecords int output 8 ) 9 as 10 begin 11 declare @ResultTable table 12 ( 13 RowIndex int 14 ,[id] int 15 ,[title] varchar(25) not null --标题 16 ,[Cutoff] datetime not null --截止时间 17 ,[url] varchar(500) not null --连接地址 18 ,[imgfile] varchar(500) not null --图片连接 19 ,[Oriprice] numeric(8,2) not null --原价 20 ,[price] numeric(8,2) not null --当前价格 21 ,[cid] int not null --类型 22 ); 23 24 Insert into @ResultTable 25 select row_number() over (order by id desc) as RowIndex, 26 [id] 27 ,[title] 28 ,[Cutoff] 29 ,[url] 30 ,[imgfile] 31 ,[Oriprice] 32 ,[price] 33 ,[cid] 34 from [Items] 35 where id=( 36 case when @id is not null or @id<=0 37 then @id 38 else 39 [id] 40 end 41 ) 42 and cid=( 43 case when @cid is not null or @cid<=0 44 then @cid 45 else 46 [cid] 47 end 48 ) 49 50 select @TotalRecords = count(*) from @ResultTable; 51 52 select * 53 from @ResultTable 54 where RowIndex > (@PageSize * (@PageIndex-1)) 55 and RowIndex <= (@PageSize * (@PageIndex)); 56 end