加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Oracle 的PL/SQL语言使用

发布时间:2020-12-12 13:34:35 所属栏目:百科 来源:网络整理
导读:-- PL/SQL语言(procedure language 过程化语言) -- 1.声明类型 declare k number ; m number default 20 ; -- Character String buffer too small问题 -- pname varchar2(4); -- 所以更换声明 pname emp.ename % type; -- 查询一行用表名声明 prow emp % row
--PL/SQL语言(procedure language 过程化语言)
--1.声明类型
declare
  k number;
  m number default 20;
  --Character String buffer too small问题
  --pname varchar2(4);
  --所以更换声明
  pname emp.ename%type;
  --查询一行用表名声明
  prow emp%rowtype;
begin
  k:=30;
  dbms_output.put_line(k);
  dbms_output.put_line(原样输出 m=||m);
  select ename into pname from emp where empno=7788;
  dbms_output.put_line(pname);
  --查询结果是一行值
  select * into prow from emp where empno=7654;
  dbms_output.put_line
  (prow.empno||-||prow.ename||-||prow.job);
end;

--2.1 结构化判断语句
declare
  k number;
begin
  k:=&这里可以让你输入;
  if k>0 then 
     dbms_output.put_line( k是正数||k);
  elsif k<0 then
     dbms_output.put_line( k是负数||k);
  else
     dbms_output.put_line( k是零||k);
  end if;
end;

--2.2.1 结构化循环语句 loop输出1-10
declare
  k number default 1;
begin
  loop
    dbms_output.put_line(k);
    exit when k=10;
    k:=k+1;
  end loop;
end;
--2.2.2 结构化循环语句 while输出1-10
declare
  k number default 1;
begin
  while k<=10 loop
    dbms_output.put_line(k);
    k:=k+1;
  end loop;
end;
--2.2.3 结构化循环语句 for输出1-10
--1..10 是集合 可称为游标
declare
  k number default 1;
begin
  for k in 1..10 loop
    dbms_output.put_line(k);
  end loop;
end;

--2.2.4 结构化循环语句 for输出1-10
-- 使用游标打印20号部门的员工姓名和工作 方法一
declare
  cursor cur is select ename,job from emp where deptno=20;
begin
  for k in cur loop
    dbms_output.put_line(k.ename||-||k.job);
  end loop;
end;
-- 使用游标打印20号部门的员工姓名和工作 方法二
declare
  pname emp.ename%type;
  pjob emp.job%type;
  cursor cur is select ename,job from emp where deptno=20;
begin
  open cur;
  loop
    fetch cur into pname,pjob;
    exit when cur%notfound;
    dbms_output.put_line(pname||-||pjob);
  end loop;
  close cur;
end;
-- 使用游标对20号部门的员工涨工资
declare
  k number;
  cursor cur is select empno from emp where deptno=20;
begin
  for k in cur loop
    update emp set sal=sal+100 where empno=k.empno;
  end loop;
end;
select * from emp;
-- 例外(基本异常)
declare
  pname emp.ename%type;
  m number;
begin
  m:=abc;
  select ename into pname from emp where deptno=20;
  dbms_output.put_line(pname);
  select ename into pname from emp where deptno=40;
  dbms_output.put_line(pname);
exception
  when no_data_found then
      dbms_output.put_line(没有记录);
  when too_many_rows then
      dbms_output.put_line(太多记录);
  when value_error then
      dbms_output.put_line(类型转换异常);
  when others then
      dbms_output.put_line(其他异常);
end;
-- 例外(自定义异常)
declare
  not_found exception;
  pname emp.ename%type;
  cursor cur is select ename from emp where deptno=40;
begin
  open cur;
  fetch cur into pname;
  if cur%notfound then
    raise not_found;
  end if;
  close cur;
exception
  when not_found then
      dbms_output.put_line(游标中没发现记录);
  when no_data_found then
      dbms_output.put_line(没有记录);
  when too_many_rows then
      dbms_output.put_line(太多记录);
  when value_error then
      dbms_output.put_line(类型转换异常);
  when others then
      dbms_output.put_line(其他异常);
end;
-- 3 存储过程(感觉包含了PL/SQL所有)
--3.1 根据员工编号得到员工的年薪
create or replace procedure getYearSal(eno in number,yearsal out number)
as --声明变量
begin --过程化语句
      select sal*12+nvl(comm,0) into yearsal from emp
      where empno=eno;
end;
--访问单值输出的存储过程
declare
      yearsal number;
begin
  getYearSal(7499,yearsal);
  dbms_output.put_line(年薪||yearsal);
end;
--3.2 给某员工涨工资(打印涨前和涨后工资)
create or replace procedure updateSal(eno in number,plussal in number)
is --声明变量
   oldsal number;
   newsal number;
begin --过程化语句
      --涨前
      select sal into oldsal from emp where empno=eno;
      dbms_output.put_line(涨前的工资:||oldsal);
      --涨工资
      update emp set sal=sal+plussal where empno=eno;
      commit;
      --涨后
      select sal into newsal from emp where empno=eno;
      dbms_output.put_line(涨后的工资:||newsal);
end;
--访问存储过程
--方法一
declare
begin
  updateSal(7499,888.88);
end;
--方法二 访问只有输入的存储过程也可以使用call
call updateSal(7499,888.88);
--3.3 得到某部门所有员工的信息
create or replace procedure getEmps(dno in number,emps out sys_refcursor)
is --声明变量

begin --过程化语句
      open emps for select * from emp where deptno=dno;   
end;
--访问存储过程
-- 访问输出参数为游标的存储过程
declare
  emps sys_refcursor;
  prow emp%rowtype;
begin
  getEmps(20,emps);
  --打开游标在存储过程中了
  loop
    fetch emps into prow;
    exit when emps%notfound;
    dbms_output.put_line(prow.empno||-||prow.job||-||prow.sal||-||prow.ename);
  end loop;
  close emps;
end;
--对比普通游标的使用,不能使用for循环
declare
  cursor emps is select * from emp where deptno=20;
  k number;
begin
  for k in emps loop
    dbms_output.put_line(k.empno||-||k.job||-||k.sal||-||k.ename);
  end loop;
end;


-- 4 触发器
-- 星期三不能插入数据
create or replace trigger notInsertPerson
before
insert
on emp
for each row
declare
 weekend varchar2(20);
begin
  select to_char(sysdate,day) into weekend from dual;
  if trim(weekend)=(wednesday) then
    raise_application_error(-20003,不能在周三办理入职);
  end if;
  
end;
select to_char(sysdate,d) from dual;

insert into emp (empno,ename,job,mgr,hiredate,sal,comm,deptno)
VALUES(8943,MLDN,MANAGER,7369,sysdate,2000,500,40);

/*综合练习:每一位雇员都要根据其收入上缴所得税,假设所得税的上缴原则为:
--2000以下上缴3%、
--2000 ~ 5000上缴8%、5000以上上缴10%,现在要求建立一张新的数据表,
--可以记录出雇员的编号、姓名、工资、佣金、上缴所得税数据,
--并且在每次修改雇员表中sal和comm字段后可以自动更新记录。*/
  

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读