我创建一个这样的程序
DELIMITER $$
CREATE PROCEDURE proc_tambah_na ( IN v_nis VARCHAR( 20 ))
BEGIN
DECLARE v_selesai INTEGER DEFAULT 0;
DECLARE v_semester INTEGER;
DECLARE v_nilai INTEGER DEFAULT 0;
DECLARE v_jur INTEGER;
DECLARE cur_mp CURSOR FOR
select semester, sum(nilai) as jml_nilai_mp from mengambil_mp amp join mata_pelajaran mp on (amp.kd_mp=mp.kd_mp) where nis=v_nis;
SELECT id_jurusan INTO v_jur FROM siswa s jOIN kelas k on (s.id_kelas=k.id_kelas) where nis=v_nis;
DECLARE cur_mp_un CURSOR FOR
select semester, sum(nilai) as jml_nilai_mp_un from mengambil_mp amp join mata_pelajaran mp on (amp.kd_mp=mp.kd_mp) where nis=8091010010 and ket_mp_un='ya' and (jurusan=v_jur or jurusan=1) group by semester;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET v_selesai = 1;
OPEN cur_mp;
get_nilai_mp : LOOP
FETCH cur_mp INTO v_semester, v_nilai;
IF v_selesai = 1 THEN
LEAVE get_nilai_mp;
END IF;
INSERT INTO nilai_akhir (nis, semester, jml_nilai_mp) VALUES (v_nis, v_semester, v_nilai);
END LOOP get_nilai_mp;
CLOSE cur_mp;
SET v_selesai=0;
OPEN cur_mp_un;
get_nilai_mp_un : LOOP
FETCH cur_mp_un INTO v_semester, v_nilai;
IF v_selesai = 1 THEN
LEAVE get_nilai_mp_un;
END IF;
UPDATE nilai_akhir SET nilai=v_nilai WHERE nis=v_nis AND semester=v_semester;
END LOOP get_nilai_mp_un;
CLOSE cur_mp_un;
END $$
DELIMITER ;
但它返回错误信息:
#
1064-您的SQL语法错误;在第13行上,检查与您的MySQL服务器版本相对应的手册以获取正确的语法,以在'DECLARE cur_mp_un CURSOR FOR select semester,sum(nilai)as jml_nilai_'附近使用我不知道那条线是什么问题。我认为这已经是正确的,但是为什么我要返回该错误信息。
PS:对不起,变量名,我用我的母语,所以不要被那个困扰
最佳答案
有一个没有GROUP BY子句的聚合函数sum(nilai)
。
聚合函数只能与GROUP BY一起使用。
DECLARE cur_mp_un CURSOR FOR
选择一个学期,sum(nilai)作为jml_nilai_mp
来自mengambil_mp放大器
加入mata_pelajaran mp于(amp.kd_mp = mp.kd_mp)
nis = v_nis
按学期分组;