Oracle函数:查询员工姓名和部门名称
'I apologize for the confusion. It is possible that there is an error in the scott.emp or scott.dept tables, causing the function to return the same data regardless of the input parameter value. You may want to check the data in those tables to ensure that it is correct. Alternatively, you can modify the function to use a join statement to retrieve the employee name and department name in a single query, like this:
CREATE OR REPLACE FUNCTION get_emp_details(empno IN NUMBER) RETURN VARCHAR2 IS emp_name VARCHAR2(50); dept_name VARCHAR2(50); BEGIN SELECT e.ename, d.dname INTO emp_name, dept_name FROM scott.emp e JOIN scott.dept d ON e.deptno = d.deptno WHERE e.empno = empno; RETURN emp_name || ' works in ' || dept_name || ' department'; EXCEPTION WHEN NO_DATA_FOUND THEN RETURN 'No employee found with employee number ' || empno; END;
This modified function uses a join statement to retrieve the employee name and department name in a single query. It also includes an exception handler to catch the case where no data is found for the given employee number. Please try executing the modified function and let me know if you have any further questions.'
原文地址: https://www.cveoy.top/t/topic/nxV7 著作权归作者所有。请勿转载和采集!