12-8 游标的基本使用
2016-03-15 21:28
260 查看
游标(重点)
1.定义游标(declear)->打开游标-》使用游标-》关闭游标
declare
cursor c is select * from emp;
--结果集可以是任意查询语句的结果
--定义一个可以接收使用C中值的变量
v_emp c%rowtype;
begin
--打开游标
open c;
--使用
fetch c into v_emp;
v_emp.sal:=v_emp.sal+200;
dbms_output.put_line('工资是:'||v_emp.sal||'是:'||v_emp.ename);
--关闭
close c;
end;
2.遍历游标
使用游标遍历输出显示10部门所有员工的姓名
declare
cursor c is select ename from emp where
deptno=10;
v_ename c%rowtype;
begin
open c;
loop
fetch c into
v_ename;
exit when(c%notfound);
--没取到记录时退出
dbms_output.put_line('员工姓名是:'||v_ename.ename);
--注意以上3句的顺序
--先取再判断,若取到就显示,若没取到就推出
end loop;
close c;
end;
改写为while遍历
declare
cursor c is select ename from emp where
deptno=10;
v_ename c%rowtype;
begin
open c;
fetch c into v_ename;
--先取一条
while(c%found) loop
--判断是否取到
dbms_output.put_line('员工姓名是:'||v_ename.ename);
--先显示再取
fetch c into
v_ename;
end loop;
close c;
end;
使用for loop遍历游标
declare
cursor c is select ename from emp where
deptno=10;
--v_ename c%rowtype;
begin
--open c;
for v_ename in c loop
--自己声明一个接收c值的变量
--自己open c fetch c into
ename
--自动判断是否取到
--结束,自动close c
dbms_output.put_line('员工姓名是:'||v_ename.ename);
--fetch c into
v_ename;
end loop;
--close c;
end;
推荐使用for:
declare
cursor c is select ename from emp where
deptno=10;
begin
for v_ename in c loop
dbms_output.put_line('员工姓名是:'||v_ename.ename);
end loop;
end;
根据用户输入的部门编号输出符合条件的员工姓名极其工资
declare
cursor c is select ename,sal from emp where
deptno=&部门编号,;
begin
for v_emp in c loop
dbms_output.put_line('员工姓名是:'||v_emp.ename||'工资为:'||v_emp.sal);
end loop;
end;
根据用户输入的部门编号及其工作输出符合条件的员工姓名极其工资
declare
cursor c is select ename,sal from emp where
deptno=&部门编号 and job=upper('&工作');
--工作是字符串型,所以要加''
begin
for v_emp in c loop
dbms_output.put_line('员工姓名是:'||v_emp.ename||'工资为:'||v_emp.sal);
end loop;
end;
1.定义游标(declear)->打开游标-》使用游标-》关闭游标
declare
cursor c is select * from emp;
--结果集可以是任意查询语句的结果
--定义一个可以接收使用C中值的变量
v_emp c%rowtype;
begin
--打开游标
open c;
--使用
fetch c into v_emp;
v_emp.sal:=v_emp.sal+200;
dbms_output.put_line('工资是:'||v_emp.sal||'是:'||v_emp.ename);
--关闭
close c;
end;
2.遍历游标
使用游标遍历输出显示10部门所有员工的姓名
declare
cursor c is select ename from emp where
deptno=10;
v_ename c%rowtype;
begin
open c;
loop
fetch c into
v_ename;
exit when(c%notfound);
--没取到记录时退出
dbms_output.put_line('员工姓名是:'||v_ename.ename);
--注意以上3句的顺序
--先取再判断,若取到就显示,若没取到就推出
end loop;
close c;
end;
改写为while遍历
declare
cursor c is select ename from emp where
deptno=10;
v_ename c%rowtype;
begin
open c;
fetch c into v_ename;
--先取一条
while(c%found) loop
--判断是否取到
dbms_output.put_line('员工姓名是:'||v_ename.ename);
--先显示再取
fetch c into
v_ename;
end loop;
close c;
end;
使用for loop遍历游标
declare
cursor c is select ename from emp where
deptno=10;
--v_ename c%rowtype;
begin
--open c;
for v_ename in c loop
--自己声明一个接收c值的变量
--自己open c fetch c into
ename
--自动判断是否取到
--结束,自动close c
dbms_output.put_line('员工姓名是:'||v_ename.ename);
--fetch c into
v_ename;
end loop;
--close c;
end;
推荐使用for:
declare
cursor c is select ename from emp where
deptno=10;
begin
for v_ename in c loop
dbms_output.put_line('员工姓名是:'||v_ename.ename);
end loop;
end;
根据用户输入的部门编号输出符合条件的员工姓名极其工资
declare
cursor c is select ename,sal from emp where
deptno=&部门编号,;
begin
for v_emp in c loop
dbms_output.put_line('员工姓名是:'||v_emp.ename||'工资为:'||v_emp.sal);
end loop;
end;
根据用户输入的部门编号及其工作输出符合条件的员工姓名极其工资
declare
cursor c is select ename,sal from emp where
deptno=&部门编号 and job=upper('&工作');
--工作是字符串型,所以要加''
begin
for v_emp in c loop
dbms_output.put_line('员工姓名是:'||v_emp.ename||'工资为:'||v_emp.sal);
end loop;
end;
相关文章推荐
- 12-4 plsql中使用循环语句
- 【备战蓝桥杯第一日】2015年12月12…
- 【备战蓝桥杯第4天】2015-12-13--&…
- 【备战蓝桥杯第5天】2015年12月16…
- 【备战蓝桥杯第6天】复习数组相关…
- 【备战蓝桥杯第6天】复习方法相关…
- 【备战蓝桥杯第6天】复习-方法调用…
- 【备战蓝桥杯第7天】复习面向对象…
- nav标签使用说明
- 使用R语言和XML包抓取网页数据-Scraping data from web pages in R with XML package
- Servlet监听器
- ASCII码表
- 自我总结(九)---
- linux 基础命令(1)
- react-native 在任意view fragment activity中集成react
- iOS:SVN出错 Description : The working copy is locked due to a previous error
- HUST 1010 The Minimum Length
- 搭建Go开发及调试环境(LiteIDE + GoClipse) -- Windows篇
- TCP/IP协议三次握手与四次握手流程解析
- CodeVS 2597团伙(并查集)