-2

我想将存储过程结果集插入表中。我的存储过程动态获取参数。请就此提出建议。

这是我的存储过程:

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 ;
4

2 回答 2

0

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

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

INSERT INTO table_name 
     SELECT ...
于 2013-04-17T05:33:05.400 回答
0

添加到 Bhavik Shah 。一行一行地使用游标

于 2013-04-17T05:41:03.487 回答