diff --git "a/45\346\235\216\346\266\233/\344\275\234\344\270\232/220920\346\270\270\346\240\207\344\275\234\344\270\232.md" "b/45\346\235\216\346\266\233/\344\275\234\344\270\232/220920\346\270\270\346\240\207\344\275\234\344\270\232.md" new file mode 100644 index 0000000000000000000000000000000000000000..0cd174890608abb9fe50fecc0880467433a17638 --- /dev/null +++ "b/45\346\235\216\346\266\233/\344\275\234\344\270\232/220920\346\270\270\346\240\207\344\275\234\344\270\232.md" @@ -0,0 +1,28 @@ +```sql +--1 创建学生游标,该游标包含(学生姓名,兴趣爱好,生源地,荣誉总数) +declare mycursor cursor for(select name,hobby,ori_loca,prize from tb_inf_student as s1 join tb_student as s2 on s1.stu_num=s2.stu_num) +-- 2循环遍历161开头的学生信息 +declare mycursor cursor scroll for(select s1.stu_num,name,hobby,ori_loca,prize from tb_inf_student as s1 join tb_student as s2 on s1.stu_num=s2.stu_num where s2.stu_num like '161%') +open mycursor +declare @stuid int,@name varchar(20),@hobby varchar(20),@ori varchar(20),@price int +fetch first from mycursor +while @@FETCH_STATUS=0 + begin + print convert(varchar(20),@stuid)+','+@name+','+@hobby+','+@ori+','+cast(@price as varchar(20)) + fetch next from mycursor + end +close mycursor +-- 3使用游标统计生源地为北京的荣誉总数 +declare mycursor cursor scroll for(select prize from tb_inf_student where ori_loca = '北京') +open mycursor +declare @sum int = 0,@temp int +fetch first from mycursor into @temp + +while @@FETCH_STATUS=0 + begin + set @sum += @temp + fetch next from mycursor into @temp + end + print @sum +close mycursor +``` \ No newline at end of file diff --git "a/45\346\235\216\346\266\233/\347\254\224\350\256\260/\350\247\206\345\233\276.md" "b/45\346\235\216\346\266\233/\347\254\224\350\256\260/220918\350\247\206\345\233\276.md" similarity index 100% rename from "45\346\235\216\346\266\233/\347\254\224\350\256\260/\350\247\206\345\233\276.md" rename to "45\346\235\216\346\266\233/\347\254\224\350\256\260/220918\350\247\206\345\233\276.md" diff --git "a/45\346\235\216\346\266\233/\347\254\224\350\256\260/220920\346\270\270\346\240\207.md" "b/45\346\235\216\346\266\233/\347\254\224\350\256\260/220920\346\270\270\346\240\207.md" new file mode 100644 index 0000000000000000000000000000000000000000..cd23f03bb8a5e8567b341c478c566fcb3d50d6aa --- /dev/null +++ "b/45\346\235\216\346\266\233/\347\254\224\350\256\260/220920\346\270\270\346\240\207.md" @@ -0,0 +1,68 @@ +### 游标 + + + +tip:在性能上,游标会迟更多的内存,减少可用的并发,占用带宽,锁定资源,当然还有更多的代码量。 + + + +游标作为一个备用方式,当使用while、子查询,临时表,表变量,自建函数或其他方式仍然无法实现某些查询的时候,可以使用游标实现。 + +```sql +declare cursor_name cursor scorll(回滚)|forward_only(默认,无回滚) for (select 语句) +open cursor——name --(打开游标) +declare @xxx --(定义变量)... +fetch first from cursor_name --(查找第一条游标) +fetch last from cursor_name--(查找最后一条游标) +fetch prior from cursor_name--(查找上一行) +fetch absolute n cursor_name--(查找第n行) +fetch relative n from cursor_name--(查找相对于目前来说第N行) +fetch next from cursor_name--(游标下移) +通常搭配 which 进行遍历 +close cursor——name--(关闭游标) + + +--对于无回滚 scorll 的游标 只能用 next(查找下一行) +``` + +全局游标 + +```sql +declare test_cursor cursor global for +select * from table_name +``` + +局部游标 + +```sql +declare test_cursor cursor local for +select * from table_name +``` + +删除游标 + +```sql +deallocate test_cursor +``` + + + +###### read_only:意味着声明的游标只能读取数据,游标不能做任何更新操作 + +###### scroll_locks:是另一种极端,将读入游标的所有数据进行锁定,防止其他程序进行更改,以确保更新的绝对成功。 + +###### optimistic:相对比较好的一个选择,OPTIMISTIC不锁定任何数据,当需要在游标中更新数据时,如果底层表数据更新,则游标内数据更新不成功,如果,底层表数据未更新,则游标内表数据可以更新。 + + + +##### 摘抄:对于游标的优化 + +- 如果能不用游标,尽量不要使用游标 +- 用完用完之后一定要关闭和释放 +- 尽量不要在大量数据上定义游标 +- 尽量不要使用游标上更新数据 +- 尽量不要使用insensitive, static和keyset这些参数定义游标 +- 如果可以,尽量使用FAST_FORWARD关键字定义游标 +- 如果只对数据进行读取,当读取时只用到FETCH NEXT选项,则最好使用FORWARD_ONLY参数 + +  到生命周期来谈游标。游标是非常邪恶的一种存在,使用游标经常会比使用面向集合的方法慢2-3倍,当游标定义在大数据量时,这个比例还会增加。如果可能,尽量使用while,子查询,临时表,函数,表变量等来替代游标,记住,游标永远只是你最后无奈之下的选择,而不是首选。 \ No newline at end of file