运行mysql存储过程时,记录不会插入主表

时间:2011-03-23 06:10:53

标签: mysql

我正在将程序从oracle迁移到mysql。但是记录没有插入到主表中。但是我已经成功地插入了打印消息。我不明白这里发生了什么问题.Plz帮我解决了这个问题。 这是我的程序代码:    在我的程序中我在SD_CHANGE_TMP表中声明了标志值为'N'的游标。之后我做了一些计算。基于这些值我插入主表即SD_CHANGE table.Procedure成功执行。我没有得到语法错误。调用程序后没有插入数据.Plz帮帮我.....

CREATE PROCEDURE HIS_CHANGE()
BEGIN     
   declare v_CHANGE_ID DECIMAL(25,0);
   declare v_MODIFIED_DATE DATETIME;
   declare v_PLAN_START_DATE DATETIME;
   declare v_PLAN_END_DATE DATETIME;
   declare v_ACTUAL_END_DATE DATETIME;
   declare v_ACTUAL_START_DATE DATETIME;
   declare v_APPROVAL_STATUS_ID DECIMAL(25,0);
   declare v_ASSIGNED_TO_ID DECIMAL(25,0);
   declare v_CHANGE_OWNER_ID DECIMAL(25,0);
   declare v_CLOSURE_CODE_ID DECIMAL(25,0);
   declare v_CREATE_DATE DATETIME;
   declare v_CREATED_BY_ID DECIMAL(25,0);
   declare v_DESCRIPTION VARCHAR(4000);
   declare v_ENHANCEMENT_AUDIT VARCHAR(30);
   declare v_HOST_ID DECIMAL(25,0);
   declare v_HOST_NAME VARCHAR(255);
   declare v_IMPACT VARCHAR(64);
   declare v_SEVERITY_ID DECIMAL(25,0);
   declare v_PRODUCT_CTI_ID DECIMAL(25,0);
   declare v_OPERATIONAL_CTI_ID DECIMAL(25,0);
   declare v_RAISED_LOCATION_ID DECIMAL(25,0);
   declare v_STATUS_ID DECIMAL(25,0);
   declare v_SLA_ID DECIMAL(25,0);
   declare v_SUMMARY VARCHAR(128);
   declare v_CHANGE_KEY VARCHAR(128);
   declare v_USERS_AFFECTED VARCHAR(60);
   declare v_A_SRC DECIMAL(25,0);
   declare v_TICKET_TYPE_ID DECIMAL(25,0);
   declare v_FLAG CHAR(1) ;
   declare v_A_CREATION_TIME TIMESTAMP;
   declare temp DOUBLE;
   declare t_his VARCHAR(2);
   declare t_max_d DATETIME;
   declare t_count DOUBLE DEFAULT 0;
   declare t_hosti DOUBLE;
   DECLARE duplicate_key INT DEFAULT 0; 
   declare done int default 0;
   declare done1 int default 0;
   declare done2 int default 0;
   declare no_data int default 0;
   declare ERROR int default 0;  

   DECLARE CHG_CUR CURSOR FOR SELECT * FROM SD_CHANGE_TMP WHERE  FLAG = 'N' ORDER  BY CHANGE_ID,MODIFIED_DATE;

   DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
   DECLARE continue HANDLER FOR 1062 SET duplicate_key=1;
   DECLARE continue HANDLER for 1328 SET no_data = 1;
   DECLARE continue HANDLER for SQLEXCEPTION SET ERROR = 1;


   SELECT "OPENING THE CURSOR...";
   OPEN CHG_CUR;

   SELECT "before the loop....";

   LOOP1:LOOP
   outer_block:BEGIN
     FETCH CHG_CUR INTO v_CHANGE_ID,v_MODIFIED_DATE,v_PLAN_START_DATE,v_PLAN_END_DATE,v_ACTUAL_END_DATE,v_ACTUAL_START_DATE,
        v_APPROVAL_STATUS_ID,v_ASSIGNED_TO_ID,v_CHANGE_OWNER_ID,v_CLOSURE_CODE_ID,v_CREATE_DATE,v_CREATED_BY_ID,v_DESCRIPTION,
        v_ENHANCEMENT_AUDIT,v_HOST_ID,v_HOST_NAME,v_IMPACT,v_SEVERITY_ID,v_PRODUCT_CTI_ID,v_OPERATIONAL_CTI_ID,v_RAISED_LOCATION_ID,
        v_STATUS_ID,v_SLA_ID,v_SUMMARY,v_CHANGE_KEY,v_USERS_AFFECTED,v_A_SRC,v_TICKET_TYPE_ID,v_FLAG,v_A_CREATION_TIME;
        select "Inside the loop";
        inner_block:begin
         BEGIN
            SELECT COUNT(*) INTO temp FROM SD_CHANGE WHERE  CHANGE_ID = v_CHANGE_ID;            
            IF temp = 0 THEN
               select temp;
               SET t_his = 'Y';             
            ELSE
               SELECT MAX(MODIFIED_DATE) INTO t_max_d FROM SD_CHANGE WHERE CHANGE_ID= v_CHANGE_ID;
               IF v_MODIFIED_DATE > t_max_d
               THEN
                  select temp;
                  SET t_his = 'Y';
                  UPDATE SD_CHANGE SET HIS_FLAG = 'N' WHERE  HIS_FLAG = 'Y' AND CHANGE_ID = v_CHANGE_ID;
               ELSE
                  SET t_his = 'N';
               END IF;
            END IF; 
          END;
          BEGIN                   
            select concat('Inserting into SD_CHANGE table');            

            insert into SD_CHANGE(CHANGE_ID,MODIFIED_DATE,PLAN_START_DATE,PLAN_END_DATE,ACTUAL_END_DATE,ACTUAL_START_DATE,APPROVAL_STATUS_ID,ASSIGNED_TO_ID,CHANGE_OWNER_ID,
            CLOSURE_CODE_ID,CREATE_DATE,CREATED_BY_ID,DESCRIPTION,ENHANCEMENT_AUDIT,HOST_ID,IMPACT,SEVERITY_ID,PRODUCT_CTI_ID,
            OPERATIONAL_CTI_ID,RAISED_LOCATION_ID,STATUS_ID,SLA_ID,SUMMARY,CHANGE_KEY,HIS_FLAG,USERS_AFFECTED,A_SRC,FLAG,A_CREATION_TIME,TICKET_TYPE_ID)
            values(v_CHANGE_ID,v_MODIFIED_DATE,v_PLAN_START_DATE,v_PLAN_END_DATE,v_ACTUAL_END_DATE,v_ACTUAL_START_DATE,v_APPROVAL_STATUS_ID,v_ASSIGNED_TO_ID,v_CHANGE_OWNER_ID,
            v_CLOSURE_CODE_ID,v_CREATE_DATE,v_CREATED_BY_ID,v_DESCRIPTION,v_ENHANCEMENT_AUDIT,v_HOST_ID,v_IMPACT,v_SEVERITY_ID,v_PRODUCT_CTI_ID,
            v_OPERATIONAL_CTI_ID,v_RAISED_LOCATION_ID,v_STATUS_ID,v_SLA_ID,v_SUMMARY,v_CHANGE_KEY,t_his,v_USERS_AFFECTED,v_A_SRC,'N',CURRENT_TIMESTAMP,v_TICKET_TYPE_ID);

            SELECT CONCAT('Inserted Successfully 1 ',v_change_id) as "Result";

            COMMIT;

            UPDATE SD_CHANGE_TMP SET FLAG = 'Y' WHERE  CHANGE_ID = v_CHANGE_ID AND MODIFIED_DATE = v_MODIFIED_DATE;

            IF duplicate_key=1 then
            begin
               select CONCAT('Rejected id and last mod time ',v_change_id, v_modified_date);
               UPDATE SD_CHANGE_TMP SET FLAG = 'D' WHERE CHANGE_ID = v_CHANGE_ID AND MODIFIED_DATE = v_MODIFIED_DATE;
            end;
            else
               SELECT CONCAT('Inserted Successfully 2 ',v_change_id) as "Result";
            END IF;

            SET duplicate_key=0;           

         END;       

         if no_data=1 then
           leave loop1;
         end if;
      END inner_block;

      IF (done=1) then
         leave loop1;
      end if;

      end outer_block;    

      select concat('loop ending.....');

      IF(t_count = 1000)
      THEN
         COMMIT;
         select t_count;
         SET t_count = 0;
      ELSE
         SET t_count = t_count+1;
      END IF;       

   END LOOP LOOP1;

   CLOSE CHG_CUR;

   select concat('close the cursor....');

   UPDATE TIMELOG SET ETIME = CURRENT_TIMESTAMP WHERE  PROCNAME = 'SD_CHANGE';

   COMMIT;

