vendredi 4 décembre 2015

Updating each row in SQLite

I have this table MUNICIPIOS:

CREATE TABLE `MUNICIPIOS` (
    `ID_PROVINCIA`  TEXT,
    `ID_MUNICIPIO`  TEXT,
    PRIMARY KEY(ID_MUNICIPIO)
);

and this another one, LOCALIDADES:

CREATE TABLE `LOCALIDADES` (
    `ID_LOCALIDAD`  TEXT,
    `ID_MUNICIPIO`  TEXT,
    `ID_PROVINCIA`  TEXT,
    PRIMARY KEY(ID_LOCALIDAD)
);

In this last table, ID_PROVINCIA is NULL in all rows. I need to fill this column with the proper value, it is the ID_PROVINCIA of its ID_MUNICIPIO.

I have tried this:

UPDATE localidades L
SET    id_provincia = (SELECT M.id_provincia
                       FROM   municipios M
                       WHERE  M.id_municipio = L.id_municipio) 

but I get this error:

near "L": syntax error

I have also tried without giving an alias to LOCALIDADES table:

UPDATE localidades
SET    id_provincia = (SELECT M.id_provincia
                       FROM   municipios M
                       WHERE  M.id_municipio = localidades.id_municipio) 

It doesn't fail and it takes much time to finish (there are many rows). However, ID_PROVINCIA in LOCALIDADES is still NULL.

Aucun commentaire:

Enregistrer un commentaire