L
L
Lander2017-09-06 09:29:49
MySQL
Lander, 2017-09-06 09:29:49

How to use the result of a stored procedure in a query?

Good afternoon. My question is completely noob but can't find an answer. I would be glad if you poke your nose into the right section of the documentation.
There is a basis in which there is a stored procedure.

CREATE DEFINER=`root`@`localhost` PROCEDURE `enum_id`(IN `start_id` INT, IN `end_id` INT) 
  NOT DETERMINISTIC CONTAINS SQL SQL SECURITY DEFINER 
BEGIN 
  DECLARE current_id INT; 
  create temporary table id_list ( 
    `id` INT NOT NULL 
  ); 
  
  SET current_id = start_id; 
  WHILE current_id <= end_id DO 
    INSERT INTO id_list (`id`) VALUES (current_id); 
    SET current_id = current_id + 1; 
  END WHILE; 
  
  select * from id_list; 
END

I’ll make a reservation right away, the example is academic and has nothing to do with reality. In real life, the procedure is more complicated, but the essence is the same...
Is it possible to use the result of this procedure (id list) in a query to join records from other tables to this table?
Something like:
SELECT * FROM (`enum_id`(0, 100)) `t` JOIN `enity` ON `t`.`id`=`entity`.`id`;

And if so, how? Thanks in advance for your replies.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
F
Fortop, 2017-09-06
@usdglander

Save results to temporary table and join with it

R
Rsa97, 2017-09-06
@Rsa97

What about the meaning? The same without any procedures:

SELECT * 
  FROM `entity`
  WHERE `entity_id` >= 0 AND `entity_id` >= 100

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question