如何在MySQL存储过程中具有动态SQL

时间:2021-12-11 16:37:34

How do you build and use dynamic sql in a MySQL stored procedure?

如何在MySQL存储过程中构建和使用动态sql ?

3 个解决方案

#1


43  

I don't believe MySQL supports dynamic sql. You can do "prepared" statements which is similar, but different.

我不相信MySQL支持动态sql。您可以做“准备”语句,它们是相似的,但不同的。

Here is an example:

这是一个例子:

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;

The prepared statements are often used to see an execution plan for a given query. Since they are executed with the execute command and the sql can be assigned to a variable you can approximate the some of the same behavior as dynamic sql.

准备好的语句通常用于查看给定查询的执行计划。由于它们是用execute命令执行的,并且sql可以被分配给一个变量,所以您可以近似地使用与dynamic sql相同的一些行为。

Here is a good link about this:

这里有一个很好的链接:

Don't forget to deallocate the stmt using the last line!

不要忘记使用最后一行来释放stmt !

Good Luck!

好运!

#2


94  

After 5.0.13, in stored procedures, you can use dynamic SQL:

5.0.13之后,在存储过程中,可以使用动态SQL:

delimiter // 
CREATE PROCEDURE dynamic(IN tbl CHAR(64), IN col CHAR(64))
BEGIN
    SET @s = CONCAT('SELECT ',col,' FROM ',tbl );
    PREPARE stmt FROM @s;
    EXECUTE stmt;
    DEALLOCATE PREPARE stmt;
END
//
delimiter ;

Dynamic SQL does not work in functions or triggers. See the MySQL documentation for more uses.

动态SQL不能在函数或触发器中工作。更多用途请参见MySQL文档。

#3


2  

You can pass thru outside the dynamic statement using User-Defined Variables

您可以使用用户定义的变量在动态语句之外传递。

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)

#1


43  

I don't believe MySQL supports dynamic sql. You can do "prepared" statements which is similar, but different.

我不相信MySQL支持动态sql。您可以做“准备”语句,它们是相似的,但不同的。

Here is an example:

这是一个例子:

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;

The prepared statements are often used to see an execution plan for a given query. Since they are executed with the execute command and the sql can be assigned to a variable you can approximate the some of the same behavior as dynamic sql.

准备好的语句通常用于查看给定查询的执行计划。由于它们是用execute命令执行的,并且sql可以被分配给一个变量,所以您可以近似地使用与dynamic sql相同的一些行为。

Here is a good link about this:

这里有一个很好的链接:

Don't forget to deallocate the stmt using the last line!

不要忘记使用最后一行来释放stmt !

Good Luck!

好运!

#2


94  

After 5.0.13, in stored procedures, you can use dynamic SQL:

5.0.13之后,在存储过程中,可以使用动态SQL:

delimiter // 
CREATE PROCEDURE dynamic(IN tbl CHAR(64), IN col CHAR(64))
BEGIN
    SET @s = CONCAT('SELECT ',col,' FROM ',tbl );
    PREPARE stmt FROM @s;
    EXECUTE stmt;
    DEALLOCATE PREPARE stmt;
END
//
delimiter ;

Dynamic SQL does not work in functions or triggers. See the MySQL documentation for more uses.

动态SQL不能在函数或触发器中工作。更多用途请参见MySQL文档。

#3


2  

You can pass thru outside the dynamic statement using User-Defined Variables

您可以使用用户定义的变量在动态语句之外传递。

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)