oracle中存储过程如何使用
发表于:2025-11-08 作者:千家信息网编辑
千家信息网最后更新 2025年11月08日,今天就跟大家聊聊有关oracle中存储过程如何使用,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。一. 使用for循环游标:遍历所有职位为经理的
千家信息网最后更新 2025年11月08日oracle中存储过程如何使用
今天就跟大家聊聊有关oracle中存储过程如何使用,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。
一. 使用for循环游标:遍历所有职位为经理的雇员
1. 定义游标(游标就是一个小集合)
2. 定义游标变量
3. 使用for循环游标
declare -- 定义游标c_job cursor c_job is select empno, ename, job, sal from emp where job = 'MANAGER'; -- 定义游标变量c_row c_row c_job%rowtype;begin -- 循环游标,用游标变量c_row存循环出的值 for c_row in c_job loop dbms_output.put_line(c_row.empno || '-' || c_row.ename || '-' || c_row.job || '-' || c_row.sal); end loop;end;
二. fetch游标:遍历所有职位为经理的雇员
使用的时候必须明确的打开和关闭
declare --定义游标c_job cursor c_job is select empno, ename, job, sal from emp where job = 'MANAGER'; --定义游标变量c_row c_row c_job%rowtype;begin open c_job; loop --提取一行数据到c_row fetch c_job into c_row; --判读是否提取到值,没取到值就退出 exit when c_job%notfound; dbms_output.put_line(c_row.empno || '-' || c_row.ename || '-' || c_row.job || '-' || c_row.sal); end loop; --关闭游标 close c_job;end;
三. 使用游标和while循环:遍历所有部门的地理位置
--3,使用游标和while循环来显示所有部门的的地理位置(用%found属性)declare --声明游标 cursor csr_TestWhile is select loc from dept; --指定行指针 row_loc csr_TestWhile%rowtype;begin open csr_TestWhile; --给第一行数据 fetch csr_TestWhile into row_loc; --测试是否有数据,并执行循环 while csr_TestWhile%found loop dbms_output.put_line('部门地点:' || row_loc.LOC); --给下一行数据 fetch csr_TestWhile into row_loc; end loop; close csr_TestWhile;end;四. 带参的游标:接受用户输入的部门编号
declare -- 带参的游标 cursor c_dept(p_deptNo number) is select * from emp where emp.deptno = p_deptNo; r_emp emp%rowtype;begin for r_emp in c_dept(20) loop dbms_output.put_line('员工号:' || r_emp.EMPNO || '员工名:' || r_emp.ENAME || '工资:' || r_emp.SAL); end loop;end;五. 加锁的游标:对所有的salesman增加佣金500
declare --查询数据,加锁(for update of) cursor csr_addComm(p_job nvarchar2) is select * from emp where job = p_job for update of comm; r_addComm emp%rowtype; commInfo emp.comm%type;begin for r_addComm in csr_addComm('SALESMAN') loop commInfo := r_addComm.comm + 500; --更新数据(where current of) update emp set comm = commInfo where current of csr_addComm; end loop;end;六. 使用计数器:找出两个工作时间最长的员工
declare cursor crs_testComput is select * from emp order by hiredate asc; --计数器 top_two number := 2; r_testComput crs_testComput%rowtype;begin open crs_testComput; fetch crs_testComput into r_testComput; while top_two > 0 loop dbms_output.put_line('员工姓名:' || r_testComput.ename || ' 工作时间:' || r_testComput.hiredate); --计速器减1 top_two := top_two - 1; fetch crs_testComput into r_testComput; end loop; close crs_testComput;end;七. if/else判断:对所有员工按基本薪水的20%加薪,如果增加的薪水大于300就取消加薪
declare cursor crs_upadateSal is select * from emp for update of sal; r_updateSal crs_upadateSal%rowtype; salAdd emp.sal%type; salInfo emp.sal%type;begin for r_updateSal in crs_upadateSal loop salAdd := r_updateSal.sal * 0.2; if salAdd > 300 then salInfo := r_updateSal.sal; dbms_output.put_line(r_updateSal.ename || ': 加薪失败。' || '薪水维持在:' || r_updateSal.sal); else salInfo := r_updateSal.sal + salAdd; dbms_output.put_line(r_updateSal.ENAME || ': 加薪成功.' || '薪水变为:' || salInfo); end if; update emp set sal = salInfo where current of crs_upadateSal; end loop;end;
八. 使用case
when:按部门进行加薪
declare cursor crs_caseTest is select * from emp for update of sal; r_caseTest crs_caseTest%rowtype; salInfo emp.sal%type;begin for r_caseTest in crs_caseTest loop case when r_caseTest.deptno = 10 THEN salInfo := r_caseTest.sal * 1.05; when r_caseTest.deptno = 20 THEN salInfo := r_caseTest.sal * 1.1; when r_caseTest.deptno = 30 THEN salInfo := r_caseTest.sal * 1.15; when r_caseTest.deptno = 40 THEN salInfo := r_caseTest.sal * 1.2; end case; update emp set sal = salInfo where current of crs_caseTest; end loop;end;
九. 异常处理:数据回滚
set serveroutput on;declare d_name varchar2(20);begin d_name := 'developer'; savepoint A; insert into DEPT values (50, d_name, 'beijing'); savepoint B; insert into DEPT values (40, d_name, 'shanghai'); savepoint C; exception when others then dbms_output.put_line('error happens'); rollback to A; commit;end;/十. 基本指令:
set serveroutput on size 1000000 format wrapped; --使DBMS_OUTPUT有效,并设置成最大buffer,防止"吃掉"最前面的空格set linesize 256; --设置一行可以容纳的字符数set pagesize 50; --设置一页有多少行数set arraysize 5000; --设置来回数据显示量,这个值会影响autotrace时一致性读等数据set newpage none; --页和页之间不设任何间隔set long 5000; --LONG或CLOB显示的长度set trimspool on; --将SPOOL输出中每行后面多余的空格去掉set timing on; --设置查询耗时col plan_plus_exp format a120; --autotrace后explain plan output的格式set termout off; --在屏幕上暂不显示输出的内容,为下面的设置sql做准备alter session set nls_date_format='yyyy-mm-dd hh34:mi:ss'; --设置时间格式
小知识:
下面的语句一定要在Command Window里面才能打印出内容
set serveroutput on;begin dbms_output.put_line('hello!');end;/看完上述内容,你们对oracle中存储过程如何使用有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注行业资讯频道,感谢大家的支持。
游标
数据
循环
内容
员工
部门
一行
变量
薪水
时间
过程
存储
位置
地理
地理位置
格式
知识
空格
经理
职位
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
超微服务器配置磁盘阵列
为什么数据突然无法连接服务器
雷霹雳家庭教育与网络安全
嘉峪关数字雄关网络技术有限公司
北京云音网络技术有限公司南京
犀牛网络安全
软件开发降低运行成本措施
有Java 基础学网络安全
金融科技和互联网安全
石家庄工业控制上位机软件开发
数据库集群价位
习颂互联网科技
软件开发中心迁移
大学生网络安全论文500
滁州服务好的服务器机箱
软件开发测试体会
网络安全使用的是什么语言
内存式数据库软件
我的世界1.16.5服务器搭建
互联网领先科技王海峰
hp服务器开机无画面
数据库eis
win10 打印服务器
暗黑二重制版怎样下载亚洲服务器
决策指挥服务器
长沙天心区学习软件开发培训
成都it软件开发
数据库集群价位
数据库等级证书
陕西省网络安全等级保护