编写一个存储过程来进行查找

发布于 2024-12-01 11:35:31 字数 206 浏览 1 评论 0原文

在此处输入图像描述

请帮助我摆脱这种情况

我必须创建一个函数来检索经理的姓名给定的员工。 (我试图将 emp 表连接到自身来执行此操作,但不确定。)我还需要写 一个 SQL select 语句,使用该函数生成员工编号和姓名及其经理姓名的列表,

谢谢

enter image description here

Please help me out from this situation

I have to create a function to retrieve the name of the manager for a given employee. (I was trying to join the emp table to itself to do this but not sure.) Also I need to write
a SQL select statement that uses the function to produce a list of employee numbers and names together with the name of their manager

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

抚笙 2024-12-08 11:35:31

正如您所指出的,解决方案是将 emp 表与其自身连接起来。

CREATE FUNCTION fn_get_manager(p_empno IN NUMBER) 
   RETURN VARCHAR2
   IS manager VARCHAR2;
BEGIN 
   SELECT mgr.name 
     INTO manager 
     FROM emp e, emp mgr 
    WHERE e.mgr = mgr.empno
      AND e.empno = p_empno; 
   RETURN(manager); 
 END;

您可以将此查询包装在用户定义的函数中,其中 @employee 变量成为参数。

要返回员工列表,您可以在 select 语句中调用该函数,

SELECT e.empno, e.name, fn_get_manager(e.empno)
  FROM emp e

尽管这种构造有效,但对于如此简单的请求来说,这是一种糟糕的方法。该函数将对 emp 表中的每条记录执行,与简单的 join 语句相比,结果会非常慢。

 SELECT e.empno, e.name, mgr.name
   FROM emp e,emp mgr 
  WHERE e.mgr = mgr.empno (+)

我在这里使用了左连接来处理“大老板”本身没有经理的边缘情况。

编辑:更新了一些 Oracle 语法的查询

As you indicated, the solution is to join the emp table with itself.

CREATE FUNCTION fn_get_manager(p_empno IN NUMBER) 
   RETURN VARCHAR2
   IS manager VARCHAR2;
BEGIN 
   SELECT mgr.name 
     INTO manager 
     FROM emp e, emp mgr 
    WHERE e.mgr = mgr.empno
      AND e.empno = p_empno; 
   RETURN(manager); 
 END;

This query you can wrap in a user defined function where the @employee variable becomes a parameter.

To return the list of employees you can call the function in the select statement

SELECT e.empno, e.name, fn_get_manager(e.empno)
  FROM emp e

Although this construction works it is a terrible way of doing it for such a simple request. The function will be executed for every record in the emp table and the result will be very slow compared to the simple join statement.

 SELECT e.empno, e.name, mgr.name
   FROM emp e,emp mgr 
  WHERE e.mgr = mgr.empno (+)

I used a left join here to also handle the edge case of the "big boss" which doesn't have a manager himself.

edit: updated queries a bit for oracle syntax

茶花眉 2024-12-08 11:35:31

Filip 的函数 不是最好的从 EMP 表的查询中调用的函数的解决方案。该表具有 MGR 属性,这就是应该传递给函数的属性。

CREATE FUNCTION fn_get_manager(p_mgr IN emp.mgr%type) 
   RETURN VARCHAR2
IS 
   manager emp.ename%type;
BEGIN 
   SELECT ename 
     INTO manager 
     FROM emp e 
    WHERE e.empno = p_mgr; 
   RETURN(manager); 
END;

这些事情的表现已经够糟糕的了,无需在 EMP 表上引入另一次不必要的读取。

但在 Filip 时代,这应该在 SQL 中正确完成,并将 EMP 表连接到其自身。

Filip's function is not the best solution for a function which is called from a query of the EMP table. That table has the MGR attribute and that is what should be passed to the function.

CREATE FUNCTION fn_get_manager(p_mgr IN emp.mgr%type) 
   RETURN VARCHAR2
IS 
   manager emp.ename%type;
BEGIN 
   SELECT ename 
     INTO manager 
     FROM emp e 
    WHERE e.empno = p_mgr; 
   RETURN(manager); 
END;

These things perform badly enough without introducing another unnecessary read on the EMP table.

But as Filip days, this properly should be done in SQL, with a JOIN of the EMP table to itself.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文