简体   繁体   中英

For loop postgreSQL doesn't seem to work properly

I'm currently in the process of doing a huge manual work which I could solve with a simple for loop.

Here is my query :

FOR i IN 317..349 LOOP
   update ville
   set ville.id_de = i from ville, departement where ville.nom_dep_vi = departement.nom_de AND i = departement.id_de;
END LOOP;

Here is the official PostgreSQL's syntax:

FOR i IN 1..10 LOOP
-- i will take on the values 1,2,3,4,5,6,7,8,9,10 within the loop
END LOOP;

I always get an error saying "Query failed PostgreSQL said: syntax error at or near "i"". If anyone could help that'd be appreciated.

No need for a loop, just use a single update statement:

update ville
  set ville.id_de = departement.id_de
from departement -- do NOT repeat ville here!
where ville.nom_dep_vi = departement.nom_de 
  AND departement.id_de between 317 and 349

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM