– 只做分页
–1.第一种
create proc MyPaging1
(
@pageIndex int,
@pageSize int,
@totalRecordCount int output
)asbeginselect top (@pageSize)*from GoodsInfo whereidnotin(select top (@pageSize *(@PageIndex-1)) Id from GoodsInfo)select @totalPageCount =Count(*)from GoodsInfo
end
go
–第2种(只做分页)
create proc MyPaging2
(
@pageIndex int,
@pageSize int,
@totalPageCount int output
)asbegin
–select top 5*from GoodsInfo whereidnotin(select top 10 Id from GoodsInfo)
declare @s int,@e intset @s = @pageIndex *(@pageSize -1)+1set @e = @pageIndex * @pageSize
select*from(selectROW_NUMBER() over (order by Id)asNO,*from GoodsInfo) T whereT.NO between @s and @e
declare @a decimalselect @a =Count(*)from GoodsInfo
set @totalPageCount =CEILING( @a / @pageSize )
end
go
select*from GoodsInfo
declare @count int
exec MyPaging1 1,5,@count output
select @count
go
– 带固定查询条件的分页
create proc MyPaging3
(
@pageIndex int,
@pageSize int,@Namevarchar(50),
@price decimal,
@totalRecordCount int output
)asbeginselect*into #Temp from GoodsInfo whereName like (’%’+@Name+’%’)and Price > @price
select top (@pageSize)*from #Temp whereidnotin(select top (@pageSize *(@PageIndex-1)) Id from #Temp)select @totalRecordCount =Count(*)from #Temp
end
– 多个条件组合的分页(多条件查询时,不建议使用存储过程)
go
create proc MyPaging3
(
@pageIndex int,
@pageSize int,@Namevarchar(50),
@price decimal,
@totalRecordCount int output
)asbeginiflen(@Name)=0and @price =0
begin
select*into #Temp1 from GoodsInfo
select top (@pageSize)*from #Temp1 whereidnotin(select top (@pageSize *(@PageIndex-1)) Id from #Temp1)select @totalRecordCount =Count()from #Temp1
end
elseiflen(@Name)=0and @price>0
begin
select*into #Temp2 from GoodsInfo whereprice>@price
select top (@pageSize)*from #Temp2 whereidnotin(select top (@pageSize *(@PageIndex-1)) Id from #Temp2)select @totalRecordCount =Count()from #Temp2
end
elseiflen(@Name)>0and @price=0
begin
select*into #Temp3 from GoodsInfo whereName like (’%’+@Name+’%’)select top (@pageSize)*from #Temp3 whereidnotin(select top (@pageSize *(@PageIndex-1)) Id from #Temp3)select @totalRecordCount =Count()from #Temp3
end
else
begin
select*into #Temp4 from GoodsInfo whereName like (’%’+@Name+’%’)and Price>@price
select top (@pageSize)*from #Temp4 whereidnotin(select top (@pageSize *(@PageIndex-1)) Id from #Temp4)select @totalRecordCount =Count()from #Temp4
end
end
declare @a int
exec MyPaging3 1,3,‘苹’,15,@a output
select @a
--------------------------------------------------
```csharp
--更新sql分页写法
Create Database MySchoolDB1220
go
use MySchoolDB1220
go
--年级表
create table Grade
(
Id int primary key identity,Namevarchar(50))--班级表
create table Category
(
Id int primary key identity,Namevarchar(50),
GId int foreign key referencesGrade(Id)-- 年级ID
)--学生表
create table Student
(
Id int primary key identity,Namevarchar(50),Sexvarchar(50),Telvarchar(50),Photovarchar(50),JoinTimevarchar(50),--入学时间
OutTimevarchar(50),--毕业时间
CId int foreign key referencesCategory(Id)--班级ID
)
go
insert intoGrade values ('S1'),('S2'),('S3')
insert intoCategory values ('S1101',1),('S1102',1),('S2201',2),('S2202',2),('S3301',3),('S3302',3)---------------------------------------------------------------------------------------------------------
go
--存储过程 -带固定条件的分页查询
create proc GetStudent_1
(
@PageIndex int,
@PageSize int,
@Cid int,
@PageCount int output
)asbegin
declare @s int=(@PageIndex -1)*@PageSize+1
declare @e int= @PageIndex * @PageSize
select @PageCount =Count(*)from Student whereCId=@Cid
select*from(selectROW_NUMBER() over (Order by C.Id)asNo,
S.*,
C.Name asCNamefrom Student S left join Category C
on C.Id = S.CId
whereC.Id = @Cid
) T
whereT.No between @s and @e
end
go
-------------------------------------------------------------------------------组合条件查询 无分页
create proc GetStudent_2
(
@Cid int,@Namevarchar(50),@StartTimevarchar(50),@EndTimevarchar(50))asbegin
declare @sqlnvarchar(2000)= N'select S.*,C.Name asCNamefrom Student S left join Category C on S.Cid=C.Id where1=1 ';if(@Cid>0)
begin
set @sql = @sql + N' and S.Cid = '+@Cid
end
if(len(@Name)>0)
begin
set @sql = @sql + N' and S.Name like '+@Name
end
if(len(@StartTime)>0)
begin
set @sql = @sql + N' and JoinTime > '''+@StartTime+''''
end
if(len(@EndTime)>0)
begin
set @sql = @sql + N' and JoinTime < '''+@EndTime+''''
end
exec sp_executesql @sql
end
--------------------------------------------------------------------------------
go
--------------------------------------------------------------------------------
create proc GetStudent_3
(
@PageIndex int,
@PageSize int,
@Cid int,@Namevarchar(50),@StartTimevarchar(50),@EndTimevarchar(50),
@PageCount int output
)asbegin
declare @s int=(@PageIndex -1)*@PageSize+1
declare @e int= @PageIndex * @PageSize
declare @SqlCountnvarchar(2000)
declare @sqlPagenvarchar(2000)
declare @sqlnvarchar(2000)= N'select S.*,C.Name asCNamefrom Student S left join Category C on S.Cid=C.Id where1=1 ';if(@Cid>0)
begin
set @sql = @sql + N' and S.Cid = '+@Cid
end
if(len(@Name)>0)
begin
set @sql = @sql + N' and S.Name like '+@Name
end
if(len(@StartTime)>0)
begin
set @sql = @sql + N' and JoinTime > '''+@StartTime+''''
end
if(len(@EndTime)>0)
begin
set @sql = @sql + N' and JoinTime < '''+@EndTime+''''
end
set @sql ='('+@sql+') T'
set @SqlCount= N'Select @PageCount =Count(*)from '+@sql
exec sp_executesql @SqlCount,N'@PageCount intout',@PageCount outset @sqlPage = N'select*from(selectROW_NUMBER() over (Order by T.Id)asNo,*from '+@sql+') TN whereTN.No between '+convert(varchar(5),@s)+' and '+convert(varchar(5),@e)+')'
exec sp_executesql @sqlPage
end
go
– 只做分页–1.第一种create proc MyPaging1(@pageIndex int,@pageSize int,@totalRecordCount int output)asbeginselect top (@pageSize) * from GoodsInfo where id not in (select top (@pageSize * (@PageInde...