19个实例学会plsql
发布日期:2025-06-07 21:09:14
浏览次数:3
分类:精选文章
本文共 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的基础知识与实际应用,适合开发者在日常工作中参考与学习。
发表评论
最新留言
表示我来过!
[***.240.166.169]2026年06月02日 12时59分32秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
php 生成证书 签名及验签
2023-02-28
PHP 的标准输入与输出
2023-02-28
php 笔记 (早前的,很乱)
2023-02-28
PHP 第一天
2023-02-28
Redis使用量暴增,快速定位有哪些大key在作怪
2023-02-28
PHP 统计数据功能 有感
2023-02-28
SpringBoot处理JSON数据
2023-02-28
PHP 输入输出流合集
2023-02-28
php-兔子问题,斐波那契数列
2023-02-28
php-约瑟夫问题
2023-02-28
php.ini中常见的配置信息选项
2023-02-28
php.ini配置中有10处设置不当,会使网站存在安全问题
2023-02-28
PHP7 新特性
2023-02-28
PHP7+MySQL5.7+Nginx1.9. on Ubuntu 14.0
2023-02-28
php7.1.6 + redis
2023-02-28
php7中使用php_memcache扩展
2023-02-28
PHP7中十个需要避免的坑
2023-02-28
php7和PHP5对比的新特性和性能优化
2023-02-28
PHP7安装pdo_mysql扩展
2023-02-28