MySQL BEGIN ... END Compound Statement

The BEGIN ... END syntax is used to create a compound statement. These compound statements contain a multiple set of statements. These statement starts with BEGIN and ends with END statements. Each statement in a compound statement ends with a semi colon (;) or the current statement delimiter.
stored procedures and functions, triggers, and events are the compound statement in MySQL. You can write a BEGIN ... END block with in another and we can also label these statements.
A compound statement can contain other blocks such as variable declarations, conditions, cursors, including loops and conditional tests.
Following is the syntax of the BEGIN ... END Compound Statement −
[begin_label:] BEGIN
   [statement_list]
END [end_label]

Assume we have created a table named Employee in MySQL database using the CREATE TABLE as shown below −
mysql> CREATE TABLE Employee(Name VARCHAR(255), Salary INT NOT NULL, Location VARCHAR(255));
Query OK, 0 rows affected (2.74 sec)
Following is an example of a MySQL stored procedure. Here we are inserting a column in to the Employee table taking values from user. (through input parameters)
mysql> DELIMITER //
mysql> Create procedure myProcedure (
   IN name VARCHAR(30),
   IN sal INT,
   IN loc VARCHAR(45))
   BEGIN
      INSERT INTO Employee(Name, Salary, Location) VALUES (name, sal, loc);
   END // 
Query OK, 0 rows affected (0.20 sec)
mysql> DELIMITER ;
Following statement calls the above created stored procedure.
CALL myProcedure ('Raman', 35000, 'Bangalore');
Query OK, 1 row affected (0.45 sec)
If you retrieve the contents of table, you can observe the newly inserted row as shown below −
mysql> select * from employee;
+-------+--------+-----------+
| Name  | Salary | Location  |
+-------+--------+-----------+
| Raman | 35000  | Bangalore |
+-------+--------+-----------+
1 row in set (0.00 sec)
Suppose we have created a table named Emp in the database using the CREATE statement as shown below −
mysql> CREATE TABLE Emp(Name VARCHAR(255), DOB DATE, Location VARCHAR(255));
Query OK, 0 rows affected (2.03 sec)
And we have inserted three records in the Emp table as −
mysql> INSERT INTO Emp VALUES ('Amit', DATE('1970-01-08'), 'Hyderabad');
mysql> INSERT INTO Emp VALUES ('Sumith', DATE('1990-11-02'), 'Vishakhapatnam');
mysql> INSERT INTO Emp VALUES ('Sudha', DATE('1980-11-06'), 'Vijayawada');
Following query creates a function named getDob()which accepts the name of the employee, retrieves and returns the value of DOB column.
mysql> DELIMITER //
mysql> CREATE FUNCTION test.getDob(emp_name VARCHAR(50))
   RETURNS DATE
   DETERMINISTIC
   BEGIN
      declare dateOfBirth DATE;
      select DOB into dateOfBirth from test.emp where Name = emp_name;
      return dateOfBirth;
   END//
Query OK, 0 rows affected (0.31 sec)
mysql> DELIMITER ;
If you call the function, you can get date of birth of an employee as shown below.
mysql> SELECT getDob('Amit');
+----------------+
| getDob('Amit') |
+----------------+
| 1970-01-08     |
+----------------+
1 row in set (0.15 sec)

    31 Lectures 
     6 hours 

 Eduonix Learning Solutions

    84 Lectures 
     5.5 hours 

 Frahaan Hussain

    6 Lectures 
     3.5 hours 

 DATAhill Solutions Srinivas Reddy

    60 Lectures 
     10 hours 

 Vijay Kumar Parvatha Reddy

    10 Lectures 
     1 hours 

 Harshit Srivastava

    25 Lectures 
     4 hours 

 Trevoir Williams
 Print
 Add Notes
 Bookmark this page