Write a PL/SQL function to concatenate first name and last name of an employee. Pass employee id as an input to the function CONCAT_NAME.
Table: EMPLOYEE
COLUMN | DATATYPE | CONS. |
---|---|---|
EMPID | NUMBER(5) | PK |
FIRST_NAME | VARCHAR2(25) | NOT NULL |
LAST_NAME | VARCHAR2(25) | |
DEPT | VARCHAR2(25) |
Functional Requirement: FUNCTION CONCAT_NAME(v_id employee.emp_id%type)
(Hint: Data is case sensitive. Use '/' to terminate the PLSQL block)Step-by-Step Guide to Creating a PL/SQL Function to Concatenate First and Last Names
Start with the Function Declaration: Begin by using the CREATE OR REPLACE FUNCTION statement to name your function, CONCAT_NAME. Specify one parameter: v_id (employee ID) as the input type from the EMPLOYEE table.
Define the Return Type: Indicate that your function will return a string using the RETURN VARCHAR2 clause.
Begin the Function Body: Use the IS keyword to start the function body.
Declare a Variable: Inside the function, declare a variable name to hold the concatenated result. Use a VARCHAR2 with a length of 50.
Implement the Logic: Inside the BEGIN and END; keywords, write the logic to concatenate the first and last names. Select FIRST_NAME and LAST_NAME from the EMPLOYEE table where EMP_ID matches the input v_id, and store the result in the name variable.
Return the Result: Use the RETURN statement to output the concatenated name.
By following the steps above, you have successfully created a PL/SQL function, CONCAT_NAME, which concatenates an employee's first and last names based on a given employee ID. This efficient and reusable function can be integrated into larger PL/SQL programs to manage and display employee names with ease.
Leave a comment