版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Ghost_chou/article/details/47283551
PL/SQL的学习
--第一个PL/SQL程序
--注:set serveroutput on 用来在对话框中显示输出内容,不能写错,写错不会有错误提示
-- "/"为执行程序;"||"为加号
-- ":="为赋值语句,中间不能有空格
set serveroutput on;
--声明部分
declare
v_num number(8);
v_name varchar2(20);
v_sal emp.sal%type;
begin
--执行部分
v_num := 200;
v_name := 'ghost';
v_sal :=520;
dbms_output.put_line('hello ghost');
dbms_output.put_line('v_num: '||v_num);
dbms_output.put_line('v_name: '||v_name);
dbms_output.put_line('v_sal: '||v_sal);
--异常部分
end;
/
--记录类型
declare
type v_type is record (
v_no emp.empno%type,
v_sal emp.sal%type,
v_job emp.job%type
);
v_record v_type;
begin
select empno,sal,job into v_record.v_no,v_record.v_sal,v_record.v_job from emp where ename='&name';
dbms_output.put_line('员工号: '||v_record.v_no);
dbms_output.put_line('工资: '||v_record.v_sal);
dbms_output.put_line('工作: '||v_record.v_job);
end;
/
--练习
--1、使用块结构输出:学校:XXX学号:XXX,姓名:XXX,性别:XXX.
declare
v_sch varchar2(20);
v_no number(8);
v_name varchar2(20);
v_sex varchar2(10);
begin
v_sch :='MIT';
v_no :=9;
v_name :='ghost';
v_sex :='male';
dbms_output.put_line('学校:'||v_sch);
dbms_output.put_line('学号:'||v_no);
dbms_output.put_line('姓名:'||v_name);
dbms_output.put_line('性别:'||v_sex);
end;
/
--2、输入员工号,显示员工姓名、工资(使用%type完成)。
declare
v_name emp.ename%type;
v_sal emp.sal%type;
begin
select ename,sal into v_name,v_sal from emp where empno=&no;
dbms_output.put_line('姓名: '||v_name);
dbms_output.put_line('工资: '||v_sal);
end;
/
--3、输入员工号,显示员工姓名、工资(使用%rowtype完成)。
declare
v_emp emp%rowtype;
begin
select ename,sal into v_emp.ename,v_emp.sal from emp where empno=&no;
dbms_output.put_line('姓名: '||v_emp.ename);
dbms_output.put_line('工资: '||v_emp.sal);
end;
/
--4、输入员工姓名,显示员工编号、工资、及工作(使用record完成)
declare
type v_type is record (
v_no emp.empno%type,
v_sal emp.sal%type,
v_job emp.job%type
);
v_record v_type;
begin
select empno,sal,job into v_record.v_no,v_record.v_sal,v_record.v_job from emp where ename='&name';
dbms_output.put_line('员工号: '||v_record.v_no);
dbms_output.put_line('工资: '||v_record.v_sal);
dbms_output.put_line('工作: '||v_record.v_job);
end;
/
--5、输入员工号,显示雇员姓名、工资、个人所得税(税率为0.03)为例(使用record、%type完成).
declare
type v_type is record (
v_name emp.ename%type,
v_sal emp.sal%type,
v_sal_af emp.sal%type
);
v_record v_type;
begin
select ename,sal,sal-sal*0.03 into v_record.v_name,v_record.v_sal,v_record.v_sal_af from emp where empno=&no;
dbms_output.put_line('员工号: '||v_record.v_name);
dbms_output.put_line('工资: '||v_record.v_sal);
dbms_output.put_line('工作: '||v_record.v_sal_af);
end;
/
--6、输出最高工资员工的名字,工作岗位。
declare
v_emp emp%rowtype;
begin
select ename,job into v_emp.ename,v_emp.job from emp where sal=(select max(emp.sal) from emp);
dbms_output.put_line('姓名: '||v_emp.ename);
dbms_output.put_line('工作: '||v_emp.job);
end;
/
--7、输出员工中最高工资和最低工资
declare
v_sal1 emp.sal%type;
v_sal2 emp.sal%type;
begin
select sal into v_sal1 from emp where sal=(select max(emp.sal) from emp);
select sal into v_sal2 from emp where sal=(select min(emp.sal) from emp);
dbms_output.put_line('最高工资: '||v_sal1);
dbms_output.put_line('最低工资: '||v_sal2);
end;
/
--8、输入员工的编号,显示员工的姓名,部门名称,工资及工资的级别
declare
v_name emp.ename%type;
v_job dept.dname%type;
v_sal emp.sal%type;
v_grade salgrade.grade%type;
begin
select e.ename,d.dname,e.sal,s.grade into v_name,v_job,v_sal,v_grade from emp e,dept d,salgrade s where empno=&no
and e.deptno = d.deptno
and e.sal between s.losal and s.hisal;
dbms_output.put_line('员工号: '||v_name);
dbms_output.put_line('工作: '||v_job);
dbms_output.put_line('工资: '||v_sal);
dbms_output.put_line('工资级别: '||v_grade);
end;
/
--条件语句
if ... then ... end if
if ... the ... elsif then ... end if
--练习
--1.输入员工的编号,判断此员工的工资:
--如果工资sal<1000则让工资在原来在基础上加上100
--如果工资1000<= sal <2000则让工资在原来在基础上加上200
--否则让员工工资在原来的基础上加上300;
declare
v_sal emp.sal%type;
v_no number(8):=&no;
begin
select sal into v_sal from emp where empno=v_no;
if v_sal<1000 then
update emp set sal=sal+100 where empno=v_no;
elsif v_sal>1000 and v_sal<2000 then
update emp set sal=sal+200 where empno=v_no;
else
update emp set sal=sal+300 where empno=v_no;
end if;
end;
/
--2.编写一个过程,可以输入一个雇员名,如果该雇员的工资低于2000,就给该员工工资增加10%。(使用if)
declare
v_sal emp.sal%type;
v_name varchar2(20):='&name';
begin
select sal into v_sal from emp where ename=v_name;
if v_sal<2000 then
update emp set sal=sal+sal*0.02 where ename=v_name;
end if;
end;
/
--3.编写一个过程,可以输入一个雇员名,如果该雇员的补助不是0 就在原来的基础上增加100;如果补助为0 就把补助设为200;(使用if...else)
declare
v_comm emp.comm%type;
v_name varchar2(20):='&name';
begin
select comm into v_comm from emp where ename=v_name;
if v_comm!=0 then
update emp set comm=nvl(comm,0)+100 where ename=v_name;
else
update emp set comm=comm+200 where ename=v_name;
end if;
end;
/
--4.编写一个过程,可以输入一个雇员编号,如果该雇员的职位是PRESIDENT就给他的工资增加1000,
--如果该雇员的职位是MANAGER 就给他的工资增加500,其它职位的雇员工资增加200。(使用if..elsif..else)
declare
v_sal emp.sal%type;
v_job emp.job%type;
v_no number(8):=&no;
begin
select sal,job into v_sal,v_job from emp where empno=v_no;
if v_job ='PRESIDENT' then
update emp set sal=sal+1000 where empno=v_no;
elsif v_job ='MANAGER' then
update emp set sal=sal+500 where empno=v_no;
else
update emp set sal=sal+200 where empno=v_no;
end if;
end;
/
--分支语句
case
when ? then ?
when ? then ?
end case
case
when ? then ?
when ? then ?
else
end case
declare
v_sex number(8):=&no;
v_result varchar2(20);
begin
case v_sex
when 1 then v_result:='male';
when 2 then v_result:='female';
else
v_result:='other';
end case;
dbms_output.put_line('性别: '||v_result);
end;
/
--循环语句
--1. loop... exit when ...end loop
--2. while ... loop ...end loop
--3. for 变量 in 初始下标..结束下标 loop... end loop 注:(..为关键符号)
--练习
--1. 循环输出“haha1...haha10”(使用while)
declare
i number(8):=1;
begin
while i<=10 loop
dbms_output.put_line('haha'||i);
i:=i+1;
end loop;
end;
/
--2.把上述示例改为loop实现
declare
i number(8):=1;
begin
loop
dbms_output.put_line('haha'||i);
exit when i=10;
i:=i+1;
end loop;
end;
/
--3.现有一张表users,字段(uid,uname),分别使用(loop、while、for完成)。
--请编写一个过程,可以输入用户名,并循环添加10 个用户到users 表中,用户编号从1 开始增加。
create table users
(
userid number(8),
uname varchar2(20)
);
declare
v_id number(8):=1;
v_name varchar2(20);
begin
while v_id<=3 loop
v_name:='&name';
insert into users values(v_id,v_name);
v_id:=v_id+1;
end loop;
end;
/
declare
v_id number(8):=1;
v_name varchar2(20);
begin
loop
v_name:='&name';
insert into users values(v_id,v_name);
exit when v_id<=3;
v_id:=v_id+1;
end loop;
end;
/
declare
v_name varchar2(20);
begin
for i in 1..10 loop
v_name:='&name';
insert into users values(i,v_name);
end loop;
end;
/
--4.打印九九乘法表
declare
begin
for i in 1..9 loop
for j in 1..i loop
dbms_output.put(i);
dbms_output.put('*');
dbms_output.put(j);
dbms_output.put('=');
if length(i*j)=1 and j!=1 then
dbms_output.put(' ');
end if;
dbms_output.put(i*j);
dbms_output.put(' ');
end loop;
dbms_output.put_line(' ');
end loop;
end;
/
----goto语句 :从1开始,到10结束
declare
i number(8):=1;
begin
loop
dbms_output.put_line('i:' || i);
if i = 10 then
--退出循环
goto end_loop;
end if;
i:=i+1;
end loop;
<<end_loop>>
null;
end;
/
-----数据找不到【异常处理】
declare
v_no number(8) :=&no;
v_name varchar2(20) ;
begin
select ename into v_name from emp where empno = v_no;
dbms_output.put_line('姓名:' || v_name);
exception --异常处理的关键字
when no_data_found then
dbms_output.put_line('编号为' ||v_no || '没有找到指定的数据');
end;
/
-----返回多行数据【异常处理】
declare
v_name varchar2(20) ;
begin
select ename into v_name from emp;
exception --异常处理的关键字
when too_many_rows then
dbms_output.put_line('不能接收多行数据');
end;
/