oracle自定义函数、存储过程
2015-05-20 12:59
447 查看
declare
v_name varchar2(50);
v_age number;
begin
v_name := '孙悟空';
v_age := 40;
-- 嵌套块
declare
v_color varchar2(50);
v_price number;
begin
v_color := '红色';
dbms_output.put_line(v_name
|| '-->' || v_color);
end;
-- 外部块无法访问嵌套块的变量
v_price := 30;
end;
declare
v_age number;
begin
v_age := 50;
if v_age > 65 then
dbms_output.put_line('老年人');
elsif v_age >45 then
dbms_output.put_line('中年人');
elsif v_age > 25 then
dbms_output.put_line('青年人');
else
dbms_output.put_line('其他');
end if;
end;
declare
v_count number := 1;
begin
-- 简单循环,死循环
loop
dbms_output.put_line('v_count:' || v_count);
v_count := v_count + 1;
-- exit完全类似于Java的break语句
exit when v_count > 20;
end loop;
end;
select * from dept;
delete from dept where to_char(deptno) not like '%0';
commit;
declare
v_count number := 1;
begin
-- 简单循环,死循环
loop
-- exit完全类似于Java的break语句
exit when v_count > 9;
insert into dept
values( v_count , '部门' || v_count
, '位置' || v_count);
v_count := v_count + 1;
end loop;
end;
declare
v_count number := 11;
begin
while v_count < 20 loop
insert into dept
values( v_count , '部门' || v_count
, '位置' || v_count);
v_count := v_count + 1;
end loop;
end;
select * from dept;
begin
-- for循环的循环计数器无须声明
for v_index in reverse 2..10 loop
-- for循环的循环计数器不能被重新赋值
//v_index := v_index + 1;
dbms_output.put_line(v_index);
end loop;
end;
declare
v_count number := 1;
begin
<<outer>>
while v_count < 10 loop
for v_index in 12..19 loop
dbms_output.put_line(v_count
|| '-->' || v_index);
exit when v_index > 15;
end loop;
v_count := v_count + 1;
end loop;
end;
declare
v_index number := 21;
begin
<<reinsert>>
insert into dept
values(v_index , '部门' || v_index
, '位置' || v_index);
v_index := v_index + 1;
if v_index < 29 then
goto reinsert;
end if;
end;
select * from dept where dname = '部门19';
select * from dept;
begin
print_boolean(true);
end;
-- 所谓存储过程,就是为匿名块加个头即可。
-- 如果没有指定参数的传递模式,默认是in
create or replace procedure print_boolean(p_val boolean)
is
begin
if p_val then
dbms_output.put_line('真');
else
dbms_output.put_line('假');
end if;
end;
create or replace procedure cal_salary(p_sal number ,
p_comm number , p_real out number)
is
v_count number;
begin
v_count := p_sal + p_comm;
p_real := v_count * 0.8;
end;
declare
v_sal number;
begin
-- 对于传出参数来说,执行存储过程时,
-- 存储过程会改变该传出参数的值。
-- 调用存储过程时,传出参数只能是变量(存储过程需要为这个参数赋值)
cal_salary(2000*2 , 200 , v_sal);
dbms_output.put_line(v_sal);
end;
create or replace procedure cal_cube(p_num in out number)
is
v_count number;
begin
-- 对于in out 参数来说,程序就要读取参数的值,
-- 也会在存储过程中改变该参数的值。
v_count := p_num*p_num*p_num;
p_num := v_count;
end;
declare
v_num number := 4;
begin
cal_cube(v_num);
dbms_output.put_line(v_num);
end;
v_name varchar2(50);
v_age number;
begin
v_name := '孙悟空';
v_age := 40;
-- 嵌套块
declare
v_color varchar2(50);
v_price number;
begin
v_color := '红色';
dbms_output.put_line(v_name
|| '-->' || v_color);
end;
-- 外部块无法访问嵌套块的变量
v_price := 30;
end;
declare
v_age number;
begin
v_age := 50;
if v_age > 65 then
dbms_output.put_line('老年人');
elsif v_age >45 then
dbms_output.put_line('中年人');
elsif v_age > 25 then
dbms_output.put_line('青年人');
else
dbms_output.put_line('其他');
end if;
end;
declare
v_count number := 1;
begin
-- 简单循环,死循环
loop
dbms_output.put_line('v_count:' || v_count);
v_count := v_count + 1;
-- exit完全类似于Java的break语句
exit when v_count > 20;
end loop;
end;
select * from dept;
delete from dept where to_char(deptno) not like '%0';
commit;
declare
v_count number := 1;
begin
-- 简单循环,死循环
loop
-- exit完全类似于Java的break语句
exit when v_count > 9;
insert into dept
values( v_count , '部门' || v_count
, '位置' || v_count);
v_count := v_count + 1;
end loop;
end;
declare
v_count number := 11;
begin
while v_count < 20 loop
insert into dept
values( v_count , '部门' || v_count
, '位置' || v_count);
v_count := v_count + 1;
end loop;
end;
select * from dept;
begin
-- for循环的循环计数器无须声明
for v_index in reverse 2..10 loop
-- for循环的循环计数器不能被重新赋值
//v_index := v_index + 1;
dbms_output.put_line(v_index);
end loop;
end;
declare
v_count number := 1;
begin
<<outer>>
while v_count < 10 loop
for v_index in 12..19 loop
dbms_output.put_line(v_count
|| '-->' || v_index);
exit when v_index > 15;
end loop;
v_count := v_count + 1;
end loop;
end;
declare
v_index number := 21;
begin
<<reinsert>>
insert into dept
values(v_index , '部门' || v_index
, '位置' || v_index);
v_index := v_index + 1;
if v_index < 29 then
goto reinsert;
end if;
end;
select * from dept where dname = '部门19';
select * from dept;
begin
print_boolean(true);
end;
-- 所谓存储过程,就是为匿名块加个头即可。
-- 如果没有指定参数的传递模式,默认是in
create or replace procedure print_boolean(p_val boolean)
is
begin
if p_val then
dbms_output.put_line('真');
else
dbms_output.put_line('假');
end if;
end;
create or replace procedure cal_salary(p_sal number ,
p_comm number , p_real out number)
is
v_count number;
begin
v_count := p_sal + p_comm;
p_real := v_count * 0.8;
end;
declare
v_sal number;
begin
-- 对于传出参数来说,执行存储过程时,
-- 存储过程会改变该传出参数的值。
-- 调用存储过程时,传出参数只能是变量(存储过程需要为这个参数赋值)
cal_salary(2000*2 , 200 , v_sal);
dbms_output.put_line(v_sal);
end;
create or replace procedure cal_cube(p_num in out number)
is
v_count number;
begin
-- 对于in out 参数来说,程序就要读取参数的值,
-- 也会在存储过程中改变该参数的值。
v_count := p_num*p_num*p_num;
p_num := v_count;
end;
declare
v_num number := 4;
begin
cal_cube(v_num);
dbms_output.put_line(v_num);
end;
相关文章推荐
- Oracle自定义函数和存储过程示例,自定义函数与存储过程区别
- Oracle数据库开发之存储过程和自定义函数
- oracle自定义函数、存储过程2
- Oracle自定义函数与存储过程
- Oracle 存储过程中调用自定义的函数问题?
- SQL Server的存储过程或自定义函数调用Com组件
- mysql系列:存储过程与自定义函数
- 如何在ORACLE启动时执行一个自定义的存储过程
- oracle 存储过程和函数例子 --2
- Oracle 存储过程 定义 和 优点 与 函数 区别
- mysql存储过程和函数(二)——简单自定义函数
- ibatis调用oracle的函数,存储过程的方法_IN_和OUT_游标
- oracle查看所有函数或存储过程的代码
- Oracle中的包、存储过程、函数被锁定解决方案
- oracle 存储过程和函数学习笔记
- 存储过程或自定义函数调用存储过程的变通实现方法
- oracle 存储过程和函数的调用方法
- oracle 存储过程和函数
- Oracle函数和存储过程的区别
- 如何实现在Oracle中应用存储过程调用MatLab函数(3)