*=*=*=*=*=*=*=*=*=*=>141020START<=*=*=*=*=*=*=*=*=*=*
-- =====<PL/SQL中的数据类型>===== --
-- 包含标量类型、LOB类型、属性类型
-- 标量类型包含:数字、字符、布尔型、日期时间、参照类型
-- LOB类型包含:BFILE、BLOB、CLOB、NCLOB
-- 复合类型包含:PL/SQL记录、PL/SQL表
-- 2.LOB类型
==============================
-- =====<PL/SQL支持的流程控制结构>===== --
-- 1.条件控制
-- 1.条件语句(分支)
-- if语句
if Conditions
then
CodeExecution
end if;
if Conditions
then
CodeExecution
else CodeExecution
end if;
if Conditions
then
CodeExecution
else if Conditions
then
CodeExecution
else CodeExecutio
end if;
end if;
例:简单的条件判断 if - then
编写一个过程,可以输入一个雇员名,如果该雇员的工资低于2000,就给该员工工资增加10%
create or replace procedure pro_conditions(v_ename in varchar2) is
v_sal emp.sal%type;
begin
select sal into v_sal from emp where ename=v_ename;
if v_sal<2000 then
v_sal:=v_sal+v_sal*0.1;
update emp set sal=v_sal where ename=v_ename;
end if;
end;
例:二重条件分支if - then
编写一个过程,可以输入一个雇员名,如果该雇员的补助不是0,就在原来的基础上增加100
如果为0就把补助设为200
create or replace procedure pro_05(v_ename varchar2) is
v_comm emp.comm%type;
begin
select nvl(comm,0) into v_comm from emp where ename=v_ename;
dbms_output.put_line('奖金:'||v_comm);
if v_comm!=0 then
v_comm:=v_comm+100;
else
v_comm:=200;
end if;
update emp set comm=v_comm where ename=v_ename;
end;
例:多重条件分支if - then
编写一个过程,可以输入一个雇员编号,给职位是PRESIDENT的员工工资增加1000
职位是MANAGER的员工工资加500
其他员工工资增加200
create or replace procedure pro_06(v_empno number) is
v_job emp.job%type;
v_sal emp.sal%type;
begin
select job,sal into v_job,v_sal from emp where empno=v_empno;
if v_job='PRESIDENT' then
v_sal:=v_sal+1000;
else if v_job='MANAGER' then
v_sal:=v_sal+500;
else v_sal:=v_sal+200;
end if;
end if;
update emp set sal=v_sal where empno=v_empno;
end;
-- case语句
例:多重条件分支if - then
编写一个过程,可以输入一个雇员编号,给职位是PRESIDENT的员工工资增加1000
职位是MANAGER的员工工资加500
CLERK +300
ANALYST +200
SALESMAN +100
create or replace procedure pro_07(v_empno number) is
v_job emp.job%type;
v_sal emp.sal%type;
begin
select job,sal into v_job,v_sal from emp where empno=v_empno;
case v_job
when 'PRESIDENT' then
v_sal:=v_sal+1000;
when 'MANAGER' then
v_sal:=v_sal+500;
when 'CLERK' then
v_sal:=v_sal+300;
when 'ANALYST' then
v_sal:=v_sal+200;
when 'SALESMAN' then
v_sal:=v_sal+100;
else null;
end case;
update emp set sal=v_sal where empno=v_empno;
end;
-- 循环控制
-- LOOP语句
-- loop
-- while loop
-- for loop
LOOP循环(相当于Java中do{}while();)
-- 创建一个新表
create table tab_user(
user_no number(10),
user_name varchar2(20));
create or replace procedure pro_08(v_name varchar2) is
-- 定义变量,并赋初值
v_num tab_user.user_no%type:=1;
begin
loop
insert into tab_user values(v_num,v_name);
v_num:=v_num+1;
exit when v_num=11;
end loop;
end;
while循环
问题:输入用户名,循环添加10个用户到表中,编号从11开始增加
create or replace procedure pro_10(v_name varchar2) is
-- 定义变量,并赋初值
v_num tab_user.user_no%type:=11;
begin
while v_num<21 loop
insert into tab_user values(v_num,v_name);
v_num:=v_num+1;
end loop;
end;
for循环
create or replace procedure pro_10(v_name varchar2) is
begin
for i in 21..30 loop
insert into tab_user values(i,v_name);
end loop;
end;
-- 顺序控制
-- goto语句
例:
declare
i number(20):=1;
begin
loop
dbms_output.put_line('i=>'||i);
i:=i+1;
if i=10 then
goto my_tag;
end if;
end loop;
dbms_output.put_line('循环结束');
<<my_tag>>
dbms_output.put_line('循环');
end;
-- =====<PL/SQL中的异常处理>===== --
-- case_not_found=>当case的条件和when项不匹配时
declare
begin
case '&aa'
when 1 then
dbms_output.put_line('杨亦风');
when 2 then
dbms_output.put_line('七夜');
end case;
exception
when case_not_found then
dbms_output.put_line('都已经飞升到魔界');
end;
-- cursor_already_open=>游标已打开
declare
type my_crusor is ref cursor;
-- 打开前要先声明
ms my_crusor;
begin
open ms for select * from emp;
open ms for select * from emp;
exception
when cursor_already_open then
dbms_output.put_line('游标已打开!');
end;
-- =====<PL/SQL游标>===== --
-- cursor
隐式游标 =>使用DML语句时自动创建
declare
begin
update emp set sal=3000 where ename='SMITH';
if sql%found then
dbms_output.put_line('有'||sql%rowcount||'个数据被更新');
else
dbms_output.put_line('没有数据受到影响');
end if;
end;
显示游标 =>用来存放查询到的结果
-- 使用%rowtype来存放查询语句返回的一条数据
/*
declare
my_row emp%rowtype;
begin
end;
*/
declare
my_row emp%rowtype;
begin
end;
-- 要接收返回的多条数据,必须使用游标
declare
begin
cursor my_cursor is select ename,job,sal from emp where deptno=&aa;
while then
end;
*=*=*=*=*=*=*=*=*=*=>141020END<=*=*=*=*=*=*=*=*=*=*