Mysql 动态获取存储过程的参数值
我正在为所有存储过程创建某种审计功能。我能够从:information_schema.parameters表中获取存储过程拥有的参数的名称。 但是,我希望为所有存储过程创建通用代码,这些存储过程将获取参数的名称,并相应地获取该调用的参数值,并登录到另一个表中 e、 gMysql 动态获取存储过程的参数值,mysql,database,dynamic-sql,Mysql,Database,Dynamic Sql,我正在为所有存储过程创建某种审计功能。我能够从:information_schema.parameters表中获取存储过程拥有的参数的名称。 但是,我希望为所有存储过程创建通用代码,这些存储过程将获取参数的名称,并相应地获取该调用的参数值,并登录到另一个表中 e、 g 提前谢谢 您可以从信息\u SCHEMA.parameters中获取例程的参数列表,但您必须知道该过程的模式和名称: mysql> delimiter $$ mysql> create procedure mypro
提前谢谢 您可以从信息\u SCHEMA.parameters中获取例程的参数列表,但您必须知道该过程的模式和名称:
mysql> delimiter $$
mysql> create procedure myproc (in foo int, in bar int)
-> begin
-> select group_concat(parameter_name order by ordinal_position) as params
-> from INFORMATION_SCHEMA.PARAMETERS
-> where specific_schema='test' and specific_name='myproc';
-> end$$
mysql> call myproc(123, 456)$$
+---------+
| params |
+---------+
| foo,bar |
+---------+
您需要使用动态SQL来执行此操作,但不能在动态SQL中引用存储的过程参数:
演示:
mysql> create procedure myproc (in foo int, in bar int)
-> begin
-> set @sql = 'SELECT foo, bar';
-> prepare stmt from @sql;
-> execute stmt;
-> end$$
mysql> call myproc(123, 456)$$
ERROR 1054 (42S22): Unknown column 'foo' in 'field list'
我通常不鼓励人们使用MySQL存储过程。这项任务在几乎任何应用程序编程语言中都要容易得多。感谢您的回复。但我的要求略有不同。例如,在您的示例中,您可以获取'foo,bar',但我想在存储过程中找到类似的内容:'foo=123,bar=456'。同样的问题是,您不能使用变量名获取它们的值。
mysql> create procedure myproc (in foo int, in bar int)
-> begin
-> set @sql = 'SELECT foo, bar';
-> prepare stmt from @sql;
-> execute stmt;
-> end$$
mysql> call myproc(123, 456)$$
ERROR 1054 (42S22): Unknown column 'foo' in 'field list'