边肖想和大家分享一下如何在MySQL 5.7中使用PREPARE、EXECUTE和deal语句。相信大部分人还是不太了解。因此,我想分享这篇文章供你参考。希望你看完这篇文章后收获多多。让我们一起来看看。
PREPARE语句准备好一条SQL语句,并分配给这条SQL语句一个名字供之后调用。准备好的SQL语句通过EXECUTE命令执行,通过DEALLOCATE PREPARE命令释放掉。
语句的名称不区分大小写。准备好的SQL语句的名称可以是字符串或包含用户指定的SQL文本的变量。PREPARE中的SQL文本必须表示单个SQL语句,而不是多个SQL语句。在SQL语句中,字符被用作以后查询执行的参数。不能引用,不能及时绑定到字符变量。
如果准备好的SQL语句名称已经存在,它将在准备新语句之前被释放。这意味着,如果新语句包含错误并且无法准备,将返回错误,并且准备好的SQL语句将不再存在。
准备好的语句范围是在其中创建它的会话,并且具有以下特征:
准备好的语句在其他会话中无效;
当会话结束时,无论会话正常结束还是异常结束,在此会话中准备的SQL语句都将不复存在。如果自动连接功能打开,客户端将不会收到连接丢失的通知。
存储过程或函数中的准备好的语句将在存储过程或函数执行后继续存在,并且可以在存储过程或包之外继续执行。示例:
mysql SET @ a=10
查询正常,0行受影响(0.00秒)
mysql从“从dept2限制中选择*准备STMT?”;
查询正常,0行受影响(0.08秒)
准备的声明
mysql使用@a执行STMT;
- -
| deptno | dname |
- -
| 10 | A |
| 20 | B |
| 30 | C |
| 40 | D |
| 50 | E |
| 60 | F |
| 70 | G |
| 80 | H |
| 90 | I |
| 100 | J |
- -
10行一组(0.02秒)
MySQL SET @ skip=1;SET @ numrows=5;
查询正常,0行受影响(0.00秒)
查询正常,0行受影响(0.00秒)
mysql准备STMT从' SELECT * FROM dept2 LIMIT?';
查询正常,0行受影响(0.00秒)
准备的声明
mysql EXECUTE STMT使用@skip,@ numrows
- -
| deptno | dname |
- -
| 20 | B |
| 30 | C |
| 40 | D |
| 50 | E |
| 60 | F |
- -
5行一组(0.00秒)
mysql解除分配准备STMT;
查询正常,0行后
ted (0.00 sec)
mysql> EXECUTE STMT USING @skip, @numrows;
ERROR 1243 (HY000): Unknown prepared statement handler (STMT) given to EXECUTE
mysql> PREPARE STMT FROM 'SELECT * FROM dept2 LIMIT ?, ?';
Query OK, 0 rows affected (0.00 sec)
Statement prepared
mysql> EXECUTE STMT USING @skip, @numrows;
+--------+-------+
| deptno | dname |
+--------+-------+
| 20 | B |
| 30 | C |
| 40 | D |
| 50 | E |
| 60 | F |
+--------+-------+
5 rows in set (0.00 sec)
示例存储过程
delimiter $$
create procedure registerSQLReference(IN the_table_name VARCHAR(80), IN the_sql_script_version VARCHAR(80), IN the_sql_script_name VARCHAR(80),
IN the_install_version VARCHAR(80), IN the_execution_duration VARCHAR(80), IN the_sql_script_description VARCHAR(200))
BEGIN
declare column_exist INT DEFAULT 0;
declare the_query VARCHAR(500);
select '[INFO] Check if the column C_CHANGE_DESCRIPTION exists in the table @VERSION_LEVEL_TABLE_NAME';
set column_exist = is_ChangeDescColumnExist ('@VERSION_LEVEL_TABLE_NAME');
IF column_exist = 0
THEN
select '[INFO] VERSION LEVEL TABLE does not contain C_CHANGE_DESCRIPTION column.';
set @v_the_table_name=the_table_name;
select concat('INSERT INTO ', @v_the_table_name, ' VALUES (?, ?, ?, ?, ?) ') into the_query;
SET @stmt=the_query;
PREPARE STMT FROM @stmt;
select concat('[INFO] the_query=', the_query );
set @v_the_sql_script_version=the_sql_script_version;
set @v_the_sql_script_name=the_sql_script_name;
set @v_date=now();
set @v_the_install_version=the_install_version;
set @v_the_execution_duration=the_execution_duration;
EXECUTE STMT using @v_the_sql_script_version , @v_the_sql_script_name, @v_date, @v_the_install_version, @v_the_execution_duration;
ELSE
select '[INFO] VERSION LEVEL TABLE contains C_CHANGE_DESCRIPTION column.';
select concat('INSERT INTO ', the_table_name, ' VALUES (?, ?, ?, ?, ?, ?) ') into the_query;
SET @stmt=the_query;
PREPARE STMT FROM @stmt;
select concat('[INFO] the_query=', the_query );
set @v_the_sql_script_version=the_sql_script_version;
set @v_the_sql_script_name=the_sql_script_name;
set @v_date=now();
set @v_the_install_version=the_install_version;
set @v_the_execution_duration=the_execution_duration;
set @v_the_sql_script_description=the_sql_script_description;
select concat('[INFO] the_query=', the_query);
EXECUTE the_query using @v_the_sql_script_version , @v_the_sql_script_name, @v_date, @v_the_install_version, @v_the_execution_duration, @v_the_sql_script_description;
END IF;
DEALLOCATE PREPARE STMT;
END$$
delimiter ;
以上是“MySQL 5.7中PREPARE、EXECUTE、DEALLOCATE语句怎么用”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注行业资讯频道!
内容来源网络,如有侵权,联系删除,本文地址:https://www.230890.com/zhan/59016.html