第三阶段数据库-9:循环,编号,游标,分页
1_sql中的循环,编号
(1)sql 中没有for循环,只有while循环,
begin end 中间的就是while执行的语句,相当于{}
declare @i int;
set @i=1;
--begin end 中间的就是while执行的语句,相当于{}
while(@i<=100)
begin
declare @rand int;
--rand(): 产生0-1之间的随机小数,不包括0和1
--产生x-y之间的随机数 rand() * (y-x)+x;
--round()四舍五入取整
set @rand=rand()*(100-60)+60
print @rand
--sqlserver 中的字符串和数字相加,先把字符串转换成数字再和数字计算
insert into StudentInfo(StuName,Score)values('张三'+cast(@i as varchar),@rand);
set @i+=1;
end
(2)重新编号
row_number对查到的数据进行重新编号,赋予他来连续的编号
over 编号的依据
select *, row_number() over(order by StuName) as '重新编号' from StudentInfo
select ProvinceName+City+Area, row_number() over(order by AddressId) as '重新编号' from AddressInfo
2_游标
(1)游标,是一种数据访问机制,用于逐行处理查询的结果,允许开发者或应用程序在结果集上定义 检索 和修改数据
(2)创建游标并使用,
declare @StuId int;
declare @StuName varchar(20);
declare @Score1 int;
--1.定义游标
declare My_Cursor cursor for
select * from StudentInfo where StuId>=130;
--2.打开游标
open My_Cursor;
--3.让游标指向结果集的第一行,并拉取第一行的数据,存储到变量上
fetch next from My_Cursor into @StuId,@StuName, @Score1;
--4.循环游标
--一个@是开发者自定义的变量
--两个@开头是sql server自定义的变量名称
--@@fetch_status作用 sql中游标指针的位置判断,代表游标读取下一行数据是否成功
--理解它为枚举,有三种值:0代表读取成功,-1表示语句失败或此行不在结果集中 -2表示被提取的行不存在
while @@fetch_status=0
begin
print @StuId;
print @StuName;
print @Score1;
print '==============';
fetch next from My_Cursor into @StuId,@StuName, @Score1;
end
--5.关闭游标
close My_Cursor;
--6.释放游标
deallocate My_Cursor
3_临时表
(1)临时表,临时存储的表,不是真实的表,类似于C#中的匿名函数
(2)判断临时表是否存在,如果存在则删除
if object_id('tempdb..#TempTable') is not null
drop table #TempTable --删除临时表的结果 表结构也删除
--delete from #TempTable --删除临时表的结果 表结构不删除
(2)创建临时表,命名必须以#开头
--创建临时表 命名必须以#开头
create table #TempTable (Score int not null,ScoreCount int null
);
(3)向表中插入数据
循环向临时表插入数据 (不推荐
直接使用select 语句配合 insert into 语句(推荐)
select * from #TempTable
--向临时表中插入数据
--1.循环向临时表插入数据(不推荐)
declare @Score int;
declare @ScoreCount int;
-- a.定义游标,是一种数据访问机制,用于逐行处理查询的结果,允许开发者或应用程序在结果集上定义 检索 和修改数据
declare Score_cursor cursor for
select Score ,Count(Score) as ScoreCount from StudentInfo
where Score>=90 and Score<=100
group by Score
--b.打开一个游标
open Score_cursor;
--c.让游标指向结果集第一行,并且拉取第一行数据并存储到相应的变量上
--fetch 拿来 next 下一个
fetch next from Score_cursor into @Score,@ScoreCount;
--d循环游标
while @@FETCH_STATUS=0
begin
insert into #TempTable(Score,ScoreCount)values(@Score,@ScoreCount);
fetch next from Score_cursor into @Score,@ScoreCount;
end
--e.关闭游标
close Score_cursor;
--f.释放游标
deallocate Score_cursor
select * from #TempTable
where ScoreCount>=4
order by ScoreCount desc;
--2.直接使用select语句配合insert Into语句(推荐)
--select 字句查询的结果要和 insert into 语句的列个数 顺序 类型 一一对应
insert into #TempTable
select Score ,Count(Score) as ScoreCount from StudentInfo--查询
where Score>=90 and Score<=100 --筛选
group by Score
select * from #TempTable
4_分页
(1)row_number over 方式 sql 2012 以下版本推荐使用
--分页
--100 条数据 10页 一页显示10条,
--当前页currentPage=1 设置每页显示的条数 pageSize=10
--第一页 rowId>=10 and RoeId<=10 1-10
--第二页 rowId>=11 and RoeId<=20 11-20
--第三页 rowId>=21 and RoeId<=30 21-30\
--每页开始的RowId : (currentPage-1)*pageSize+1
--每页结束的RoeId:currentPage*pageSize
--1.row_number over 是方式 sql 2012 以下版本推荐使用
--当前页,可以声明的时候赋值,也可以先声明再赋值
--declare @currentPage int=10;
declare @currentPage int;
declare @pageSize int;--每页显示的条数,10,20,30...
set @currentPage=1;
set @pageSize=10;
--嵌套查询
--外层查询select * from (--内层查询,子查询-- row_number() over(order by Score asc)按照成绩升序重新编号--返回一个新表select row_number() over(order by Score asc) as RowId ,* from StudentInfo) as S --利用新表进行筛选where RowId between ( @currentPage-1)*@pageSize+1 and @currentPage * @pageSize
(2)offset fetch next 方式 sql 2012版本以上推荐;
offset()偏移 ;fetch拿取
declare @currentPage1 int;
declare @pageSize1 int;
set @currentPage1=1;
set @pageSize1=10;
select * from StudentInfo --查询
order by StuId --根据id排序
offset (@currentPage1-1)*@pageSize1 rows --offset 偏移,跳过多少行
fetch next @pageSize1 rows only --读取接下来的多少行数据