我正在将一个Oracle函数移植到Postgres PGPLSQL中..我一直在使用本指南:http://www.postgresql.org/docs/8.1/static/plpgsql.html
CREATE OR REPLACE PROCEDURE DATA_UPDATE
(mission NUMBER,
task NUMBER)
AS
BEGIN
IF mission IS NOT NULL THEN
UPDATE MISSION_OBJECTIVE MO
SET (MO.MO_TKR_TOTAL_OFF_SCHEDULED,
MO.MO_TKR_TOTAL_RECEIVERS) =
(SELECT NVL(SUM(RR.TRQ_FUEL_OFFLOAD),0),
NVL(SUM(RR.TRQ_NUMBER_RECEIVERS),0)
FROM REFUELING_REQUEST RR, MISSION_REQUEST_PAIRING MRP
WHERE MO.MSN_INT_ID = MRP.MSN_INT_ID
AND MO.MO_INT_ID = MRP.MO_INT_ID
AND MRP.REQ_INT_ID = RR.REQ_INT_ID)
WHERE MO.MSN_INT_ID = mission
AND MO.MO_INT_ID = task ;
END IF ;
COMMIT ;
END ;
我到目前为止:
CREATE OR REPLACE FUNCTION DATA_UPDATE
(NUMERIC,
NUMERIC)
RETURNS integer as '
DECLARE
mission ALIAS for $1;
task ALIAS for $2;
BEGIN
IF mission IS NOT NULL THEN
UPDATE MISSION_OBJECTIVE MO
SET (MO.MO_TKR_TOTAL_OFF_SCHEDULED,
MO.MO_TKR_TOTAL_RECEIVERS) =
(SELECT COALESCE(SUM(RR.TRQ_FUEL_OFFLOAD),0),
COALESCE(SUM(RR.TRQ_NUMBER_RECEIVERS),0)
FROM REFUELING_REQUEST RR, MISSION_REQUEST_PAIRING MRP
WHERE MO.MSN_INT_ID = MRP.MSN_INT_ID
AND MO.MO_INT_ID = MRP.MO_INT_ID
AND MRP.REQ_INT_ID = RR.REQ_INT_ID)
WHERE MO.MSN_INT_ID = mission
AND MO.MO_INT_ID = task ;
END IF;
COMMIT;
END;
' LANGUAGE plpgsql;
这是我得到的错误:
ERROR: syntax error at or near "SELECT"
LINE 1: ...OTAL_OFF_SCHEDULED, MO.MO_TKR_TOTAL_RECEIVERS) = (SELECT COA...
我不知道为什么这不起作用......有什么想法吗?
答案 0 :(得分:1)
我认为Postgres不支持更新构造(我尝试的简单测试失败并出现相同的错误)。你可能需要这样做:
CREATE OR REPLACE FUNCTION DATA_UPDATE
(mission NUMERIC,
task NUMERIC)
RETURNS void as '
DECLARE
offScheduled int4;
totalReceivers int4;
BEGIN
IF mission IS NOT NULL THEN
select COALESCE(SUM(RR.TRQ_FUEL_OFFLOAD),0),
COALESCE(SUM(RR.TRQ_NUMBER_RECEIVERS),0) into offScheduled, totalReceivers
FROM REFUELING_REQUEST RR, MISSION_REQUEST_PAIRING MRP
WHERE MO.MSN_INT_ID = MRP.MSN_INT_ID
AND MO.MO_INT_ID = MRP.MO_INT_ID
AND MRP.REQ_INT_ID = RR.REQ_INT_ID;
UPDATE MISSION_OBJECTIVE MO
SET MO.MO_TKR_TOTAL_OFF_SCHEDULED = offScheduled,
MO.MO_TKR_TOTAL_RECEIVERS = totalReceivers
WHERE MO.MSN_INT_ID = mission
AND MO.MO_INT_ID = task ;
END IF;
END;
' LANGUAGE plpgsql;
...假设我没有严重破坏逻辑;)
我冒昧地改变了参数的定义方式和返回类型(因为看起来你实际上并没有从函数中返回任何东西?)
编辑:oops,我使用了select into
的错误构造...
答案 1 :(得分:1)
PostgreSQL在其UPDATE语法中支持FROM扩展。 Find out more
所以这样的事情对你有用......
UPDATE MISSION_OBJECTIVE MO
SET MO.MO_TKR_TOTAL_OFF_SCHEDULED = COALESCE(SUM(RR.TRQ_FUEL_OFFLOAD),0),
MO.MO_TKR_TOTAL_RECEIVERS) = COALESCE(SUM(RR.TRQ_NUMBER_RECEIVERS),0)
FROM REFUELING_REQUEST RR, MISSION_REQUEST_PAIRING MRP
WHERE MRP.MSN_INT_ID = mission
AND MRP.MO_INT_ID = task
AND MRP.REQ_INT_ID = RR.REQ_INT_ID
AND MO.MSN_INT_ID = MRP.MSN_INT_ID
AND MO.MO_INT_ID = MRP.MO_INT_ID