2013-04-17 48 views
-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 ; 
+0

使用插入語句是什麼問題? –

回答

0

1)創建的列一個表來存儲A.depart_key,A.depart_id,A.depart_name,A.yr_no等

2)使用INSERT查詢上方的SELECT語句如

INSERT INTO table_name 
    SELECT ... 
0

加入Bhavik Shah。採取逐行使用遊標

+0

** @ manuthalasseril **,它是一個評論,而不是一個答案。並且,請提供鏈接。 – BlitZ

+0

@CORRUPT我找不到評論按鈕。它只顯示Bhavik Shah答案中的共享鏈接和編輯按鈕.- [簡單遊標鏈接](http://www.kbedell.com/2009/03/02/a-simple-example-of-a-mysql -stored-過程 - 即用途-A-光標/) – manuthalasseril

相關問題