如何将存储过程结果插入表中

时间:2013-04-17 05:15:57

标签: mysql stored-procedures

我想将存储过程结果集插入表中。我的存储过程动态获取参数。请告知此事。

这是我的存储过程:

DELIMITER $$

CREATE DEFINER=`admin`@`%` PROCEDURE `usp_StuCurResDep`( IN deptkey BIGINT)
BEGIN
SELECT A.depart_key, 
       A.depart_id, 
       A.depart_name, 
       A.yr_no, 
       Sum(A.totalstudents)                                                 AS 
       TotalStudents, 
       Sum(A.passed)                                                        AS 
       Passed, 
       Ifnull(Round(( Sum(A.passed) / Sum(A.totalstudents) ) * 100, 2), '') AS 
       percent 
FROM   (SELECT c.depart_key, 
               d.depart_id, 
               d.depart_name, 
               0 AS TotalStudents, 
               0 AS Passed, 
               bs.yr_no 
        FROM   batch bat 
               INNER JOIN course c 
                       ON bat.cour_key = c.cour_key 
               INNER JOIN batch_schedule bs 
                       ON bat.bat_key = bs.bat_key 
               INNER JOIN aicte_department d 
                       ON d.depart_key = c.depart_key 
        WHERE  c.depart_key = deptkey 
               AND bat.bat_key IN (SELECT bs.bat_key 
                                   FROM   batch_schedule bs 
                                   WHERE  ( bs.start_date <= CURRENT_DATE() 
                                            AND bs.end_date >= CURRENT_DATE() )) 
        GROUP  BY c.depart_key, 
                  bs.yr_no 
        UNION ALL 
        SELECT dpt.depart_key, 
               dpt.depart_id, 
               dpt.depart_name, 
               Count(*) AS TotalStudents, 
               0        AS Passed, 
               bs.yr_no 
        FROM   exam_schedule_hdr hdr 
               INNER JOIN exam_schedule_dtl dtl 
                       ON hdr.iexamschkey = dtl.iexamschkey 
               INNER JOIN tb_examresult er 
                       ON dtl.iexamdtlkey = er.er_examschdtlkey 
               INNER JOIN tb_catalogue cat 
                       ON hdr.iexam_id = cat.catlg_key 
                          AND catlg_other = 1 
               INNER JOIN batch bat 
                       ON hdr.ibat_id = bat.bat_key 
               INNER JOIN course c 
                       ON bat.cour_key = c.cour_key 
               INNER JOIN aicte_department dpt 
                       ON c.depart_key = dpt.depart_key 
               INNER JOIN batch_schedule bs 
                       ON bat.bat_key = bs.bat_key 
                          AND hdr.ssemester = bs.sem_no 
        WHERE  c.depart_key = deptkey 
               AND er_status = 'P' 
               AND hdr.ssemester IN (SELECT DISTINCT Max(hdr.ssemester) AS 
                                                     Sem_No 
                                     FROM   exam_schedule_hdr hdr 
                                            INNER JOIN exam_schedule_dtl dtl 
                                                    ON hdr.iexamschkey = 
                                                       dtl.iexamschkey 
                                            INNER JOIN tb_examresult er 
                                                    ON dtl.iexamdtlkey = 
                                                       er.er_examschdtlkey 
                                            INNER JOIN tb_catalogue cat 
                                                    ON hdr.iexam_id = 
                                                       cat.catlg_key 
                                                       AND catlg_other = 1 
                                     WHERE  c.depart_key = deptkey 
                                            AND er_status = 'P' 
                                            AND hdr.ibat_id = bat.bat_key 
                                     GROUP  BY hdr.ibat_id) 
        GROUP  BY dpt.depart_key, 
                  bs.yr_no 
        UNION ALL 
        SELECT dpt.depart_key, 
               dpt.depart_id, 
               dpt.depart_name, 
               0        AS TotalStudents, 
               Count(*) AS Passed, 
               bs.yr_no 
        FROM   exam_schedule_hdr hdr 
               INNER JOIN exam_schedule_dtl dtl 
                       ON hdr.iexamschkey = dtl.iexamschkey 
               INNER JOIN tb_examresult er 
                       ON dtl.iexamdtlkey = er.er_examschdtlkey 
               INNER JOIN tb_catalogue cat 
                       ON hdr.iexam_id = cat.catlg_key 
                          AND catlg_other = 1 
               INNER JOIN batch bat 
                       ON hdr.ibat_id = bat.bat_key 
               INNER JOIN course c 
                       ON bat.cour_key = c.cour_key 
               INNER JOIN aicte_department dpt 
                       ON c.depart_key = dpt.depart_key 
               INNER JOIN batch_schedule bs 
                       ON bat.bat_key = bs.bat_key 
                          AND hdr.ssemester = bs.sem_no 
        WHERE  c.depart_key = deptkey 
               AND er_status = 'P' 
               AND er_passfail = 'P' 
               AND hdr.ssemester IN (SELECT DISTINCT Max(hdr.ssemester) AS 
                                                     Sem_No 
                                     FROM   exam_schedule_hdr hdr 
                                            INNER JOIN exam_schedule_dtl dtl 
                                                    ON hdr.iexamschkey = 
                                                       dtl.iexamschkey 
                                            INNER JOIN tb_examresult er 
                                                    ON dtl.iexamdtlkey = 
                                                       er.er_examschdtlkey 
                                            INNER JOIN tb_catalogue cat 
                                                    ON hdr.iexam_id = 
                                                       cat.catlg_key 
                                                       AND catlg_other = 1 
                                     WHERE  er_status = 'P' 
                                            AND hdr.ibat_id = bat.bat_key 
                                     GROUP  BY hdr.ibat_id) 
        GROUP  BY dpt.depart_key, 
                  bs.yr_no) A 
WHERE  A.yr_no IS NOT NULL 
       AND A.yr_no <> 0 
GROUP  BY A.depart_key, 
          A.yr_no 
ORDER  BY a.depart_key, 
          a.yr_no; 
END$$

DELIMITER ;

2 个答案:

答案 0 :(得分:0)

1)创建一个包含列的表来存储A.depart_key,A.depart_id,A.depart_name,A.yr_no等

2)在SELECT语句上方使用INSERT查询,如

INSERT INTO table_name 
     SELECT ...

答案 1 :(得分:0)

加入Bhavik Shah。一个接一个地使用游标