Why MySQL Statements are Legal in a Procedure Body
什么MySQL语句在存储过程体中是合法的?
什么样的SQL语句在Mysql存储过程中才是合法的呢?你可以创建一个包含INSERT, UPDATE,DELETE, SELECT, DROP, CREATE, REPLACE等的语句。你唯一需要记住的是如果代码中包含MySQL扩充功能,那么代码将不能移植。在标准SQL语句中:任何数据库定义语言都是合法的,如:
CREATE PROCEDURE p () DELETE FROM t; //
SET、COMMIT以及ROLLBACK也是合法的,如:CREATE PROCEDURE p () SET @x = 5; //
CREATE PROCEDURE p () DROP TABLE t; //
CREATE PROCEDURE p () SELECT 'a'; //
CREATE PROCEDURE p1 ()
CREATE PROCEDURE p2 () DELETE FROM t; //
CREATE PROCEDURE, ALTER PROCEDURE, DROP PROCEDURE, CREATE FUNCTION,
DROP FUNCTION, CREATE TRIGGER, DROP TRIGGER.
CREATE PROCEDURE db5.p1 () DROP DATABASE db5//
"USE database"
mysql> CALL p1() //
------
| s1 |
------
| 5 |
------
1 row in set (0.03 sec)
Query OK, 0 rows affected (0.03 sec)
"SELECT * FROM t;"
mysql> CALL p1() //
mysql> SELECT * FROM t; //
"SELECT * FROM t;"
CREATE PROCEDURE p2 ()
LANGUAGE SQL <--
NOT DETERMINISTIC <--
SQL SECURITY DEFINER <--
COMMENT 'A Procedure' <--
SELECT CURRENT_DATE, RAND() FROM t //
CREATE PROCEDURE p2 ()
LANGUAGE SQL <--
NOT DETERMINISTIC
SQL SECURITY DEFINER
COMMENT 'A Procedure'
SELECT CURRENT_DATE, RAND() FROM t //
CREATE PROCEDURE p2 ()
LANGUAGE SQL
NOT DETERMINISTIC <--
SQL SECURITY DEFINER
COMMENT 'A Procedure'
SELECT CURRENT_DATE, RAND() FROM t //
CREATE PROCEDURE p2 ()
LANGUAGE SQL
NOT DETERMINISTIC
SQL SECURITY DEFINER <--
COMMENT 'A Procedure'
SELECT CURRENT_DATE, RAND() FROM t //
SQL SECURITY DEFINER
SQL SECURITY DEFINER
CREATE PROCEDURE p2 ()
LANGUAGE SQL
NOT DETERMINISTIC
SQL SECURITY DEFINER
COMMENT 'A Procedure' <--
SELECT CURRENT_DATE, RAND() FROM t //
CREATE PROCEDURE p2 ()
LANGUAGE SQL
NOT DETERMINISTIC
SQL SECURITY DEFINER
COMMENT ''
SELECT CURRENT_DATE, RAND() FROM t //
CREATE PROCEDURE p2 ()
SELECT CURRENT_DATE, RAND() FROM t //
LANGUAGE SQL NOT DETERMINISTIC SQL SECURITY DEFINER COMMENT ''
mysql> call p2() //
-------------- -----------------
| CURRENT_DATE | RAND() |
-------------- -----------------
| 2004-11-09 | 0.7822275075896 |
-------------- -----------------
1 row in set (0.26 sec)
Query OK, 0 rows affected (0.26 sec)
当调用过程p2时,一个SELECT语句被执行返回我们期望获得的随机数。
Digression: sql_mode unchanging
不会改变的
sql_mode
mysql> set sql_mode='ansi' //
mysql> create procedure p3()select'a'||'b'//
mysql> set sql_mode=''//
mysql> call p3()//
------------
| 'a' || 'b' |
------------
| ab |
------------
"SELECT 'Hello, world'"
语句mysql> CREATE PROCEDURE p4 () SELECT 'Hello, world' //
Query OK, 0 rows affected (0.00 sec)
mysql> CALL p4()//
--------------
| Hello, world |
--------------
| Hello, world |
--------------
1 row in set (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
mysql> CREATE PROCEDURE p5(p INT) SET @x = p //
Query OK, 0 rows affected (0.00 sec)
mysql> CALL p5(12345)//
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT @x//
-------
| @x |
-------
| 12345 |
-------
1 row in set (0.00 sec)
mysql> CREATE PROCEDURE p6 (OUT p INT)
-> SET p = -5 //
mysql> CALL p6(@y)//
mysql> SELECT @y//
------
| @y |
------
| -5 |
------
"SET @y = -5;"
CREATE PROCEDURE p7 ()
BEGIN
SET @a = 5;
SET @b = 5;
INSERT INTO t VALUES (@a);
SELECT s1 * @a FROM t WHERE s1 >= @b;
END; // /* I won't CALL this.
这个语句将不会被调用
*/
完成过程体的构造就是BEGIN/END块。这个BEGIN/END语句块和Pascal语言中的BEGIN/END是基本相同的,和C语言的框架是很相似的。我们可以使用块去封装多条语句。在这个例子中,我们使用了多条设定会话变量的语句,然后完成了一些insert和select语句。如果你的过程体中有多条语句,那么你就需要BEGIN/END块了。BEGIN/END块也被称为复合语句,在这里你可以进行变量定义和流程控制。
未完待续...