我创建了一个像这样的程序
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语法出错;查看与您的MySQL服务器版本相对应的手册,以获得正确的语法,使用附近的“DECLARE cur_mp_un CURSOR FOR选择学期”,将sum(nilai)作为jml_nilai _'在第13行
我不知道这条线有什么问题。我认为它已经是正确的了,但为什么我会回复那个错误信息..
PS:抱歉变量名称,我使用我的母语,所以不要为此烦恼
答案 0 :(得分:0)
有一个没有GROUP BY子句的聚合函数sum(nilai)
聚合函数只能与GROUP BY一起使用。
DECLARE cur_mp_un CURSOR for
选择学期,sum(nilai)为jml_nilai_mp
来自mengambil_mp amp
加入mata_pelajaran mp on(amp.kd_mp = mp.kd_mp)
其中nis = v_nis
GROUP BY学期;