[英]how do i get a stored procedure result to use on another stored procedure or query statement
I am trying to create a stored procedure where inside the procedure is a query statement which its result will be used as a parameter for another query我正在尝试创建一个存储过程,其中过程内部是一个查询语句,其结果将用作另一个查询的参数
CREATE DEFINER=`root`@`localhost` PROCEDURE `getRecActive`(
IN adminid VARCHAR(25))
BEGIN
select record_id from records2 where record_div = @adminid
//i want to use record_id result to the next query
select * from records where record = "record_id";
END
select * from records
where record in
(
select record_id from records2 where record_div = @adminid
)
If you must do this as a procedure then you need to select into a local variable and use that in your second query.如果您必须将其作为一个过程执行,那么您需要将 select 转换为局部变量并在第二个查询中使用它。 given
给定
+----+----------+----------+
| id | username | photo |
+----+----------+----------+
| 1 | aaa | john.png |
| 2 | Jane | jane.png |
| 3 | Ali | |
+----+----------+----------+
3 rows in set (0.00 sec)
drop procedure if exists p;
delimiter $$
CREATE PROCEDURE p(
IN adminid VARCHAR(25))
BEGIN
declare vid int;
select id into vid from users where username = adminid;
select id,username,photo from users where id = vid;
END $$
delimiter ;
call p('aaa')
+----+----------+----------+
| id | username | photo |
+----+----------+----------+
| 1 | aaa | john.png |
+----+----------+----------+
1 row in set (0.00 sec)
Query OK, 0 rows affected (0.02 sec)
Note you cannot stuff a result set into a variable and what you are trying to do won't work at all if thats what you get back from your first query.请注意,您不能将结果集填充到变量中,如果这是您从第一个查询中得到的结果,那么您尝试做的事情根本不起作用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.