考生导入-存储过程.sql 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. CREATE DEFINER = CURRENT_USER PROCEDURE `import_exam_students`(IN `batch_id` bigint)
  2. BEGIN
  3. declare total BIGINT(20);
  4. declare count BIGINT(20);
  5. declare id BIGINT(20);
  6. declare course_id_temp BIGINT(20);
  7. declare course_id BIGINT(20);
  8. declare course_code varchar(255);
  9. declare identity_number varchar(255);
  10. declare student_code varchar(255);
  11. declare org_id_temp BIGINT(20);
  12. declare org_id BIGINT(20);
  13. declare org_code varchar(255);
  14. declare root_org_id BIGINT(20);
  15. DECLARE cursor_temp CURSOR FOR
  16. select t.id,t.course_id,t.org_id,t.course_code,t.student_code,t.identity_number,t.org_code,t.root_org_id
  17. from ec_e_exam_student_tmp t
  18. where t.batch_id= batch_id;
  19. set count = 0;
  20. select count(1) into total from ec_e_exam_student_tmp t where t.batch_id= batch_id;
  21. open cursor_temp;
  22. read_loop: LOOP
  23. set count = count+1;
  24. FETCH cursor_temp INTO id,course_id,org_id,course_code,student_code,identity_number,org_code,root_org_id;
  25. SELECT id,course_id,org_id,course_code,student_code,identity_number,org_code,root_org_id;
  26. IF course_id is null THEN
  27. SELECT x.id into course_id_temp from ecs_core_course x where x.code= course_code and x.enable=true and x.org_id=root_org_id;
  28. IF course_id_temp is not null THEN
  29. update ec_e_exam_student_tmp x set x.course_id=course_id_temp
  30. where x.course_id is null and x.course_code=course_code and x.root_org_id=root_org_id and x.batch_id= batch_id;
  31. END IF;
  32. set course_id_temp=null;
  33. END IF;
  34. IF org_id is null THEN
  35. SELECT x.id into org_id_temp from ecs_core_org x where x.code= org_code and x.root_id=root_org_id;
  36. IF org_id_temp is not null THEN
  37. update ec_e_exam_student_tmp x set x.org_id=org_id_temp
  38. where x.org_id is null and x.org_code=org_code and x.root_org_id=root_org_id and x.batch_id= batch_id;
  39. END IF;
  40. set org_id_temp=null;
  41. END IF;
  42. commit;
  43. IF total = count THEN
  44. LEAVE read_loop;
  45. END IF;
  46. END LOOP;
  47. close cursor_temp;
  48. END