MySQL Tutorial/Procedure Function/Default

Материал из SQL эксперт
Перейти к: навигация, поиск

Declare variable with default value

mysql>
mysql> delimiter $$
mysql>
mysql> CREATE PROCEDURE myProc()
    -> BEGIN
    ->     DECLARE my_pi          FLOAT          /* Floating point number*/
    ->             DEFAULT 3.1415926;            /* initialized as PI */
    ->
    ->     select "my_pi="+my_pi;
    -> END$$
Query OK, 0 rows affected (0.00 sec)
mysql> delimiter ;
mysql>
mysql> call myProc();
+----------------+
| "my_pi="+my_pi |
+----------------+
| 3.141592502594 |
+----------------+
1 row in set (0.00 sec)
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql>
mysql> drop procedure myProc;
Query OK, 0 rows affected (0.00 sec)
mysql>
mysql>


Integer variable with default value

mysql>
mysql> delimiter $$
mysql> CREATE PROCEDURE myProc()
    -> BEGIN
    ->
    ->     DECLARE l_int1      INT DEFAULT -2000000;
    ->
    ->     select l_int1;
    ->
    -> END$$
Query OK, 0 rows affected (0.02 sec)
mysql> delimiter ;
mysql>
mysql> call myProc();
+----------+
| l_int1   |
+----------+
| -2000000 |
+----------+
1 row in set (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
mysql>
mysql> drop procedure myProc;
Query OK, 0 rows affected (0.00 sec)
mysql>