DawnCHENXI

 

 

------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥-------------

 

 

PL/SQL

PL/SQL(Procedural Language):过程化sql语言!

在原本的sql语句之上,再增加一些逻辑或者循环等操作。

1:基本语法

01.declare     可选部分    声明:变量,异常,游标......

02.begin      必选部分 

03.exception   可选部分     针对于异常的处理

04.end        必选部分 

2:小例子

-- 直接在输出台中显示内容

begin
dbms_output.put_line('真的很神奇。');
end;

 

-- 查询1002老师编号 ,我们得到对应的老师姓名和薪水

declare  --声明变量

  v_tname   teacher.tname%type;
  v_sal     teacher.sal%type;
  v_result   varchar2(30);
begin  -- 开始pl/sql编程

   select tname,sal into v_tname,v_sal   from teacher where tno=1002;
dbms_output.put_line('姓名是==》'||v_tname);  --  ||是拼接字符串
dbms_output.put_line('薪水是==》'||v_sal);

  if v_sal<=30000  then 
    v_result:='薪水是一般化'||v_sal;   --   :=是赋值操作

  elsif   v_sal>30000 and  v_sal<=40000 then
    v_result:='薪水是一般以上'||v_sal;

  else 
     v_result:='薪水可以了'||v_sal;   

  end  if;
   dbms_output.put_line(v_result); 

end;

  3:case

-- 直接在输出台中显示内容

begin
dbms_output.put_line('真的很神奇。');
end;

 

-- 查询1002老师编号 ,我们得到对应的老师姓名和薪水

declare  --声明变量
  v_tname   teacher.tname%type;
  v_sal     teacher.sal%type;
  v_result   varchar2(30);

begin  -- 开始pl/sql编程
   select tname,sal into v_tname,v_sal   from teacher where tno=1002;
dbms_output.put_line('姓名是==》'||v_tname);  --  ||是拼接字符串
dbms_output.put_line('薪水是==》'||v_sal);

  if v_sal<=30000  then 
    v_result:='薪水是一般化'||v_sal;   --   :=是赋值操作

  elsif   v_sal>30000 and  v_sal<=40000 then
    v_result:='薪水是一般以上'||v_sal;

  else 
     v_result:='薪水可以了'||v_sal;   

  end  if;
   dbms_output.put_line(v_result); 

end;

 

--  case 块

declare 
  num1 varchar2(20);
  v_result  varchar2(20);   --声明了2个变量

begin
  num1:='大家辛苦了20';   --  给num1赋值

  case num1               -- case开始
    when '大家辛苦了1' then
      v_result:='1'; 

    when '大家辛苦了2' then
      v_result:='2'; 

    when '大家辛苦了3' then
     v_result:='3'; 

     else
       v_result:='默认的'; 

  end  case;              -- case结束
   dbms_output.put_line(v_result);   -- 输出结果

end;

 

4:循环结构

-- 循环结构 do_while

declare
i number;

begin
i:=1; --初始化变量

loop --开始循环

dbms_output.put_line(i); --循环体

i:=i+1;

exit when i>20; -- 循环条件

end loop; --结束循环

end;

-- while循环

declare
  i number;

begin
  i:=1;   --初始化变量

  while i<20  loop
    dbms_output.put_line(i);   --循环体
    i:=i+1;
  end loop;  

end;  

-- for循环

declare
  i number;

begin
 for i in 1..20
   loop
        dbms_output.put_line(i);   --循环体
   end loop;
end;

 

5:函数

-- 函数

create or replace function fn_teacher_tid
(f_tid varchar2)
return  varchar2
is  
f_result teacher.tid%type;

begin
    --判断身份证格式是否正确
    if length(f_tid)!=18 then
      dbms_output.put_line('身份证格式不正确!');  

    else
      dbms_output.put_line('身份证格式正确!');

       --给返回值赋值
      f_result:=substr(f_tid,1,6)||'********'||substr(f_tid,15); 

    end if;  
    return f_result;

end   fn_teacher_tid; -- 函数结束

    

-- 调用函数

select fn_teacher_tid(11010119910815477) from dual;

 

6:游标

游标概念

01.是一个数据的缓存区,存放的是sql语句执行的结果集;

02.是一个能从多条数据结果集中每次获取一条记录的机制!

