MySQL BEGIN


 

MySQL BEGIN

This example illustrates how to use the BEGIN keyword, when we create function.

This example illustrates how to use the BEGIN keyword, when we create function.

MySQL BEGIN

This example illustrates how to use the BEGIN keyword, when we create function.

In this example we create a table 't2' and function 'f1' which return integer. Now we start to insert value in the table but before insertion we execute a BEGIN command which return the value which is inserting in the table.

In the below query we execute a query for create table, create function, select function and select table and the output display in the table.

 

 

Query

 

CREATE TABLE t2 (c1 INT);
 
Query

 

DELIMITER //
  CREATE FUNCTION f1(p1 INT) RETURNS INT
  BEGIN
  INSERT INTO t2 VALUES (p1);
  RETURN p1;
  END //
DELIMITER ;

 

Query

 

select f1(4);
 

 

Output

 

+-------+
| f1(4) |
+-------+
| 4     |
+-------+

 

Query

 

select * from t2;
 
Output

 

+------+
| c1   |
+------+
| 4    |
+------+

 

Ads