0

I'm trying to execute a query within a loop which is within another loop, which is within an anonymous code block.

I'm trying to run this query

query := 'CREATE VIEW hopsPartialDistance AS '
            ||'SELECT AvgDistance '
            ||'FROM Distance '
            ||'WHERE PlanetOrigin = ' || rHops.PlanetOrigin
            ||' AND PlanetDestination = ' || rHops.PlanetDestination; 
      EXECUTE query;

But I keep getting the error

relation "hopspartialdistance" already exists

I then run

\d hopspartialdistance;

And the output is

Did not find any relation named "hopspartialdistance".

So I don't know where the error is coming from.

Here's the full function

DO 
$$
DECLARE 
routeDistance real := 0.0;
hopDistance real := 0.0;
rRoute record;
rHops record;
query text := '';

BEGIN
  FOR rRoute IN 
  SELECT MonitoringKey FROM TradingRoute
  LOOP
    query := 'CREATE VIEW PortsOfCall AS '
            ||'SELECT PlanetID, VisitOrder '
            ||'FROM EnrichedCallsAt '
            ||'WHERE MonitoringKey = ' || rRoute.MonitoringKey
            ||' ORDER BY VisitOrder';  
    EXECUTE query;
    
    CREATE VIEW Hops AS
    SELECT A.PlanetID AS PlanetOrigin, 
    B.PLanetID AS PlanetDestination
    FROM PortsOfCall A INNER JOIN PortsOfCall B ON A.VisitOrder + 1 = B.VisitOrder;
    
    routeDistance = 0.0;
    
  

    FOR rHops IN 
    SELECT PlanetOrigin, PlanetDestination FROM Hops
    LOOP
      query := 'CREATE VIEW hopsPartialDistance AS '
            ||'SELECT AvgDistance '
            ||'FROM Distance '
            ||'WHERE PlanetOrigin = ' || rHops.PlanetOrigin
            ||' AND PlanetDestination = ' || rHops.PlanetDestination; 
      EXECUTE query;
  
      hopDistance = (SELECT SUM(AvgDistance) FROM  hopsPartialDistance) ;        
      routeDistance = routeDistance + hopDistance;

    END LOOP;
    INSERT INTO RouteLength (RouteMonitoringKey, RouteTotalDistance) 
    SELECT rRoute.MonitoringKey, routeDistance;

    DROP VIEW Hops
    CASCADE;

    DROP VIEW PortsOfCall
    CASCADE;

  END LOOP;
END;
$$;
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
coderoftheday
  • 1,987
  • 4
  • 7
  • 21
  • And a `SELECT * FROM hopspartialdistance`? – nbk Apr 30 '22 at 16:06
  • @nbk Still says no relation exist – coderoftheday Apr 30 '22 at 16:13
  • Pretty sure it has to do with: "... execurte a query within a loop which is within another loop, which is within an annonymous code block." . You will need to show the complete function, I'm betting you are doing `CREATE VIEW hopsPartialDistance ...` more then once in the function. FYI, no point in doing `hopsPartialDistance` as without it being double quoted it is going to be folded to all lower case per the error message: `hopspartialdistance`. – Adrian Klaver Apr 30 '22 at 16:13
  • @AdrianKlaver I've added the full code – coderoftheday Apr 30 '22 at 16:15
  • you are looping and creating a new view with the same name over and over, and as this produces an error the code will rollback – nbk Apr 30 '22 at 16:16
  • @nbk but the PortsOfCall View has no problem – coderoftheday Apr 30 '22 at 16:18
  • 3
    You don't `DROP VIEW hopsPartialDistance` like you do the other views, so it exists when the `DO` is run again. – Adrian Klaver Apr 30 '22 at 16:19
  • FYI, you can do `CREATE TEMPORARY VIEW ...` per [CREATE VIEW](https://www.postgresql.org/docs/current/sql-createview.html). – Adrian Klaver Apr 30 '22 at 16:23
  • Drop your view then excute your query – Aman Apr 30 '22 at 18:19

0 Answers0