如何在MySQL存储过程中使用动态SQL如何在MySQL存储过程中构建和使用动态SQL?
3 回答
跃然一笑
TA贡献1826条经验 获得超6个赞
mysql> PREPARE stmt FROM -> 'select count(*) -> from information_schema.schemata -> where schema_name = ? or schema_name = ?';Query OK, 0 rows affected (0.00 sec)Statement prepared mysql> EXECUTE stmt -> USING @schema1,@schema2+----------+| count(*) |+----------+| 2 |+----------+1 row in set (0.00 sec)mysql> DEALLOCATE PREPARE stmt;
stmt
呼如林
TA贡献1798条经验 获得超3个赞
可以使用用户定义的变量在动态语句之外传递。
Server version: 5.6.25-log MySQL Community Server (GPL)
mysql> PREPARE stmt FROM 'select "AAAA" into @a';
Query OK, 0 rows affected (0.01 sec)
Statement prepared
mysql> EXECUTE stmt;
Query OK, 1 row affected (0.01 sec)
DEALLOCATE prepare stmt;
Query OK, 0 rows affected (0.01 sec)
mysql> select @a;
+------+
| @a |
+------+
|AAAA |
+------+
1 row in set (0.01 sec)
添加回答
举报
0/150
提交
取消