java中使用游标色步骤_游标使用参考语句实例解析

游标是从表中检索出结果集,从中每次指向一条记录进行交互的机制。

作用

指定结果集中特定行的位置。

基于当前的结果集位置检索一行或连续的几行。

在结果集的当前位置修改行中的数据。

对其他用户所做的数据更改定义不同的敏感性级别。

可以以编程的方式访问数据库。

一个简单实用:

1

2

3

4

5

6

7

8

9

10

11

12

13

Declare

-- 声明游标

Cursor Mycur Is

Select * From Emp;

Empinfo Emp%Rowtype;

Cou Number;

Begin

-- 游标操作使用循环,但是在操作之前必须先将游标打开

For Empinfo In Mycur Loop

Cou := Mycur%Rowcount;

Dbms_Output.Put_Line('行号:' || Cou || ' 雇员编号:' || Empinfo.Empno || ' 雇员姓名:' || Empinfo.Ename);

End Loop;

End;

循环取出数据的两种写法:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

Declare

-- 声明游标

Cursor Mycur Is

Select * From Emp; -- List (EmpPo)

Empinfo Emp%Rowtype;

Cou Number;

Begin

-- 游标操作使用循环,但是在操作之前必须先将游标打开

If Mycur%Isopen Then

Null;

Else

Open Mycur;

End If;

-- 使游标向下一行

Fetch Mycur

Into Empinfo;

-- 判断此行是否有数据被发现

While (Mycur%Found) Loop

Cou := Mycur%Rowcount;

Dbms_Output.Put_Line('行号:' || Cou || ' 雇员编号:' || Empinfo.Empno || ' 雇员姓名:' || Empinfo.Ename);

-- 修改游标,继续向下

Fetch Mycur

Into Empinfo;

End Loop;

End;

第二种写法:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

Declare

-- 声明游标

Cursor Mycur Is

Select * From Emp;

Empinfo Emp%Rowtype;

Cou Number;

Begin

-- 游标操作使用循环,但是在操作之前必须先将游标打开

If Mycur%Isopen Then

Null;

Else

Open Mycur;

End If;

Loop

-- 使游标向下一行

Fetch Mycur

Into Empinfo;

Exit When Mycur%Notfound;

Cou := Mycur%Rowcount;

Dbms_Output.Put_Line('行号:' || Cou || ' 雇员编号:' || Empinfo.Empno || ' 雇员姓名:' || Empinfo.Ename);

End Loop;

End;

在存储过程中使用游标

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

Create Or Replace Procedure Myproc(Oi_Return Out Integer) Is

Cursor Mycur Is

Select * From Emp_0915;

Empinfo Emp_0915%Rowtype;

Cou Number;

Exc_Return Exception; -- 程序中间返回自定义异常

Begin

If Mycur%Isopen Then

Null;

Else

Open Mycur;

End If;

Loop

Fetch Mycur

Into Empinfo;

Exit When Mycur%Notfound;

Cou := Mycur%Rowcount;

Dbms_Output.Put_Line(Cou || '开始更新...');

Update Emp_0915 t Set t.Sal = t.Sal + 1 Where t.Empno = Empinfo.Empno;

Dbms_Output.Put_Line(Cou || '更新结束...');

End Loop;

Commit;

Oi_Return := 1;

Exception

When Exc_Return Then

Rollback;

Oi_Return := 0;

End;

在oracle中测试:

1

2

3

4

5

6

7

8

9

10

Declare

Re Integer;

Begin

Myproc(Re);

If Re = 1 Then

Dbms_Output.Put_Line(Re || ':执行结束。。。');

Else

Dbms_Output.Put_Line(Re || ':执行错误___');

End If;

End;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值