0

So I want to pass dynamic value of param to stored procedure from parent query however it is producing error, here is the query:

SELECT *
FROM mytable
WHERE mytable.user_id IN (CALL getDownlines(mytable.user_id))

However when I run SP directly, it works fine:

CALL getDownlines(100)

Any ideas please ?

Here is SP for reference, not sure how to convert it into function:

DELIMITER $

DROP PROCEDURE IF EXISTS getDownlines$

CREATE PROCEDURE getDownlines(in_id INT)
BEGIN

    drop table if exists temp1;
    drop table if exists temp2;
    drop table if exists results; 

    create temporary table temp2 as (select id, upline_id from agents where upline_id = in_id); 
    create temporary table results as (select id, upline_id from temp2); 
    create temporary table temp1 (id int, upline_id int);

    while (select count(*) from temp2) do 

        insert into temp1 (id, upline_id)
        select a.id, upline_id 
        from agents a
        where a.upline_id in (select id from temp2) ;

        insert into results (id, upline_id)
        select distinct id, upline_id
        from temp1;

        delete from temp2;

        insert into temp2 (id, upline_id)
        select distinct id, upline_id
        from temp1;

        delete from temp1;
    end while;    

    select a.* 
    from results r
    join agents a
    on a.id = r.id;

    drop table if exists temp1;
    drop table if exists temp2;
    drop table if exists results; 

End $$  

DELIMITER ;  
1
  • 2
    make it a function and you can call it Commented Feb 6, 2017 at 8:58

1 Answer 1

3

That is not possible to call a procedure from a select. Do it in 2 steps:

  1. Call the procedure and store the result somewhere.
  2. Run the select against the result of the procedure.

Or turn your procedure into a function.

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks but I want to pass dynamic parameter from parent query, can you provide some example of what exactly you mean ? thx
You can either change the procedure to make the result for all user_ids or you could take the code of the procedure and put it directly into the select
Cannot put procedure code directly in select cause it contains drop, insert, etc commands, please see updated question thanks
Not sure that your procedure is doing but it has to be super slow, You should better put a select at the end of your procedure to get the result you desire.
cannot understand your theory thanks anyways i will figure out myself
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.