我的存储过程出了什么问题,它没有执行主逻辑就结束了

时间:2019-07-14 11:30:09

标签: mysql stored-procedures

我已将不同的存储过程从Oracle导出到MySql中,但是此存储过程存在问题,它在没有执行的情况下结束。

我曾尝试从表中移动数据转储,但仍无法正常工作

CREATE DEFINER = 'root'@'%'
PROCEDURE MOB_INSURANCE.PRC_INITIATE_INSURANCE_PAYMENT(
                             in PARAM_BEN_CNIC varchar(4000),
                             in PARAM_INITIATOR_MSISDN varchar(4000),
                             in PARAM_BENEFICIARY_MSISDN varchar(4000),
                             in PARAM_INITIATOR_CNIC varchar(4000),
                             out PARAM_TID varchar(4000),
                             in PARAM_FEE double,
                             in PARAM_PREMIUM double,
                             -- param_plan_id in INTEGER,
                             in PARAM_COMPANY_CODE varchar(4000),
                             in PARAM_PRODUCT_CODE varchar(4000),
                             in PARAM_PLAN_CODE varchar(4000),
                             in PARAM_EFFECTIVE_DATE datetime,
                             in PARAM_DURATION double, -- float
                             in PARAM_TRANS_AMOUNT double, -- flaot
                             in PARAM_CHANNEL varchar(4000),
                             in PARAM_CREATED_BY varchar(4000),
                             in PARAM_CREATED_IP varchar(4000),
                             out PARAM_RET_CODE varchar(4000),
                             out PARAM_RET_DESC varchar(4000))
