简体   繁体   English

MySQL:对存储过程参数使用select语句

[英]MySQL: Use a select statement for stored procedure parameter

I have a stored procedure that works: 我有一个有效的存储过程:

call my_procedure('A,B,C,D');

I want to populate the A,B,C with a list from a subquery of another table, eg: 我想用另一个表的子查询中的列表填充A,B,C,例如:

call my_procedure(
  SELECT group_concat(letters) FROM table WHERE type = 'some_type')
);

Possible? 可能? Or am I doing it wrong? 或者我做错了吗?

SELECT my_function(group_concat(letters)) FROM table WHERE type = 'some_type';

You can assign the value to a user-defined variable and then pass that variable to the function. 您可以将值分配给用户定义的变量,然后将该变量传递给该函数。

For example: 例如:

SELECT group_concat(letters) 
INTO @foo
FROM table 
WHERE type = 'some_type';

call my_function(@foo);

Yes, you can pass a string to a procedure that's returned as the result of subquery. 是的,您可以将字符串传递给作为子查询结果返回的过程。

But not as a bare query: 但不是一个简单的查询:

mysql> create procedure foo (in s text) begin select s; end !!

mysql> call foo( select group_concat(user) from mysql.user );
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use near 
'select group_concat(user) from mysql.user )' at line 1

If you enclose the query in parentheses, it counts as a scalar subquery , that is, a subquery that is bound to return one row, one column: 如果将查询括在括号中,则它将计为标量子查询 ,即绑定返回一行,一列的子查询:

mysql> call foo( (select group_concat(user) from mysql.user) );
+--------------------+
| s                  |
+--------------------+
| root,root,bill,xbm |
+--------------------+

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM