can have zero or more SQL statements with BEGIN ... END statement. *No statement without BEGIN ... END
statement gets error.
can have only single statement without BEGIN ... END
statement not changing the delimiter to $$
without error.
can have multiple parameters with IN
, OUT
and INOUT
and without them.
can get zero or more values with zero or more IN
and INOUT
parameters from the caller.
can return zero or more values to the caller with OUT
and INOUT
parameters.
can have local variables and user-defined variables. *My answer explains local variables and user-defined variables.
can have transaction.
cannot have RETURN statement otherwise there is the error.
is not atomic by default so if there is error, it is not rollbacked automatically. *My answer explains it in detail.
*In this answer, I explain how to create a procedure with an IN
parameter and my answer and my answer explain OUT
and INOUT
parameters respectively and the doc explains a procedure in detail.
For example, you create test
table as shown below:
CREATE TABLE test (
num int
);
Then, you insert the row whose num
is 2
as shown below:
INSERT INTO test (num) VALUES (2);
Now, you can create addition(IN value INT)
procedure which adds value
to num
as shown below. *The IN
parameter IN value INT
can get the value from the caller and you can also use value INT
without IN
which is also recognized as an IN
parameter and basically, you need to change the default delimiter ;
to something like $$
when creating a procedure otherwise there is error, then after creating a procedure, you need to change the delimiter $$
back to ;
as shown below and my answer explains delimiter and you must select a database when creating a procedure otherwise there is the error:
DELIMITER $$
CREATE PROCEDURE addition(IN value INT)
BEGIN
UPDATE test SET num = num + value;
END$$
DELIMITER ;
DELIMITER $$
CREATE PROCEDURE addition(INOUT v1 INT, INOUT v2 INT)
BEGIN
SELECT v1+1, v2+1 into v1, v2;
END$$
DELIMITER ;
Then, you can call addition(3)
with CALL statement, then 3
is added to num
as shown below:
mysql> CALL addition(3);
...
mysql> SELECT num FROM test;
+------+
| num |
+------+
| 5 |
+------+
Or, you can set the user-defined session variable e.g. @v
with 3
, then 3
is added to num
as shown below. *The initial value of a user-defined session variable is NULL
:
mysql> SET @v = 3;
...
mysql> CALL addition(@v);
...
mysql> SELECT num FROM test;
+------+
| num |
+------+
| 5 |
+------+
In addition, you can create addition()
procedure with the user-defined session variable e.g. @v
and without an IN
parameter as shown below:
DELIMITER $$
CREATE PROCEDURE addition()
BEGIN -- ↓↓ Here
UPDATE test SET num = num + @v;
END$$
DELIMITER ;
Then, you set @v
with 3
and call addition()
, then 3
is added to num
as shown below:
mysql> SET @v = 3;
...
mysql> CALL addition();
...
mysql> SELECT num FROM test;
+------+
| num |
+------+
| 5 |
+------+
And, in this case below, you can create addition(IN value INT)
procedure not changing the delimiter to $$
without error:
CREATE PROCEDURE addition(IN value INT)
UPDATE test SET num = num + value;