Oracle到Postgres转换的故障排除

时间:2019-01-08 02:33:37

标签: plpgsql postgresql-9.5

已将独立过程从Oracle转换为Postgres,但不确定为什么即使成功编译了代码也会出现运行错误

将以下代码从Oracle转换为Postgres

CREATE OR REPLACE FUNCTION ssp2_pcat.pop_hoa_contracts_for_prod(
)
RETURNS void
LANGUAGE 'plpgsql'

COST 100
VOLATILE 
AS $BODY$

DECLARE 
C1 CURSOR for
SELECT MARKET_CODE, CONTRACT_COMBO_ID, COUNT(*) FROM 
ssp2_pcat.VPF_HOA_CONTRACTS_FOR_PROD A
WHERE start_Date IN
  (SELECT MAX(start_date)
  FROM VPF_HOA_CONTRACTS_FOR_PROD b
  WHERE A.MARKET_CODE     = b.MARKET_CODE
  AND A.CONTRACT_COMBO_ID = b.CONTRACT_COMBO_ID
  AND A.CONTRACT_ID       = B.CONTRACT_ID
  AND b.start_date       <= current_date
  AND b.end_date          > current_date )
  GROUP BY MARKET_CODE, CONTRACT_COMBO_ID 
  ORDER BY MARKET_CODE, CONTRACT_COMBO_ID; 

C2 CURSOR(iMktCode VARCHAR, iCombo integer) for
SELECT MARKET_CODE, CONTRACT_COMBO_ID, CONTRACT_ID 
FROM ssp2_pcat.VPF_HOA_CONTRACTS_FOR_PROD A
WHERE start_Date IN
  (SELECT MAX(start_date)
  FROM ssp2_pcat.VPF_HOA_CONTRACTS_FOR_PROD b
  WHERE A.MARKET_CODE     = b.MARKET_CODE
  AND A.CONTRACT_COMBO_ID = b.CONTRACT_COMBO_ID
  AND A.CONTRACT_ID       = B.CONTRACT_ID
  AND b.start_date       <= current_date
  AND b.end_date          > current_date  )
  AND MARKET_CODE       = iMktCode
  AND CONTRACT_COMBO_ID = iCombo
  ORDER BY MARKET_CODE, CONTRACT_COMBO_ID, START_DATE;
Contracts     VARCHAR(32000);
Contract_Val1 VARCHAR(4000) := NULL;
Contract_Val2 VARCHAR(4000) := NULL;
Contract_Val3 VARCHAR(4000) := NULL;
Contract_Val4 VARCHAR(4000) := NULL;
Contract_Val5 VARCHAR(4000) := NULL;
Contract_Val6 VARCHAR(4000) := NULL;
Contract_Val7 VARCHAR(4000) := NULL;
Contract_Val8 VARCHAR(4000) := NULL;
Num           INTEGER;
Cont_Num      INTEGER;
l_start TIMESTAMP :=  clock_timestamp();
l_end TIMESTAMP :=  clock_timestamp();
Time_Taken    VARCHAR(20);
                    i record;
                    j record;

BEGIN
l_start  :=  clock_timestamp();
DELETE FROM ssp2_pcat.HOA_CONTRACTS_KH; 
 FOR i IN C1 LOOP 
 BEGIN
 Num       := 0;
  Contracts := NULL;
  Cont_Num  := 1;        
 FOR j IN C2 (i.MARKET_CODE, i.CONTRACT_COMBO_ID) LOOP 
    Num         := Num + 1;
    IF Num       = 1 THEN
      Contracts := '|' || j.CONTRACT_ID || '|';
    ELSE
      IF LENGTH(Contracts || j.CONTRACT_ID || '|') > 4000 THEN
 PERFORM ssp2_pcat.Assign (Cont_Num, SUBSTRING(Contracts, 1, 
 LENGTH(Contracts)-1));
        Num       := 1;
        Contracts := '|' || j.CONTRACT_ID || '|';
        Cont_Num  := Cont_Num + 1;
      ELSE
        Contracts := Contracts || j.CONTRACT_ID || '|';
      END IF;
    END IF;
  END LOOP;                        
  PERFORM ssp2_pcat.Assign (Cont_Num, Contracts);
  IF Cont_Num > 5 THEN
    raise notice'%', ('MARKET_CODE: ' || i.MARKET_CODE || ', CONTRACT_COMBO_ID: ' || i.CONTRACT_COMBO_ID || ' has more than 32K in size. These Contracts are left out: ' || Contracts);
  END IF;
  INSERT INTO HOA_CONTRACTS_KH
    (
      MARKET_CODE,
      CONTRACT_COMBO_ID,
      CONTRACT_ID,
      CONTRACT_ID2,
      CONTRACT_ID3,
      CONTRACT_ID4,
      CONTRACT_ID5,
      LAST_UPDATED
    )
    VALUES
    (
      i.MARKET_CODE,
      i.CONTRACT_COMBO_ID,
      Contract_Val1,
      Contract_Val2,
      Contract_Val3,
      Contract_Val4,
      Contract_Val5,
      CURRENT_TIMESTAMP::TIMESTAMP(0)
    );
  Contract_Val1 := NULL;
  Contract_Val2 := NULL;
  Contract_Val3 := NULL;
  Contract_Val4 := NULL;
  Contract_Val5 := NULL;
  Contract_Val6 := NULL;
  Contract_Val7 := NULL;
  Contract_Val8 := NULL;

EXCEPTION
WHEN OTHERS THEN
  ROLLBACK;
  raise notice'%', ('1) POP_HOA_CONTRACTS_FOR_PROD: ' || SQLERRM);

END;
END LOOP;           
RAISE NOTICE 'Function excution time Took: %', l_start;
RAISE NOTICE 'Function excution time Took: %',l_end-l_start;
SELECT l_end-l_start INTO Time_Taken;
raise notice'%',('POP_HOA_CONTRACTS_FOR_PROD Took: ' || Time_Taken );
EXCEPTION
 WHEN OTHERS THEN
 raise notice'%', ('2) POP_HOA_CONTRACTS_FOR_PROD: ' || SQLERRM);

END;
$BODY$;

代码已成功编译,但出现如下运行时错误,

注意:2)POP_HOA_CONTRACTS_FOR_PROD:无法在PL / pgSQL中开始/结束事务

调试了整个代码,看起来我仍然无法识别问题,当我不熟悉此数据库时,谁能帮助我进一步了解Postgres。在单元测试中发现它没有调用代码中提到的assign函数,

0 个答案:

没有答案