PL/SQL functions [duplicate]

泄露秘密 提交于 2019-12-11 18:44:06

问题


Possible Duplicate:
Homework on PL/SQL FUNCTIONS
plsql functions

Function:

  1. function to Display_Employee_Name_In_Uppercase that accepts the Employee_ID from the Empoyees table and returns the first and the last name of the employee in uppercase.
  2. Write a small PL/SQL program to display the names of the employees whose Employee_IDs are 107, 200 and 205.

this is what I have done I didnt know how to complete it can help ?

CREATE OR REPLACE FUNCTION disp (emp_id in  varchar20) return emp_name
select into emp_name 
fname||lname 
from employees 
where employee_id=emp_id
END disp ;

回答1:


Something like this...

CREATE OR REPLACE 
FUNCTION Fn_Display(p_empId IN VARCHAR2)
RETURN VARCHAR2

IS

empName VARCHAR2(100);    

BEGIN

 BEGIN
  SELECT UPPER(first_name || ' ' || last_name)
  INTO  empName
  FROM Employees
  WHERE employee_id = p_empId; 
 EXCEPTION 
    WHEN NO_DATA_FOUND THEN
       RAISE NO_DATA_FOUND                
 END;


 RETURN empName;

END Fn_Display;

You can call this function wherever you want. here is a sample...

  DECLARE

    empId VARCHAR2(100);
    empName VARCHAR2(100);

  BEGIN

    empId := &ENTER_EMPLOYEE_ID;
    empName := Fn_Display(empId);

    DBMS_OUTPUT.PUT_LINE('Employee Name: '||empName);

  END;



回答2:


You could try this code, maybe this one works for you:

CREATE OR REPLACE FUNCTION disp (emp_id in  varchar2) return varchar2 IS
emp_name varchar2(256);
BEGIN
  select UPPER(fname || ' ' || lname) 
  into emp_name  
  from employees 
  where employee_id = emp_id;
  return emp_name;
END disp;


来源:https://stackoverflow.com/questions/10512495/pl-sql-functions

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!