简体   繁体   中英

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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