mysql - 在另一个存储过程中调用存储过程

时间:2017-03-28 04:22:15

标签: mysql

由于mysql函数无法返回表/结果集,因此我创建了一个存储过程( Get_StudentsWithAllIndicators )来完成我需要的东西。现在,我需要在这样的实际存储过程(Find_MapDetails)中使用此存储过程结果。

select * from students where studentid in (Get_StudentsWithAllIndicators('7,8', 2));

但它不起作用!如果您建议创建临时表并在其中插入值,请告诉我语法..谢谢

Get_StudentsWithAllIndicators

CREATE PROCEDURE `Get_StudentsWithAllIndicators`(IN p_list VARCHAR(255), IN p_length int)
 BEGIN
/*make query with the length of indicators in the list*/
DECLARE x  INT;
SET x=1;
SET @queryMain = 'SELECT distinct studentid FROM studentindicators WHERE studentid IN ';
SET @queryWhere = '(SELECT studentid FROM studentindicators WHERE indicatorid = substring_index(\'';
SET @query = '';

WHILE x  <= p_length DO

SET @query =  CONCAT(@query, @queryWhere, p_list, '\', ",", 1)) AND studentId IN ');    
SET x=x+1;
SET @lengthWithCommas = Length(p_list);

SET p_list = substr(p_list, instr(p_list, ',') + 1, @lengthWithCommas - instr(p_list, ','));

END WHILE;

/*remove last AND - note: no occurence of A after AND is expected*/
SET @query = CONCAT(@queryMain, LEFT(@query, LENGTH(@query) - LOCATE('A', REVERSE(@query))));


PREPARE stmt FROM @query;
EXECUTE stmt;

 END

1 个答案:

答案 0 :(得分:1)

INSERT INTO #MYTEMPTABLE exec Get_StudentsWithAllIndicators('7,8', 2)   

SELECT * FROM students where studentId in (SELECT studentid FROM #MYTEMPTABLE)

将第一个sp的结果保存到temp表中。