在MySQL中多次执行一个sql查询,类似于在TSQL(sql Server)中执行GO



我试图在MySQL中多次运行以下命令。是否有一个简单的方法像在TSQL/SQL服务器的命令MySQL?

select
now(),
@@max_connections as "Max Connections",
count(host) as "Current Connections"
from information_schema.processlist; 

想保持简单的演示。不寻找SPROC。

mysql> set @query = 'select
'>   now(),
'>   @@max_connections as "Max Connections",
'>   count(host) as "Current Connections"
'> from information_schema.processlist; ';
Query OK, 0 rows affected (0.00 sec)
mysql> prepare stmt from @query;
Query OK, 0 rows affected (0.01 sec)
Statement prepared
mysql> execute stmt;
+---------------------+-----------------+---------------------+
| now()               | Max Connections | Current Connections |
+---------------------+-----------------+---------------------+
| 2021-08-16 18:22:03 |             512 |                   1 |
+---------------------+-----------------+---------------------+
1 row in set (0.01 sec)

然后你可以在同一个会话中多次execute stmt(准备好的语句具有当前会话的作用域)。

参见https://dev.mysql.com/doc/refman/8.0/en/sql-prepared-statements.html获取更多信息。

最新更新