--存储过程
--函数
--完成普通的SQL编程。
PLSQL 完整语法结构:
declare
--定义变量、常量等(数据类型和长度)
begin
--plsql的内容
exception 异常类型
--异常处理
end;
--数据类型。
*/
-------计算员工的缴税---------
declare
t_name scott.emp.ename%type;
t_sal number(
10,
2);
t_tax_rate constant number(
3,
2) :
= 0.2;
begin
select ename,sal
+nvl(comm,
0)
into t_name,t_sal
from scott.emp
where empno
=&请输入编号;
dbms_output.put_line('姓名:'||t_name);
dbms_output.put_line('薪水:'||t_sal);
dbms_output.put_line('缴税:'||(t_sal
*t_tax_rate));
dbms_output.put_line('实发:'||(t_sal
-(t_sal
*t_tax_rate)));
exception
when no_data_found
then
dbms_output.put_line('您输入的编号有误!');
end;
--控制结构
if 条件
then
elsif 条件2 then
else
end if;
declare
tint number(
2) :
= 9;
begin
if tint
>10 then
dbms_output.put_line('大于10');
elsif tint=10 then
dbms_output.put_line('等于10');
else
dbms_output.put_line('小于10');
end if;
end;
--case语句
--精确匹配
declare
tint number(
2) :
= 9;
begin
case tint
when 9 then
dbms_output.put_line('==9');
when 8 then
dbms_output.put_line('==8');
else
dbms_output.put_line('others');
end case;
end;
--范围匹配
declare
tint number(
2) :
= -5;
begin
case
when tint
=9 or tint
>9 then
dbms_output.put_line('>=9');
when tint
<9 and tint
>0 then
dbms_output.put_line('0-8');
else
dbms_output.put_line('others');
end case;
end;
--循环结构
--1. loop 。。。。 end loop;循环。
declare
i int :
= 1;
begin
loop
dbms_output.put_line('i='||i);
i := i
+ 1;
exit when i
>10;
end loop;
end;
--2. while 循环(loop增强——次数未知)
declare
i int :
= 1;
begin
while i
<10 loop
dbms_output.put_line('while >>>> i='||i);
i := i
+ 1;
end loop;
end;
--3. for 循环(loop简化版——次数固定)
create or replace procedure proc_show99
as
begin
for i
in 1..
9 loop
for j
in 1..i loop
dbms_output.put(j||'*'||i
||'='||j
*i
||' ');
end loop;
dbms_output.put_line('');
end loop;
end;
--自定义异常(如果出现不满足要求的数据,使用抛出异常的方式,终止程序的执行)
declare
d_name scott.dept.dname%type;
begin
select dname
into d_name
from scott.dept;
dbms_output.put_line(d_name);
exception
when others
then
dbms_output.put_line('系统故障,!!!');
end;
--修改员工的工资,如果工资降低,抛出异常。
declare
t_sal number(
10,
2);
e_no number(
10) :
=&请输入编号;
n_sal number(
10) :
=&请输入薪水;
exceptionInfo exception;
begin
select sal
into t_sal
from scott.emp
where empno
=e_no;
if n_sal
<t_sal
then
--dbms_output.put_line('员工工资不能降!!!');
raise exceptionInfo;
end if;
dbms_output.put_line('我能执行吗?');
exception
when exceptionInfo
then
dbms_output.put_line('员工工资不能降!!!');
end;
--游标
--1. 显示游标 (open。。。 close)
--2. for循环游标(隐士游标)
--显示游标
declare
cursor cur_deptinfo
is select * from scott.dept;
deptLine scott.dept%rowtype;
begin
open cur_deptinfo;
--打开
-- 提取数据
loop
fetch cur_deptinfo
into deptLine;
exit when cur_deptinfo
%notfound;
dbms_output.put_line(deptLine.deptno||':'||deptLine.dname);
end loop;
close cur_deptinfo;
--关闭
end;
--for循环游标
declare
cursor cur_deptinfo
is select * from scott.dept;
begin
for deptLine
in cur_deptinfo loop
--
dbms_output.put_line(deptLine.deptno
||':'||deptLine.dname);
end loop;
end;
转载于:https://www.cnblogs.com/huzi007/archive/2013/01/21/2869477.html
相关资源:Oracle SQL高级编程