如何从存储过程中的表中获取id?

时间:2017-07-25 05:10:48

标签: mysql stored-procedures

我是存储过程的新手。我正在编写存储过程来存储临时表中的学生详细信息。一切都插入正常,除了courseid。我需要从临时表中插入对应于课程的courseid。我得到了schoolid并正确插入。但是在校园之后什么也没有插入。我不知道什么是错的。

DELIMITER $$
CREATE OR REPLACE PROCEDURE InsertUpdateStudents()
BEGIN
DECLARE v_finished INTEGER DEFAULT 0;
DECLARE newsudentid, schoolid, courseid, subjectid, studentcount INT;
DECLARE co, na, ma, im, fe, fr, s, bp, o, nstudentcode, st CHAR(55);
DEClARE cur1 CURSOR FOR SELECT DT.code, DT.subject, DT.course, DT.ImageURL, DT.EnglishCr, DT.FrenchCr, DT.Si, DT.BP, DT.Off, DT.Avail FROM xcart_students_temp AS DT;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET v_finished = 1;
 OPEN cur1;
 dummy_students: LOOP
 FETCH cur1 into co, na, ma, im, fe, fr, s, bp, o, st;
 IF v_finished = 1 THEN 
 LEAVE dummy_students;
 END IF;
IF  NOT EXISTS(SELECT (1) FROM xcart_students AS P WHERE P.studentcode=co) THEN
SELECT MA.school_id INTO schoolid FROM xcart_schools AS MA WHERE LOWER(MA.name)='stmays' OR LOWER(MA.name)='st.marys' ;
SELECT BRAN.course_id INTO courseid FROM xcart_course AS BRAN WHERE LOWER(BRAN.course_name) = LOWER(ma) AND BRAN.course_school_id = schoolid  ;
INSERT INTO xcart_students (cid,sid,coursename, subjectname, transfercode, mfgcode, meta_keywords, meta_description, title_tag,avail) VALUES(schoolid, courseid, ma, na, co, co, CONCAT(ma,' ',na,' ',fe), CONCAT(ma,' ',na,' ',fe), CONCAT(ma,' ',na,' ',fe), st);
SET newsudentid = LAST_INSERT_ID();
SET studentcount = 1000 + newsudentid; 
SET nstudentcode = CONCAT('W',studentcount);
UPDATE xcart_students SET studentcode = nstudentcode WHERE studentid = newsudentid;
INSERT INTO xcart_students_lng_en (studentid, student, descr, fulldescr, keywords) VALUES(newsudentid, CONCAT(ma,' ',na,' ',fe,' -', s,' -',bp,' -',o), CONCAT(ma,' ',na,' ',fe,' -', s,' -',bp,' -',o), CONCAT(ma,' ',na,' ',fe,' -', s,' -',bp,' -',o), CONCAT(ma,' ',na,' ',fe,' -', s,' -',bp,' -',o) );
INSERT INTO xcart_students_lng_fr (studentid, student, descr, fulldescr, keywords) VALUES(newsudentid, CONCAT(ma,' ',na,' ',fr,' -', s,' -',bp,' -',o), CONCAT(ma,' ',na,' ',fr,' -', s,' -',bp,' -',o), CONCAT(ma,' ',na,' ',fr,' -', s,' -',bp,' -',o), CONCAT(ma,' ',na,' ',fr,' -', s,' -',bp,' -',o) );
IF NULLIF(im, '') IS NULL  THEN
  SET im =  'images/students/no-image.jpg';
END IF;
INSERT INTO xcart_images_P(id, image_path, avail, alt, md5) VALUES(newsudentid, im, 'Y', CONCAT(ma,' ',na,' ',fe,' -', s,' -',bp,' -',o), MD5(CONCAT(ma,' ',na,' ',fe,' -', s,' -',bp,' -',o)));
END IF;
 END LOOP dummy_students;
 CLOSE cur1;
END$$
DELIMITER ;

1 个答案:

答案 0 :(得分:0)

而不是使用......

SET courseid = (SELECT course_id FROM xcart_course WHERE course_name = ma);

使用

SELECT course_id INTO courseid FROM xcart_course WHERE course_name = ma;