博客
关于我
19个实例学会plsql
阅读量:797 次
发布时间:2023-04-04

本文共 3694 字,大约阅读时间需要 12 分钟。

PL/SQL学习实例与代码示例

本文将为大家展示一系列与PL/SQL相关的代码示例,涵盖了变量声明、流程控制、函数与存储过程的实现,以及数据库操作等内容。这些示例将帮助开发者更好地理解PL/SQL的基本语法和实际应用场景。

1. 变量与运算

变量声明

declare    age number default 90;  -- 默认年龄    height number := 175; -- 默认身高begin    dbms_output.put_line('年龄'||age||'身高'||height);  -- 输出年龄和身高end;

变量运算

declare    age number default 90;  -- 默认年龄    height number := 175; -- 默认身高begin    dbms_output.put_line('年龄'||age||'身高'||height);  -- 输出当前年龄和身高    age := age + 20;       -- 增加20岁    dbms_output.put_line('20年后年龄'||age||'岁');  -- 输出20年后的年龄end;

2. 流程控制

条件判断

declare    age number default 90;  -- 默认年龄    height number := 175; -- 默认身高begin    if age > 70 then        dbms_output.put_line('古稀之年');    else        dbms_output.put_line('风华正茂');    end if;end;

多重条件判断

declare    age number default 90;  -- 默认年龄    height number := 175; -- 默认身高    gender char(2) := '男';  -- 性别,默认为男begin    if gender = '男' then        dbms_output.put_line('你可以和女性结婚');    end if;    if height > 170 then        dbms_output.put_line('可以打篮球');    else        dbms_output.put_line('可以踢足球');    end if;    if age < 20 then        dbms_output.put_line('年轻小伙');    elsif age <= 50 then        dbms_output.put_line('年轻有为');    elsif age <= 70 then        dbms_output.put_line('安享天伦');    else        dbms_output.put_line('佩服佩服');    end if;end;

3. 函数与存储过程

存储过程

create function qiuhe(n number) return numberis    begin        if n > 1 then            return n + qiuhe(n - 1);        else            return 1;        end if;    end;begin    dbms_output.put_line(qiuhe(10));end;

存储函数

create function mian(a number, b number) return numberis    begin        area := a * b;        return area;    end;begin    dbms_output.put_line(mian(5, 4));    dbms_output.put_line(mian(6, 7));    dbms_output.put_line(mian(3, 7));end;

4. 数据库操作

查询数据

declare    depart dept%rowtype;  -- 表 dept 的记录类型    total_sal number;    -- 总薪水类型    total_comm number;   -- 总奖金类型begin    -- 查询部门信息    procedure deptinfo(dno number)    is        begin            select dname, loc into depart.dname, depart.loc from dept where deptno = dno;            select sum(sal), sum(comm) into total_sal, total_comm from emp where deptno = dno;            dbms_output.put_line('部门名称:' || depart.dname || '在' || depart.loc);            dbms_output.put_line('这个部门每月工资及奖金各是' || total_sal || '和' || total_comm);        end;    begin        deptinfo(80);        deptinfo(30);    end;end;

异常处理

declare    depart dept%rowtype;  -- 表 dept 的记录类型    total_sal number;    -- 总薪水类型    total_comm number;   -- 总奖金类型begin    -- 查询部门信息    procedure deptinfo(dno number)    is        begin            select dname, loc into depart.dname, depart.loc from dept where deptno = dno;            select sum(sal), sum(comm) into total_sal, total_comm from emp where deptno = dno;            dbms_output.put_line('部门名称:' || depart.dname || '在' || depart.loc);            dbms_output.put_line('这个部门每月工资及奖金各是' || total_sal || '和' || total_comm);        end;    begin        deptinfo(80);        deptinfo(30);    end;    exception        when NO_DATA_FOUND then            dbms_output.put_line('没有数据');        when others then            dbms_output.put_line('其他错误');    end;end;

5. 触发器

表级触发器

create trigger t3 after delete on goodsis    begin        dbms_output.put_line('有人触发我');    end;

行级触发器

create trigger t4 after delete on goods for each rowis    begin        dbms_output.put_line('有人触发我');    end;

事件触发器

create trigger t5 before insert on o for each rowis    begin        select cnt into tmp from g where gid = :new.gid;        if :new.much > tmp then            :new.much := tmp;        end if;        update g set cnt = cnt - :new.much where gid = :new.gid;    end;

以上代码示例涵盖了PL/SQL的基础知识与实际应用,适合开发者在日常工作中参考与学习。

转载地址:http://nsrfk.baihongyu.com/

你可能感兴趣的文章