views:

51

answers:

2

I am porting an Oracle function into Postgres PGPLSQL.. I have been using this guide: 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 ;

I've got it this far:

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;

This is the error I get:

ERROR:  syntax error at or near "SELECT"
LINE 1: ...OTAL_OFF_SCHEDULED, MO.MO_TKR_TOTAL_RECEIVERS) = (SELECT COA...

I do not know why this isn't working... any ideas?

+1  A: 

I don't think Postgres supports that update construct (the simple test I tried fails with the same error). You might have to do something like this:

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;

... assuming I haven't mangled the logic too badly ;)

I took the liberty of changing the way the parameters are defined, and the return type (since it looks like you're not actually returning anything from the function?)

Edit: oops, I used the wrong construct for select into...

rjohnston
+1  A: 

PostgreSQL supports a FROM extension in its UPDATE syntax. Find out more.

So something like this could work for you....

  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
APC