plsql - 在 PL/SQL 中连接两个表并获得所需的结果
问题描述
数据库中两个表的结构如下。
1) 部门表:Dept_Id number(5) 主键,Sept_Name varchar2(20),Employee_strength number(4) 不为空。
2)Employee 表:E_Id number(5), E_Name varchar2(20), Designation varchar2(20), D_ID number(5) 参考Department 表Dept_ID。
将写入一个 pl/sql 程序块来打印具有指定为“SE”的员工的部门的名称,如果在部门表中没有找到满足给定条件的记录,则代码应打印消息“未找到记录”如果记录发现代码必须打印部门名称。
请帮忙。
解决方案
这是一个选项(基于类似于您的表格;这些属于 Scott)。
我会找一个推销员。
SQL> break on deptno
SQL> select distinct d.deptno, e.job
2 from dept d left join emp e on e.deptno = d.deptno
3 order by d.deptno, e.job;
DEPTNO JOB
---------- ---------
10 CLERK
MANAGER
PRESIDENT
20 ANALYST
CLERK
MANAGER
30 CLERK
MANAGER
SALESMAN --> only department 30 has SALESMEN
40
10 rows selected.
SQL>
PL/SQL 块:
SQL> set serveroutput on
SQL> declare
2 l_exists number(1);
3 begin
4 for cur_d in (select d.deptno, d.dname from dept d order by d.deptno) loop
5 select max(1)
6 into l_exists
7 from emp e
8 where e.deptno = cur_d.deptno
9 and e.job = 'SALESMAN';
10
11 dbms_output.put_line(cur_d.deptno || ' - ' ||
12 case when l_exists = 1 then cur_d.dname
13 else 'no record found'
14 end);
15 end loop;
16 end;
17 /
10 - no record found
20 - no record found
30 - SALES
40 - no record found
PL/SQL procedure successfully completed.
SQL>
推荐阅读
- android - 从 kotlin 中的另一个类文件访问文本视图
- python - 训练和验证 LSTM 问题:Precision & Recall 问题
- html - 使用 HTML 限制适当的十进制数字,但没有字母
- vue.js - 无法从 Socket.io 获得响应
- laravel - 如果在 laravel 中未验证电子邮件,请在注册后 2 天内从数据库中删除用户
- android - 我如何在 android studio 中编写:如果条件至少在 5 秒内为真,那么做点什么?
- laravel - 我的vue组件显示白字几乎就像没有打开一样,可能是什么问题
- java - Java 应用程序即使在成功执行后也不会终止
- reactjs - 使用表单验证器对 ReactJs 中的选项卡进行表单验证
- git - 是否可以只合并主存储库中的一个子存储库(子树)?