Oracle笔记整理(5)

2019-04-08 21:42

end loop;

commit;--提交事务 end;

select * from emp where sal<1500 /* ???? declare

cursor cur is select sal from emp where sal < 1500 for update; begin

for v_sal in cur loop

dbms_output.put_line('sal:'||v_sal);

update emp set sal=v_sal+100 where current of cur;--使用当前的游标作为条件 end loop; commit;--提交事务 end;

*/ /*

ref游标:可执行动态SQL 1.声明游标类型 2.声明游标

3.操作游标 */

declare

type my_cur_type is ref cursor;--声明ref游标类型 my_cur my_cur_type;--声明游标(声明变量) v_sql varchar2(200); v_emp_rec emp%rowtype; begin

v_sql:='select * from emp'; open my_cur for v_sql;--打开游标 loop

fetch my_cur into v_emp_rec; exit when my_cur%NOTFOUND ;

dbms_output.put_line('ename:'||v_emp_rec.ename); end loop; close my_cur; end;

12.存储过程

/*

存储过程的优点:

模块化:将程序分解为逻辑模块

可重用性:可以被任意数目的程序调用 可维护性:简化维护操作

安全性:通过设置权限,使数据更安全 执行效率较高 语法:

CREATE [OR REPLACE] PROCEDURE [()] IS|AS --变量的声明部分 BEGIN

--业务 [EXCEPTION --异常处理 ] END; */

create or replace procedure pro1 is

v_hello varchar2(20):='hello,xasxt!'; begin

dbms_output.put_line(v_hello); end; /*

调用存储过程的方式:

1.execute 存储过程名称(sqlplus中) eg: execute pro1();

2.call 存储过程名称(sqlplus中) eg: call pro1();

3.使用程序块调用 begin

存储过程名称 end; */ begin pro1(); end;

--根据部门编号,为员工加薪(部门编号=10,加薪100,部门编号=20,加薪200,部门编号=30,加薪300)

create or replace procedure pro2 is

cursor c is select * from emp for update; begin

for v_emp_rec in c

loop

if(v_emp_rec.deptno=10) then

update emp set sal=sal+100 where current of c; elsif v_emp_rec.deptno=20 then

update emp set sal=sal+200 where current of c; elsif v_emp_rec.deptno=30 then

update emp set sal=sal+300 where current of c; end if; end loop; commit; end;

select * from emp begin pro2();

end;

--带参数的存储过程 /*

IN:用于接受调用程序的值 OUT:用于向调用程序返回值

IN OUT:用于接受调用程序的值,并向调用程序返回更新的值 */

create or replace procedure pro3(emp_no number) is

v_name varchar2(20); begin

select ename into v_name from emp where empno=emp_no; dbms_output.put_line('名称:'||v_name); exception when NO_DATA_FOUND then

dbms_output.put_line('没有找到雇员信息..'); end;

--调用带参的存储过程 begin

pro3('793422'); end;

--既有输入参数又有输出参数(接受返回值)

create or replace procedure pro4(emp_no in number,emp_sal out number) is

v_name varchar2(20); begin

select ename,sal into v_name,emp_sal from emp where empno=emp_no; dbms_output.put_line('名称:'||v_name); exception when NO_DATA_FOUND then

dbms_output.put_line('没有找到雇员信息..'); end;

-- 调用 declare

emp_no number:=7934;

emp_sal number;--接收返回值 begin

pro4(emp_no,emp_sal);

dbms_output.put_line('out:....'||emp_sal); end;

--根据分页单位计算出指定表的总页数

create or replace procedure pro5(tableName in varchar2,pageSize in number,totalPages out number)

is

v_totalRows number;--总记录数 v_sql varchar2(200); begin

--查询指定表的总记录数

v_sql:='select count(*) from '||tableName; --执行动态SQL

execute immediate v_sql into v_totalRows;

--总记录数%分页单位==0?总记录数/分页单位:总记录数/分页单位+1 if mod(v_totalRows,pageSize)=0 then totalPages:=v_totalRows/pageSize; else

totalPages:=floor(v_totalRows/pageSize)+1; end if; end;

--调用 declare

tableName varchar2(20):='&tableName'; pageSize number :='&pageSize'; totalPages number;

begin

pro5(tableName,pageSize,totalPages);

dbms_output.put_line('totalPages--->'||totalPages); end;

12.函数

/*

函数:类似于存储过程,都是oracle的子程序,函数有返回值,而存储过程没有返回值 函数只能接受 IN 参数,而不能接受 IN OUT 或 OUT 参数 CREATE [OR REPLACE] FUNCTION [(param1,param2)]

RETURN IS|AS [local declarations] BEGIN

Executable Statements; RETURN result; EXCEPTION

Exception handlers; END; */

create or replace function fun_hello return varchar2 is

begin

return 'hello,xasxt!'; end;

--调用函数

select fun_hello from dual;

create or replace function fun_sal(sal_val number) return varchar2 is

v_max number; v_min number;

v_return varchar2(50); begin

select max(sal),min(sal) into v_max,v_min from emp; if sal_val>=v_min and sal_val<=v_max then

v_return:='你的输入的工资在最高工资和最低工资之间!'; else

v_return:='你的工资不在最高工资和最低工资范围之间'; end if ;

return v_return;

end;

--调用方式1:使用SQL调用 select fun_sal(20000) from dual; --调用方式2:使用程序块调用 declare

v_str varchar2(50); v_sal number; begin

v_sal:='&sal';

v_str:= fun_sal(v_sal);

dbms_output.put_line(v_str); end;


Oracle笔记整理(5).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:金榜题名宴会致辞

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: