PLSQL

简单的输出

declare --简单的在Output输出
begin dbms_output.put_line('asdasd');
end;

定义基本类型变量和赋值

自定义变量个自定义值

 i varchar2(20) :='hello plsql';

手动赋值

age number :=&aaa;--弹出对话框为age赋值

通过查询表定义变量,赋值

 declare 
vsal emp.sal%type;--定义变量属性个emp表的sal字段是一个类型
begin 
  select sal into vsal from emp where empno=7369;--查询出sal并赋值给vsal
  dbms_output.put_line(vsal);--打印vsal
  end;

定义对象变量

declare 
vrow emp%rowtype;--定义对象类型为emp的所有字段
begin
 select * into vrow from emp where empno=7369;--把查询到一条数据赋值给vrow
 dbms_output.put_line(vrow.ename || '_______' || vrow.sal);--输出
end;

if else

declare 
age number :=20;
begin
  if age<=18 then
    dbms_output.put_line('未成年');
    elsif age >18 and age <24 then
      dbms_output.put_line('青年');
elsif age >=24 and age <40 then
  dbms_output.put_line('中年');
else
    dbms_output.put_line('老年');
end if;
end;

循环

普通循环

declare 
begin 
  for i in  1..10 loop
      dbms_output.put_line(i);
end loop;
end;

反转循环

从10开始循环到1

多加了一个reverse

declare 
begin 
  for i in reverse 1..10 loop
      dbms_output.put_line(i);
end loop;
end;

自定义结束条件循环

declare 
i number :=1;
begin
  loop
      dbms_output.put_line(i);
  i := i+1;
  exit when i>10;--设置结束条件
  end loop;
end;

游标

作用:封装查询多条语句,可以进行逐行操作

普通声明

declare
cursor cur_emp is select  *from emp;--声明一个游标
vrow emp%rowtype;--声明变量
begin
  open cur_emp;
  loop
  fetch cur_emp into vrow;--提取一行数据
  dbms_output.put_line(vrow.ename || vrow.sal);
  exit when cur_emp%notfound;--结束条件
  end loop;
  close cur_emp;
end;

打开游标时给游标赋值

--查询10号部门的员工
declare 
cursor cur_emps(vdeptno number ) is select *from emp where deptno= vdeptno;--声明游标的时候,给游标后面加变量
vrow emp%rowtype;
begin
open cur_emps(10);  --给有标的变量赋值
loop
fetch cur_emps into vrow;
exit when cur_emps%notfound;--提取一行数据
dbms_output.put_line(vrow.ename || '__________'|| vrow.sal);
end loop;
close cur_emps;
end;

for循环遍历游标

--fro循环遍历游标
declare 
cursor cur_emp is select *from emp;
begin
for vrow in cur_emp loop                    --for循环自动声明vrow的类型
  dbms_output.put_line(vrow.ename);
end loop;
end;

系统引用型游标

--系统引用型游标
declare 
    cur_emps sys_refcursor;--声明一个游标
    vrow emp%rowtype;
    begin
      open cur_emps for select *from emp;
      loop
        fetch cur_emps into vrow;
      exit when cur_emps%notfound;
      dbms_output.put_line(vrow.ename);
        end loop;
        close cur_emps;
end;

例外(异常)

常见例外

 	   zero_divide : 发生了除零异常
       value_error: 类型转换异常
       no_data_found: 空指针异常,没有找到数据
       too_many_rows : 查询出多行记录,但是赋值给了单行变量

简单实现

declare
i number;
begin
  i :=5/0;
exception
  when zero_divide then   --当发生除零异常时
    dbms_output.put_line('发生了除零异常');
end;

存储过程(可执行的代码片段)

实际上就是将一段编译好了的PLSQL代码片断,封装在数据库服务器中

1,可以方便直接调用

  1. 能够提高执行效率

  2. 提高代码的复用性

    创建一个存储过程的方法

    格式

    --存储过程
    create or replace procedure 方法名(参数名 in 参数类型,参数名 in 参数类型)--创建一个方法,需要传入两个参数(当前传入的是员工的编号和员工的工资)
    is
    currsal number;--声明变量的地方
    begin
    -- 要执行的sql语句
    commit;--提交
    end;

--存储过程
create or replace procedure pro_addsal(vempno in number,vcount in number)--创建一个方法,需要传入两个参数(当前传入的是员工的编号和员工的工资)
is 
currsal number;--声明一个变量
begin
  select sal into currsal from emp where empno=vempno;--查询传入的员工编号的当前工资
dbms_output.put_line('涨薪前:'||currsal);--打印涨薪前的工资
update emp set sal=currsal+vcount where empno=vempno;--执行涨薪语句
dbms_output.put_line('涨薪后:'||(currsal+vcount));--打印涨薪后的工资
commit;--提交
end;

执行

第一种执行方式(简单常用)

call pro_addsal(7369,100);

第二中执行方式

declare
begin
   proc_addsal(7369,-100);
end;

存储函数

函数有返回, 过程没有返回值

函数存在得到意义是给过程去调用的

创建

格式

create or replace function 函数名(vempno number) return 返回类型
is
yearsal number;--生命变量的地方
begin
执行sql语句的地方
return yearsal;--返回参数
end;

--查询员工的年薪的函数
create or replace function fun_getYearSal(vempno number) return number--传入员工编号,返回年薪
is
yearsal number;
begin 
  select sal*12+nvl(comm,0) into yearsal from emp where empno=vempno;
  return yearsal;
end;

执行存储函数

plsql代码片段执行

--调用
declare 
vyearsal number;
begin
  vyearsal := fun_getYearSal(7369);
  dbms_output.put_line(vyearsal);
  end;

sql语句执行

select  fun_getYearSal(7369)from dual;
原文地址:https://www.cnblogs.com/AngeLeyes/p/7380640.html