SQLize Online / PHPize Online  /  SQLtest Online

A A A
Share      Blog   Popular
Copy Format Clear
CREATE TABLE tbl_employees ( emp_id INT AUTO_INCREMENT PRIMARY KEY, emp_name VARCHAR(50), emp_departmentid INT, emp_salary DECIMAL(10, 2) ); INSERT INTO tbl_employees(emp_name, emp_departmentid, emp_salary) VALUES ('Nicole', 2, 1421.42), ('Michael', 3, 4262.53), ('Vince', 1, 6314.96), ('Kobe', 2, 8361.21), ('Bryan', 3, 3626.64), ('Loki', 1, 1332.12), ('Tomo', 3, 12242.88), ('Vilma', 3, 6327.24), ('Petra', 1, 4829.84); -- Drop the Function DROP FUNCTION IF EXISTS fn_getEmpNameByID; -- Create the Function DELIMITER $$ CREATE FUNCTION fn_getEmpNameByID(_id INT) RETURNS VARCHAR(50) BEGIN DECLARE _EmpName VARCHAR(50); SELECT emp_name INTO _EmpName FROM tbl_employees WHERE _id = emp_id; RETURN _EmpName; END $$ DELIMITER ; -- EXECUTE SELECT fn_getEmpNameByID(1); -- SELECT fn_getEmpNameByID(2); -- SELECT fn_getEmpNameByID(3); -- SELECT fn_getEmpNameByID(4); -- SELECT fn_getEmpNameByID(5); -- SELECT fn_getEmpNameByID(6); -- SELECT fn_getEmpNameByID(10); SELECT * FROM tbl_employees;

Stuck with a problem? Got Error? Ask ChatGPT!

Copy Clear