0% found this document useful (0 votes)
4 views3 pages

MySQL UDF and Procedure

The document explains how to create a User-Defined Function (UDF) and a Stored Procedure in MySQL. It provides an example of a UDF that calculates the square of a number and a Stored Procedure for adding employee records to a table. It also includes sample usage and output for both the UDF and Stored Procedure.

Uploaded by

sahasafi26
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views3 pages

MySQL UDF and Procedure

The document explains how to create a User-Defined Function (UDF) and a Stored Procedure in MySQL. It provides an example of a UDF that calculates the square of a number and a Stored Procedure for adding employee records to a table. It also includes sample usage and output for both the UDF and Stored Procedure.

Uploaded by

sahasafi26
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

MySQL User-Defined Function and Stored Procedure

1. User-Defined Function (UDF)

Function Definition:

--------------------

DELIMITER //

CREATE FUNCTION square(num INT)

RETURNS INT

DETERMINISTIC

BEGIN

RETURN num * num;

END //

DELIMITER ;

Usage:

------

SELECT square(6) AS Result;

Output:

-------

+--------+

| Result |

+--------+

| 36 |
+--------+

2. Stored Procedure

Table Definition:

-----------------

CREATE TABLE employees (

id INT AUTO_INCREMENT PRIMARY KEY,

name VARCHAR(100),

salary DECIMAL(10, 2)

);

Procedure Definition:

---------------------

DELIMITER //

CREATE PROCEDURE add_employee (

IN emp_name VARCHAR(100),

IN emp_salary DECIMAL(10,2)

BEGIN

INSERT INTO employees (name, salary)

VALUES (emp_name, emp_salary);

END //

DELIMITER ;

Usage:
------

CALL add_employee('John Doe', 55000.00);

CALL add_employee('Jane Smith', 62000.00);

View Data:

----------

SELECT * FROM employees;

Output:

-------

+----+-------------+----------+

| id | name | salary |

+----+-------------+----------+

| 1 | John Doe | 55000.00 |

| 2 | Jane Smith | 62000.00 |

+----+-------------+----------+

You might also like