sp_lbl:begin

   declare PARAM_EXPIRY DATETIME;
   declare COMP_ID DOUBLE;
   declare PRODUCT_ID DOUBLE;
   -- p_id NUMBER;
   declare PLAN_ID DOUBLE;
   declare PLAN_DURATION double;
   declare PLAN_FEE double;
   declare PLAN_PREMIUM double;
   declare PLAN_TYPE VARCHAR(50);
   declare TRANSACTION_AMOUNT double;
   declare PREVIOUSLYPURCHASEDCOUNT DOUBLE;
   -- param_newtid VARCHAR2(50 BYTE);

  DECLARE EXIT HANDLER FOR SQLEXCEPTION BEGIN
  begin
    rollback;
    set PARAM_RET_CODE = '1';
    set  PARAM_RET_DESC = 'INVALIDINPUT';
    -- set PARAM_RET_DESC = CONCAT(IFNULL(SQLERRM, '') , IFNULL(DBMS_UTILITY.FORMAT_ERROR_BACKTRACE, ''));
  end;
 -- END;

   set PARAM_RET_CODE = '2';
   -- DBMS_OUTPUT.PUT_LINE('i am here!');
   -- param_tid:='495';

  -- Input validations

  -- Company Code validation
  IF PARAM_COMPANY_CODE IS NULL OR PARAM_COMPANY_CODE = '' THEN
    sp_lbl1:loop
    set PARAM_RET_CODE = '1';
    set PARAM_RET_DESC = 'Company Code is required';
    set PARAM_TID = '';
    Leave sp_lbl1;
    end loop sp_lbl1;
  ELSE
      select('Company Code is valid');
      -- Check company code existence in the database
      BEGIN
            DECLARE EXIT HANDLER FOR NOT FOUND BEGIN
            set COMP_ID = NULL;
            END;
        SELECT ID INTO COMP_ID FROM INSURANCE_COMPANY WHERE CODE = PARAM_COMPANY_CODE AND STATUS = 'Active';
                 END;

      -- Check if company is registered
      IF (COMP_ID IS NULL) THEN
      sp_lbl2:loop
          set PARAM_RET_CODE = '1';
          set PARAM_RET_DESC = 'Company Code is not registered/active with the system';
          set PARAM_TID = '';
          Leave sp_lbl2;
          end loop sp_lbl2;
      END IF;

  END IF;

  -- Product Code validation
  IF PARAM_PRODUCT_CODE IS NULL OR PARAM_PRODUCT_CODE = '' THEN
    sp_lbl3:loop
    set PARAM_RET_CODE = '1';
    set PARAM_RET_DESC = 'Product Code is required';
    set PARAM_TID = '';
    Leave sp_lbl3;
    end loop sp_lbl3;
  ELSE
      select(CONCAT('Product Code is valid' , IFNULL(COMP_ID, '') , IFNULL(PARAM_PRODUCT_CODE, '')));
      -- Check prodcut code existence in the database
      BEGIN
            DECLARE EXIT HANDLER FOR NOT FOUND BEGIN
            BEGIN
              set PRODUCT_ID = NULL;
              select('Exception Product Id');
            END;
            END;
            DECLARE EXIT HANDLER FOR SQLEXCEPTION BEGIN
            BEGIN
              set PRODUCT_ID = NULL;
              select(CONCAT('Exception Product Id' , IFNULL(SQLERRM, '') , IFNULL(DBMS_UTILITY.FORMAT_ERROR_BACKTRACE, '')));
            END;
            END;
      -- product_id := 0;
        -- SELECT p.INSURANCE_PRODUCT_ID INTO product_id FROM INSURANCE_PRODUCTS p WHERE ( p.STATUS = 'Active' AND p.COMPANY_ID = comp_id AND p.product_code = param_product_code); -- AND p.STATUS = 'Active' 
         SELECT INSURANCE_PRODUCT_ID INTO PRODUCT_ID  FROM INSURANCE_PRODUCTS WHERE COMPANY_ID=COMP_ID AND PRODUCT_CODE = PARAM_PRODUCT_CODE AND STATUS = 'Active' ;
         select(CONCAT('Product Id : ' , IFNULL(PRODUCT_ID, '')));
       -- product_id := 43;
                 END;



      -- Check if product is registered
      IF (PRODUCT_ID IS NULL) THEN
          sp_lbl4:loop
          set PARAM_RET_CODE = '1';
          set PARAM_RET_DESC = 'Product Code is not registered/active with the Company code in the system';
          set PARAM_TID = '';
          Leave sp_lbl4;
          end loop sp_lbl4;
      END IF;

  END IF;

   -- Plan Code validation
  IF PARAM_PLAN_CODE IS NULL OR PARAM_PLAN_CODE = '' THEN
    sp_lbl5:loop
    set PARAM_RET_CODE = '1';
    set PARAM_RET_DESC = 'Plan Code is required';
    set PARAM_TID = '';
    Leave sp_lbl5;
    end loop sp_lbl5;
  ELSE
      select('Plan Code is valid');
      -- Check prodcut code existence in the database
      BEGIN
            DECLARE EXIT HANDLER FOR NOT FOUND BEGIN
            set PLAN_ID = NULL;
            END;
        SELECT INSURANCE_PLAN_ID, DURATION INTO PLAN_ID, PLAN_DURATION FROM INSURANCE_PLANS WHERE INSURANCE_PRODUCT_ID = PRODUCT_ID AND PLAN_CODE = PARAM_PLAN_CODE AND STATUS = 'Active';
                 END;

      -- Check if plan is registered
      IF (PLAN_ID IS NULL) THEN
          sp_lbl6:loop
          set PARAM_RET_CODE = '1';
          set PARAM_RET_DESC = 'Plan Code is not registered/active with the Product in the system';
          set PARAM_TID = '';
          Leave sp_lbl6;
          end loop sp_lbl6;
      END IF;

  END IF;

   -- Plan Duration Validation       
  IF PARAM_DURATION IS NULL THEN
    sp_lbl6:loop
    set PARAM_RET_CODE = '1';
    set PARAM_RET_DESC = 'Duration is required';
    set PARAM_TID = '';
    Leave sp_lbl6;
    end loop sp_lbl6;
  ELSE
      select('Plan Duration is provided');

      -- Check if duraiton provided is same defined in database
      IF (PLAN_DURATION IS NULL) THEN
          sp_lbl7:loop
          set PARAM_RET_CODE = '1';
          set PARAM_RET_DESC = 'Plan Duration is not defined in the system';
          set PARAM_TID = '';
          Leave sp_lbl7;
          end loop sp_lbl7;
      ELSE

          IF (PLAN_DURATION != PARAM_DURATION) THEN
          sp_lbl8:loop
          set PARAM_RET_CODE = '1';
          set PARAM_RET_DESC = 'Plan Duration is not the same that is defined in the system';
          set PARAM_TID = '';
         -- Leave sp_lbl; 
          leave sp_lbl8;
         end loop sp_lbl8;
          END IF;

      END IF;

  END IF;

   -- Plan Fee / Premium, transactionAmount validation
  IF PARAM_FEE IS NULL THEN
    sp_lbl9:loop
    set PARAM_RET_CODE = '1';
    set PARAM_RET_DESC = 'Plan Fee is required';
    set PARAM_TID = '';
    Leave sp_lbl9;
    end loop sp_lbl9;
    END IF;

  -- Plan Fee / Premium, transactionAmount validation
  IF PARAM_PREMIUM IS NULL THEN
    sp_lbl10:loop
    set PARAM_RET_CODE = '1';
    set PARAM_RET_DESC = 'Plan Premium is required';
    set PARAM_TID = '';
    Leave sp_lbl10;
    end loop sp_lbl10;
    END IF;

  IF PARAM_PREMIUM IS NOT NULL AND PARAM_FEE IS NOT NULL THEN
      select('Plan Fee/Premium is valid');
      -- Check plan fee/premium existence in the database
      BEGIN
            DECLARE EXIT HANDLER FOR NOT FOUND BEGIN
          sp_lbl11:loop
          set PLAN_FEE = NULL;
            set PLAN_PREMIUM = NULL;
            set PLAN_TYPE = NULL;
            leave sp_lbl11;
            end loop sp_lbl11;
            END;
        SELECT FEE, PREMIUM,FEETYPE INTO PLAN_FEE, PLAN_PREMIUM,PLAN_TYPE FROM PLAN_PRICING WHERE INSURANCE_PLAN_ID = PLAN_ID AND END_TIME IS NULL;
                 END;

      -- Check if plan is registered
      IF (PLAN_FEE IS NULL) THEN
          sp_lbl12:loop
          set PARAM_RET_CODE = '1';
          set PARAM_RET_DESC = 'Plan Fee is not defined in the system';
          set PARAM_TID = '';
          Leave sp_lbl12;
          end loop sp_lbl12;

      END IF;

       -- Check if plan is registered
      IF (PLAN_PREMIUM IS NULL) THEN
          sp_lbl13:loop
          set PARAM_RET_CODE = '1';
          set PARAM_RET_DESC = 'Plan Premium is not defined in the system';
          set PARAM_TID = '';
          Leave sp_lbl13;
          end loop sp_lbl13;

      END IF;

      -- Check if plan is registered
      IF (PLAN_TYPE IS NULL) THEN
          sp_lbl14:loop
          set PARAM_RET_CODE = '1';
          set PARAM_RET_DESC = 'Plan Fee type is not defined in the system';
          set PARAM_TID = '';
          Leave sp_lbl14;
          end loop sp_lbl14;

      END IF;

       -- Check if plan premium is registered
      IF (PLAN_PREMIUM != PARAM_PREMIUM) THEN
          sp_lbl15:loop
          set PARAM_RET_CODE = '1';
          set PARAM_RET_DESC = 'Plan Premium is not the same which is defined in the system';
          set PARAM_TID = '';
          Leave sp_lbl15;
          end loop sp_lbl15;

      END IF;

       -- Check if plan fee is registered
      IF (PLAN_FEE != PARAM_FEE) THEN
          sp_lbl16:loop
          set PARAM_RET_CODE = '1';
          set PARAM_RET_DESC = 'Plan Fee is not the same which is defined in the system';
          set PARAM_TID = '';
          Leave sp_lbl16;
          end loop sp_lbl16;

      END IF;

      -- Check if trnsaction amount is sum of fee and premium
      IF PLAN_TYPE = 'Percentage Amount' THEN
          -- Plan fee is percentage of premium
          set PLAN_FEE = (PLAN_FEE/100) * PLAN_PREMIUM;
      END IF; 

      IF ((PLAN_PREMIUM + PLAN_FEE) != PARAM_TRANS_AMOUNT) THEN
          sp_lbl17:loop
          set PARAM_RET_CODE = '1';
          set PARAM_RET_DESC = 'Sum of Fee and Premium is not equal to transaction amount provided.';
          set PARAM_TID = '';
          Leave sp_lbl17;
          end loop sp_lbl17;

      END IF;


  END IF; -- outer if condition for param_fee

   -- Check if CNIC has already subscribed this plan
  SELECT COUNT(*) into PREVIOUSLYPURCHASEDCOUNT
  FROM PURCHASED_INSURANCE
  WHERE BENEFICIARY_MSISDN=PARAM_BENEFICIARY_MSISDN AND INSURANCE_PLAN_ID=PLAN_ID AND EXPIRY_DATE>NOW() AND STATUS!='Refunded' AND STATUS!='Unpaid';
  select(CONCAT('Previously Purchased Count ' , IFNULL(PREVIOUSLYPURCHASEDCOUNT, '')));
  IF (PREVIOUSLYPURCHASEDCOUNT > 0) THEN
          sp_lbl18:loop
          set PARAM_RET_CODE = '1';
          set PARAM_RET_DESC = 'The plan has already been subscribed by the beneficiary MSISDN';
          set PARAM_TID = '';
          Leave sp_lbl18;
          end loop sp_lbl18;
  END IF;

   SELECT SEQ_TRANSACTION.nextval INTO PARAM_TID FROM DUAL;
   -- DBMS_OUTPUT.PUT_LINE('PARAM_RET_DESC = ' || PARAM_RET_DESC);
   -- DBMS_OUTPUT.PUT_LINE('PARAM_RET_DESC = ' || param_tid);
   SELECT (PARAM_EFFECTIVE_DATE+PARAM_DURATION) INTO PARAM_EXPIRY FROM DUAL;
   INSERT INTO PURCHASED_INSURANCE (TID,FEE,EFFECTIVE_DATE,EXPIRY_DATE,PREMIUM,BENEFICIARY_CNIC,
                                     INITIATOR_MSISDN,INSURANCE_PLAN_ID,BENEFICIARY_MSISDN,INITIATOR_CNIC,CHANNEL, CREATED_BY,CREATED_IP,CREATED_DATETIME,STATUS,TRANSACTION_AMOUNT) 


   VALUES(PARAM_TID,PARAM_FEE,PARAM_EFFECTIVE_DATE,PARAM_EXPIRY,PARAM_PREMIUM,PARAM_BEN_CNIC,
          PARAM_INITIATOR_MSISDN,PLAN_ID,PARAM_BENEFICIARY_MSISDN,PARAM_INITIATOR_CNIC,PARAM_CHANNEL,PARAM_CREATED_BY, PARAM_CREATED_IP,SYSDATE(),'Unpaid',PARAM_TRANS_AMOUNT);

   INSERT INTO TRANSACTION_HISTORY (TID,START_TIME,END_TIME,STATUS,TRANSACTION_AMOUNT,CREATED_BY,CREATED_IP,CREATED_DATETIME)
   VALUES(PARAM_TID,PARAM_EFFECTIVE_DATE,NULL,'Unpaid',PARAM_TRANS_AMOUNT,PARAM_CREATED_BY, PARAM_CREATED_IP,SYSDATE()) ;
   COMMIT;

   set PARAM_RET_CODE = '0';

   -- leave sp_lbl;
END;
end

我只是在结果中看不到任何值,但是如果它在工作,那么它将在异常情况下返回01并带有无效输入,在工作情况下返回00并带有事务ID

1 个答案:

答案 0 :(得分:0)

没有尝试运行它,但是仅查看它,我看到有2个变量正在存储您存储的proc中发生的事情。

out PARAM_RET_CODE varchar(4000)
out PARAM_RET_DESC varchar(4000)

这2个变量是您存储的proc的实际输出。

可能以哪种条件为准,但是通过打印这两个变量将使您对该条件有一些了解,因为该存储的proc不返回任何内容。在存储的过程的末尾打印它们,您将得到答案。