mysqlprepare命令_MySQLprepare语句的SQL语法
MySQL prepare语法:
PREPAREstatement_name FROM preparable_SQL_statement; /*定义*/
EXECUTE statement_name [USING @var_name [, @var_name] ...]; /*执⾏预处理语句*/
{DEALLOCATE | DROP} PREPARE statement_name /*删除定义*/ ;
PREPARE语句⽤于预备⼀个语句,并指定名称statement_name,以后引⽤该语句。语句名称对⼤⼩写不敏感。preparable_stmt可以是⼀个⽂字字符串,也可以是⼀个包含了语句⽂本的⽤户变量。该⽂本必须表现为⼀个单⼀的SQL语句,⽽不是多个语句。在这语句
⾥,‘?’字符可以被⽤于标识参数,当执⾏时,以指⽰数据值绑定到查询后。‘?’字符不应加引号,即使你想要把它们与字符串值结合在⼀起。参数标记只能⽤于数据值应该出现的地⽅,⽽不是SQL关键字,标识符,等等。
如果预语句已经存在,则在新的预语句被定义前,它会被隐含地删掉。
例如:
mysql> prepare optimize_tables from "optimize table temp";Query OK, 0 rows affected (0.00 sec)Statement prepared
mysql> execute optimize_tables;+-----------+----------+----------+----------+| Table    | Op      | Msg_type | Msg_text |+-----------+----------+----------+----------+| p | optimize | status  | OK      | +-----------+----------+----------+----------+1 row in set (0.37 sec)
mysql> deallocate prepare optimize_tables;Query OK, 0 rows affected (0.00 sec)
--------------------------------------------------------------------------mysql> prepare md5sum from 'select md5(?) AS md5sum';Query OK, 0 rows affected (0.00 sec)Statement prepared
mysql> set @a=111;Query OK, 0 rows affected (0.00 sec)
mysql> set @b=222;Query OK, 0 rows affected (0.00 sec)
mysql> execute md5sum using @a;+----------------------------------+| md5sum                          |+----------------------------------+| 698d51a19d8a121ce581499d7b701668 | +----------------------------------+1 row in set (0.00 sec)
mysql> execute md5sum using @b;+----------------------------------+| md5sum                          |+----------------------------------+| bcbe3365e6ac95ea2c0343a2395834dd | +----------------------------------+1 row in set (0.00 sec)
mysql> drop prepare md5sum;Query OK, 0 rows affected (0.00 sec)
--------------------------------------------------------------------------------------
mysql> prepare update_table from "update users set password=password('aaa') where username='a'";Query OK, 0 rows affected (0.00 sec)Statement prepared
mysql> execute update_table;Query OK, 0 rows affected (0.00 sec)Rows matched: 1 Changed: 0 Warnings: 0
mysql> deallocate prepare update_table;Query OK, 0 rows affected (0.00 sec)
从MySQL 5.0 开始,⽀持了⼀个全新的SQL句法:
PREPARE stmt_name FROM preparable_stmt;
EXECUTE stmt_name [USING @var_name [, @var_name] ...];
{DEALLOCATE | DROP} PREPARE stmt_name;
通过它,我们就可以实现类似 MS SQL 的 sp_executesql 执⾏动态SQL语句!
同时也可以防⽌注⼊式攻击!
为了有⼀个感性的认识,
下⾯先给⼏个⼩例⼦:
mysql> PREPARE stmt1 FROM 'SELECT SQRT(POW(?,2) + POW(?,2)) AS hypotenuse';
mysql> SET @a = 3;
mysql> SET @b = 4;
mysql> EXECUTE stmt1 USING @a, @b;
+------------+
| hypotenuse |
+------------+
| 5 |
+------------+
mysql> DEALLOCATE PREPARE stmt1;
mysql> SET @s = 'SELECT SQRT(POW(?,2) + POW(?,2)) AS hypotenuse';
mysql> PREPARE stmt2 FROM @s;
mysql> SET @a = 6;
mysql> SET @b = 8;
mysql> EXECUTE stmt2 USING @a, @b;
+------------+
| hypotenuse |
+------------+
| 10 |
+------------+
mysql> DEALLOCATE PREPARE stmt2;
如果你的MySQL 版本是 5.0.7 或者更⾼的,你还可以在 LIMIT ⼦句中使⽤它,⽰例如下:mysql> SET @a=1;mysql> PREPARE STMT FROM "SELECT * FROM tbl LIMIT ?"; mysql> EXECUTE STMT USING @a;
mysql> SET @skip=1; SET @numrows=5; phperz
mysql> PREPARE STMT FROM "SELECT * FROM tbl LIMIT ?, ?";
mysql> EXECUTE STMT USING @skip, @numrows; 使⽤ PREPARE 的⼏个注意点:
mysql中delete语句A:PREPARE stmt_name FROM preparable_stmt;
预定义⼀个语句,并将它赋给 stmt_name ,stmt_name 是不区分⼤⼩写的。
B: 即使 preparable_stmt 语句中的 ? 所代表的是⼀个字符串,你也不需要将 ? ⽤引号包含起来。
C: 如果新的 PREPARE 语句使⽤了⼀个已存在的 stmt_name ,那么原有的将被⽴即释放! 即使这个新的 PREPARE 语句因为错误⽽不能被正确执⾏。
D: PREPARE stmt_name 的作⽤域是当前客户端连接会话可见。
E: 要释放⼀个预定义语句的资源,可以使⽤ DEALLOCATE PREPARE 句法。
F: EXECUTE stmt_name 句法中,如果 stmt_name 不存在,将会引发⼀个错误。
G: 如果在终⽌客户端连接会话时,没有显式地调⽤ DEALLOCATE PREPARE 句法释放资源,服务器端会⾃⼰动释放它。
H: 在预定义语句中,CREATE TABLE, DELETE, DO, INSERT, REPLACE, SELECT, SET, UPDATE, 和⼤部分的 SHOW 句法被⽀持。
G: PREPARE 语句不可以⽤于存储过程,⾃定义函数!但从 MySQL 5.0.13 开始,它可以被⽤于存储过程,仍不⽀持在函数中使⽤!下⾯给个⽰例: CREATE PROCEDURE `p1`(IN id INT UNSIGNED,IN name VARCHAR(11))BEGIN lable_exit: BEGIN SET
@SqlCmd = 'SELECT * FROM tA '; IF id IS NOT NULL THEN SET @SqlCmd = CONCAT(@SqlCmd , 'WHERE id=?'); PREPARE stmt FROM @SqlCmd; SET @a = id; EXECUTE stmt USING @a; LEAVE lable_exit; END IF; IF name IS NOT NULL THEN SET @SqlCmd = CONCAT(@SqlCmd , 'WHERE name LIKE ?'); PREPARE stmt FROM @SqlCmd; SET @a = CONCAT(name, '%'); EXECUTE stmt USING @a; LEAVE lable_exit; END IF; END lable_exit;END; CALL `p1`(1,NULL);CALL `p1`(NULL,'QQ');DROP PROCEDURE `p1`;

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。