END;

1 个答案:

答案 0 :(得分:0)

IDFMA - 有意义答案的数据不足

您还没有提供足够的信息来帮助您。写清楚 您的问题描述,提供结构和样本数据等......

delimiter ;

drop procedure if exists insert_sd_change;

delimiter #

-- took a wild stab at your datatypes

create procedure insert_sd_change
(
in p_change_id int unsigned, 
in p_modified_date datetime, 
in p_plan_start_date datetime, 
in p_plan_end_date datetime, 
in p_actual_end_date datetime, 
in p_actual_start_date datetime,
in p_approval_status_id tinyint unsigned, 
in p_assigned_to_id int unsigned, 
in p_change_owner_id int unsigned, 
in p_closure_code_id tinyint unsigned, 
in p_create_date datetime, 
in p_created_by_id int unsigned,
in p_description varchar(255), 
in p_enhancement_audit tinyint unsigned, 
in p_host_id int unsigned, 
in p_impact varchar(255), 
in p_severity_id int unsigned, 
in p_product_cti_id int unsigned, 
in p_operational_cti_id int unsigned,
in p_raised_location_id int unsigned, 
in p_status_id tinyint unsigned, 
in p_sla_id smallint unsigned, 
in p_summary varchar(255), 
in p_change_key int unsigned, 
in p_his_flag tinyint unsigned, 
in p_users_affected int unsigned, 
in p_a_src varchar(255),
in p_ticket_type_id tinyint unsigned
)
proc_main:begin