作用

 01.游标可以反复使用,减少访问数据库的次数

 02.大大提高我们的检索效率

分类

 01.隐式游标: 我们执行dml操作时,数据库自动创建  ==?名字 sql

 02.显示游标: 可以返回多行数据的查询结果

03.REF游标: 用于处理运行时才能确定的动态sql

 

游标常用的属性

01. %found        是否还有数据   boolean类型的值

02. %notfound     是否没有数据   boolean类型的值

03.%rowcount     sql语句影响的行数

04.%isopen        判断游标是否打开

 

 --游标(显示游标)

declare
 c_tname  teacher.tname%type;
 cursor  tname_cursor is select tname from teacher; -- 声明游标

begin
  open  tname_cursor; --打开游标
   fetch tname_cursor into c_tname;  -- 给变量赋值

  --循环输出教师名称
  while tname_cursor%found loop
     dbms_output.put_line('老师的姓名'||c_tname);
     fetch tname_cursor into c_tname;  -- 给变量赋值
  end  loop;

  close tname_cursor; --关闭游标
end;

7:存储过程

定义

01.就是一组用于完成特定数据库功能的sql语句的集合;

02.经常被使用;

03.sql语句的集合必须编译成功后才能存储在数据库系统中。

 

组成部分:

   01.声明  变量:输入的变量,输出的变量

   02.执行

   03.异常处理

-- 创建一个新增部门信息的  存储过程

create or replace procedure  pro_dept_add
(
p_deptno dept.deptno%type,
p_dname dept.dname%type,
p_loc dept.loc%type
)

is e_deptno exception;  -- 声明存储过程中可能出现的异常
begin
    if p_deptno<10 then
      raise e_deptno;   --抛出异常

    end if;
   --新增部门信息
   insert into dept(deptno,dname,loc)
   values (p_deptno,p_dname,p_loc);

   --手动提交事务
   commit;

exception     -- 对异常进行处理
    when e_deptno then
       dbms_output.put_line('部门编号不合法');

    when others then
       dbms_output.put_line('其他的异常');

end pro_dept_add;

-- 调用存储过程

call pro_dept_add(10,'新增部门','一楼');

8:触发器

定义

   01.在创建触发器的时候,规定了触发的时机

   02.人为的不能去调用

组成部分

  01.触发器名

  02.触发时间

  03.before:在数据库动作执行之前

  04.after:在数据库动作执行之后

  05.insert

  06.update

  07.delete

  08.表名: 触发器所在的表

  09.for each row

属性

  :old  代表修改之前的值

  :new 代表修改之后的值

只能在.for each row中使用

Insert   ====>:new

Delete  ====>:old

Update  ====>:old  :new

 

-- 创建一个序列

create sequence sq_teacherlog_logid
minvalue 1
maxvalue 9999999
start with 1
increment by 1
cache 50;

-- 创建一个表 用来 监听 teacher表的变化

create  table  teacher_log(
logid  number not null primary key,    
log_type varchar2(10) not null,
log_time date not null,
log_data  varchar2(50)
)

-- 创建一个触发器

create  or replace trigger tr_teacher
after insert or update or delete    -- 触发时机
on  teacher for each row

declare
t_type teacher_log.log_type%type;
t_time teacher_log.log_time%type;
t_data teacher_log.log_data%type;

begin
  if inserting then  --新增
    t_type:='insert';
    t_data:=:new.tno||'===='||:new.sal;

  elsif deleting then  -- 删除  
    t_type:='delete'; 
    t_data:=:old.tno||'===='||:old.sal;

  else
    t_type:='update'; 
    t_data:=:old.tno||'===='||:old.sal||'===='||:new.sal;

  end if;

  -- 将用户操作的数据保存到 teacher_log

 

insert into teacher_log
  values(sq_teacherlog_logid.nextval,t_type,sysdate,t_data);
end tr_teacher;

-- 新增数据到teacher

insert into teacher(tno,sal) values(1200,20000);

-- 修改上面的新增数据

update teacher  set sal=40000 where tno=1200;

-- 删除上面的数据

delete from teacher where tno=1200;

 

 

 

原作者:晨曦Dawn

博客地址:https://www.cnblogs.com/DawnCHENXI/p/9073382.html

转载请注明出处!!!!!!!如果有错误请指出,会更改,感激不尽

分类:

技术点:

oracle

相关文章: