Обсуждение: SQL state 42601 FOR syntax error

Поиск
Список
Период
Сортировка

SQL state 42601 FOR syntax error

От
"Ramroth, Laurie"
Дата:

Sir/Madam:

 

The code that follows iterates on a temporary table of distinct vehicle id’s that results from a query on the normal.joined_points_speeds_orderedID table.  The normal.joined_points_speeds_orderedid table has vehicle_id, trip_id, local_ts, calc_miles_duration, calc_time_duration, and calc_speed fields and is ordered by vehicle_id. The purpose of this code is to break up a large table of point level vehicle data by vehicle_id (into several smaller tables stored as csv).  I think the code that follows should do this by copying temporary tables into csv files, however it doesn’t.  I get a FOR syntax error on the following code.

 

FOR rowNum IN SELECT DISTINCT ON (vehicle_id) vehicle_id FROM normal.joined_points_speeds_orderedID LOOP

    EXECUTE ‘CREATE  TEMP TABLE vehiclePointData_’||rowNum|| ‘AS SELECT * FROM normal.joined_points_speeds_orderedID WHERE joined_points_speeds_orderedID.vehicle_id=’||rowNum;

   EXECUTE ‘COPY vehiclePointData_’||rowNum||’TO D:/PSRC_Public_access/calculations_points_speeds/’||rowNum||’.csv delimiters ‘||’,’||’ WITH CSV HEADER’;

END LOOP;

 

The Error reads—

ERROR:  syntax error at or near “FOR”

LINE 1: FOR rowNum IN (SELECT DISTINCT ON (vehicle_id) vehicle_id …

 

********Error**********

ERROR: syntax error at or near “FOR”

SQL state: 42601

Character: 1

 

Can you spot the error in the included code?

 

Thank you.

 

Laurie Ramroth

 

Re: SQL state 42601 FOR syntax error

От
Tom Lane
Дата:
"Ramroth, Laurie" <Laurie.Ramroth@nrel.gov> writes:
> I get a FOR syntax error on the following code.

> FOR rowNum IN SELECT DISTINCT ON (vehicle_id) vehicle_id FROM normal.joined_points_speeds_orderedID LOOP
>     EXECUTE 'CREATE  TEMP TABLE vehiclePointData_'||rowNum|| 'AS SELECT * FROM normal.joined_points_speeds_orderedID
WHEREjoined_points_speeds_orderedID.vehicle_id='||rowNum; 
>    EXECUTE 'COPY vehiclePointData_'||rowNum||'TO D:/PSRC_Public_access/calculations_points_speeds/'||rowNum||'.csv
delimiters'||','||' WITH CSV HEADER'; 
> END LOOP;

> The Error reads-
> ERROR:  syntax error at or near "FOR"
> LINE 1: FOR rowNum IN (SELECT DISTINCT ON (vehicle_id) vehicle_id ...

You haven't provided us a lot of context, but I'm going to guess that
you are trying to execute this as just plain SQL.  It's not SQL.
FOR is a construct in the plpgsql procedural language; so you need to
put this into a plpgsql function.

            regards, tom lane