-- p_ prefix for parameters, v_ prefix for local variables !

declare v_duplicate_key tinyint unsigned default 0;

if not exists (select 1 from sd_change where change_id = p_change_id and modified_date = p_modified_date) then

  insert into sd_change
  (
  change_id, modified_date, plan_start_date, plan_end_date, actual_end_date, actual_start_date,
  approval_status_id, assigned_to_id, change_owner_id, closure_code_id, create_date, created_by_id,
  description, enhancement_audit, host_id, impact, severity_id, product_cti_id, operational_cti_id,
  raised_location_id, status_id, sla_id, summary, change_key, his_flag, users_affected, a_src,
  flag, a_creation_time, ticket_type_id
  )
  values
  (
  p_change_id, p_modified_date, p_plan_start_date, p_plan_end_date, p_actual_end_date, p_actual_start_date,
  p_approval_status_id, p_assigned_to_id, p_change_owner_id, p_closure_code_id, p_create_date, p_created_by_id,
  p_description, p_enhancement_audit, p_host_id, p_impact, p_severity_id, p_product_cti_id, p_operational_cti_id,
  p_raised_location_id, p_status_id, p_sla_id, p_summary, p_change_key, p_his_flag, p_users_affected, p_a_src,
  'N', now(), p_ticket_type_id
  );

  update sd_change_tmp set flag = 'Y' where change_id = p_change_id and modified_date = p_modified_date;

else

 set v_duplicate_key = 1;

 update sd_change_tmp set flag = 'D' where change_id = p_change_id and modified_date = p_modified_date;

end if;

end proc_main#

delimiter ;

call insert_sd_change(...);