不知道有何问题。。貌似只插入了一条数据,郁闷中。。。
CREATE DEFINER = 'root'@'localhost' PROCEDURE `load_user_data`()
NOT DETERMINISTIC
CONTAINS SQL
SQL SECURITY DEFINER
COMMENT ''
BEGIN
declare done int default 0;
declare s_id int;
declare temp_s_id int;
declare temp_d_id int;
declare cur_s_id int;
declare cur_d_id int;
declare u_code VARCHAR(60);
declare u_name VARCHAR(60);
declare u_role_name VARCHAR(60);
declare u_dept_name VARCHAR(60);
declare role_id int; /**** 角色ID-社区用户**/
declare role_center_id int; /**** 角色ID-社区卫生中心 **/
declare s_role_id int;
declare code cursor for select user_code from `user_temp`;
DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET done = 1;
select ROLE_ID into role_id from sys_role where ROLE_NAME='社区用户';
select ROLE_ID into role_center_id from sys_role where ROLE_NAME='社区卫生中心';
open code;
repeat
fetch code into u_code;
select t.`USER_NAME`,t.`DEPT`,t.`TYPE` into u_name,u_dept_name,u_role_name from user_temp t where USER_CODE=u_code;
select STAFF_ID into temp_s_id from sys_staff where STAFF_CODE=u_code;
select u_code;
IF u_dept_name='无' THEN
set u_dept_name = u_name;
END IF;
/**---- 插入用户数据---- 1. 插入部门数据 2.插入到员工表**/
if temp_s_id is null or temp_s_id=0 then
/** 如果该部门名称为‘无’,部门名称与员工名相同 **/
/** 如果该部门在系统中已存在,不需要插入 **/
select DEPARTMENT_ID into temp_d_id from sys_department where DEPARTMENT_NAME=u_dept_name ;
/** 如果该部门在系统中不存在,需要插入一条部门数据 **/
IF temp_d_id is null then
select max(DEPARTMENT_ID) into cur_d_id from sys_department;
set cur_d_id = cur_d_id+1;
INSERT INTO `sys_department` (`DEPARTMENT_ID`, `DOMAIN_ID`,
`PARENT_DEPARTMENT_ID`, `DEPARTMENT_NAME`, `STS`) VALUES
(cur_d_id,2,2,u_dept_name,0) ;
ELSE
set cur_d_id=temp_d_id;
end if;
select max(STAFF_ID) into cur_s_id from sys_staff;
set cur_s_id=cur_s_id+1;
INSERT INTO `sys_staff` (`STAFF_ID`, `DOMAIN_ID`, `DEPARTMENT_ID`,
`STAFF_CODE`, `PASSWD`, `STAFF_NAME`, `EXP_DATE`, `STS`) VALUES
(cur_s_id,2,cur_d_id,u_code,'c4ca4238a0b923820dcc509a6f75849b',u_name,'2011-12-31',1);
select max(ID) into s_role_id from sys_staff_role ;
set s_role_id= s_role_id+1;
if u_role_name='社区卫生中心' then
insert into `sys_staff_role`(ID,STAFF_ID,ROLE_ID) values
(s_role_id,cur_s_id,role_center_id);
else
insert into `sys_staff_role`(ID,STAFF_ID,ROLE_ID) values
(s_role_id,cur_s_id,role_id);
end if;
end if;
until done end repeat;
close code;
commit;
END;