From 40ddccbf7f5c36bbcd959531660680631c7c8907 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BD=95=E7=9D=BF?= <2476310189@qq.com> Date: Wed, 21 Sep 2022 03:01:44 +0000 Subject: [PATCH 1/4] =?UTF-8?q?=E6=96=B0=E5=BB=BA=202022-9-20=E6=B8=B8?= =?UTF-8?q?=E6=A0=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../2022-9-20\346\270\270\346\240\207/.keep" | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 "06\344\275\225\347\235\277/\347\254\224\350\256\260/2022-9-20\346\270\270\346\240\207/.keep" diff --git "a/06\344\275\225\347\235\277/\347\254\224\350\256\260/2022-9-20\346\270\270\346\240\207/.keep" "b/06\344\275\225\347\235\277/\347\254\224\350\256\260/2022-9-20\346\270\270\346\240\207/.keep" new file mode 100644 index 0000000..e69de29 -- Gitee From 693ba1e12d41004e366b23f6957cd82fdd86dd5a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BD=95=E7=9D=BF?= <2476310189@qq.com> Date: Wed, 21 Sep 2022 03:02:10 +0000 Subject: [PATCH 2/4] =?UTF-8?q?=E7=AC=94=E8=AE=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 何睿 <2476310189@qq.com> --- .../2022-9-20\347\254\224\350\256\260.md" | 73 +++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 "06\344\275\225\347\235\277/\347\254\224\350\256\260/2022-9-20\346\270\270\346\240\207/2022-9-20\347\254\224\350\256\260.md" diff --git "a/06\344\275\225\347\235\277/\347\254\224\350\256\260/2022-9-20\346\270\270\346\240\207/2022-9-20\347\254\224\350\256\260.md" "b/06\344\275\225\347\235\277/\347\254\224\350\256\260/2022-9-20\346\270\270\346\240\207/2022-9-20\347\254\224\350\256\260.md" new file mode 100644 index 0000000..b6417ae --- /dev/null +++ "b/06\344\275\225\347\235\277/\347\254\224\350\256\260/2022-9-20\346\270\270\346\240\207/2022-9-20\347\254\224\350\256\260.md" @@ -0,0 +1,73 @@ +### 一、游标 + +#### 1,分类 + +- **静态游标(Static)**:在操作游标的时候,数据发生变化,游标中数据不变 +- **动态游标(Dynamic)**:在操作游标的时候,数据发生变化,游标中数据改变,默认值。 +- **键集驱动游标(KeySet)**:在操作游标的时候,被标识的列发生改变,游标中数据改变,其他列改变,游标中数据不变。 + +#### 2、声明游标 + +```sql +declare 游标名 cursor scroll for (查询语句) +``` + +##### (1)**scroll** + +指定游标使用的读取选项,默认值为NEXT,如果不使用该关键字,那么读取游标只能进行NEXT操作,如果使用该关键字,那么游标向任何方向,或者任何位置移动,进行next、last、first、prior、relative、absulute 操作。 + +#### 3、打开游标(open) + +```sql +open 游标名 +``` + +#### 4、关闭游标(close) + +```sql +close 游标名 +``` + +#### 5、读取游标 + +``` +fetch first/next/last from 游标名 into () +``` + +| 名称 | 含义 | +| ------------------ | ------------------------------------------------------------ | +| **==first==** | 读取游标中的第一行 | +| **==last==** | 读取游标的最后一行 | +| **==next==** | 读取游标当前行下一行数据 | +| **==prior==** | 读取当前游标上一行数据 | +| **==relative n==** | 读取游标当前行之前或之后第n行数据(n为正则向前,反之则向后) | +| **==absulute n==** | 读取游标第n行数据(n为负从最后一行开始,反之则从第一行开始) | + +#### 6、删除游标 + +``` +deallocate 游标名 +``` + +#### 7、游标系统变量与函数 + +- **@@CURSOR_NUM**返回最后打开的游标中满足条件的元组数。 + +- **@@FETCH_STATUS** 返回上次执行FETCH命令的状态。 + +| 返回值 | 说明 | +| ------ | --------------------------------- | +| 0 | fetch语句成功 | +| -1 | fetch语句失败或此元组不在结果集中 | +| -2 | 被读取的元组不存在 | + +#### 8.游标指向某行多列数据 + +```sql +declare 游标名 cursor [scroll] for (select 列1,列2,列3,列4 from 表名) + +把数据赋给变量 + +fetch first from 游标名 into 变量1,变量2,变量3,变量4 +``` + -- Gitee From 3ac3f63ada62f4d57666549bd947e3d3ad1a8654 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BD=95=E7=9D=BF?= <2476310189@qq.com> Date: Wed, 21 Sep 2022 04:13:38 +0000 Subject: [PATCH 3/4] =?UTF-8?q?=E6=96=B0=E5=BB=BA=202022-9-20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../\344\275\234\344\270\232/2022-9-20/.keep" | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 "06\344\275\225\347\235\277/\344\275\234\344\270\232/2022-9-20/.keep" diff --git "a/06\344\275\225\347\235\277/\344\275\234\344\270\232/2022-9-20/.keep" "b/06\344\275\225\347\235\277/\344\275\234\344\270\232/2022-9-20/.keep" new file mode 100644 index 0000000..e69de29 -- Gitee From b2f9fd18e005b3063acd12bc7a995c88eb8b0c30 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BD=95=E7=9D=BF?= <2476310189@qq.com> Date: Wed, 21 Sep 2022 04:14:12 +0000 Subject: [PATCH 4/4] =?UTF-8?q?=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 何睿 <2476310189@qq.com> --- .../2022-9-20\344\275\234\344\270\232.md" | 86 +++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 "06\344\275\225\347\235\277/\344\275\234\344\270\232/2022-9-20/2022-9-20\344\275\234\344\270\232.md" diff --git "a/06\344\275\225\347\235\277/\344\275\234\344\270\232/2022-9-20/2022-9-20\344\275\234\344\270\232.md" "b/06\344\275\225\347\235\277/\344\275\234\344\270\232/2022-9-20/2022-9-20\344\275\234\344\270\232.md" new file mode 100644 index 0000000..d324656 --- /dev/null +++ "b/06\344\275\225\347\235\277/\344\275\234\344\270\232/2022-9-20/2022-9-20\344\275\234\344\270\232.md" @@ -0,0 +1,86 @@ +--- 创建学生游标,该游标包含(学生姓名,兴趣爱好,生源地,荣誉总数) + +```sql +declare bb cursor scroll for (select name,hobby,ori_loca,prize from tb_student join tb_inf_student on tb_student.stu_num=tb_inf_student.stu_num) +open cc +``` + +--- 循环遍历161开头的学生信息 + +```sql +declare cc cursor scroll for (select * from tb_student ) +declare @a int ,@b varchar(20) ,@c int ,@d varchar(20),@e varchar(20),@f varchar(20) +fetch first from cc into @a ,@b ,@c ,@d ,@e,@f +while @@FETCH_STATUS=0 +begin +if @a like'161%' +begin + print convert(varchar(20),@a) +','+ @b +','+ convert(varchar(20),@c) +','+@d +','+@e+','+@f + fetch next from cc into @a ,@b ,@c ,@d ,@e,@f + end + else + begin + fetch next from cc into @a ,@b ,@c ,@d ,@e,@f + end +end +close cc +deallocate ee +``` + +--- 使用游标统计生源地为北京的荣誉总数 + +```sql +open dd +declare dd cursor scroll for (select ori_loca,prize from tb_inf_student) +declare @a1 varchar(20) +declare @a2 varchar(20) +declare @b1 int =0 +fetch first from dd into @a1,@a2 +while @@FETCH_STATUS=0 +begin +if @a1='北京' +begin + set @b1+=@a2 + fetch next from dd into @a1 ,@a2 + end + else + begin + fetch next from dd into @a1 ,@a2 + end +end +print @b1 +``` + +--- 合理使用游标和事务,让5-1号前借书的学生将图书归还 + +```sql +deallocate xs3 +declare xs3 cursor scroll for (select stu_num,return_time from tb_record where borrow_time<'2019-05-01') +open xs3 +begin transaction +declare @a char(8) +declare @b date +fetch first from xs3 into @a,@b +while @@FETCH_STATUS=0 + begin + if @b is null + begin + update tb_record set return_time = getdate() where stu_num = @a + end + fetch next from xs3 into @a,@b + end +--select * from tb_record +--rollback transaction +declare @err int=0 +set @err +=@@error +if @err>0 + begin + print '失败' + rollback transaction + end +else + begin + select stu_num,return_time from tb_record + end +``` + -- Gitee