En utilisant postgres 8.4, mon objectif est de mettre à jour le tableau existant:
CREATE TABLE public.dummy
(
address_id SERIAL,
addr1 character(40),
addr2 character(40),
city character(25),
state character(2),
Zip character(5),
customer boolean,
supplier boolean,
partner boolean
)
WITH (
OIDS=FALSE
);
Au départ, j'ai testé ma requête à l'aide de l'instruction insert:
insert into address customer,supplier,partner
SELECT
case when cust.addr1 is not null then TRUE else FALSE end customer,
case when suppl.addr1 is not null then TRUE else FALSE end supplier,
case when partn.addr1 is not null then TRUE else FALSE end partner
from (
SELECT *
from address) pa
left outer join cust_original cust
on (pa.addr1=cust.addr1 and pa.addr2=cust.addr2 and pa.city=cust.city
and pa.state=cust.state and substring(cust.Zip,1,5) = pa.Zip )
left outer join supp_original suppl
on (pa.addr1=suppl.addr1 and pa.addr2=suppl.addr2 and pa.city=suppl.city
and pa.state=suppl.state and pa.Zip = substring(suppl.Zip,1,5))
left outer join partner_original partn
on (pa.addr1=partn.addr1 and pa.addr2=partn.addr2 and pa.city=partn.city
and pa.state=partn.state and pa.Zip = substring(partn.Zip,1,5) )
where pa.address_id = address_id
étant débutant, je ne parviens pas à convertir en instruction update, c’est-à-dire à mettre à jour les lignes existantes avec les valeurs renvoyées par l’instruction select. Toute aide est grandement appréciée.
Postgres permet:
UPDATE dummy
SET customer=subquery.customer,
address=subquery.address,
partn=subquery.partn
FROM (SELECT address_id, customer, address, partn
FROM /* big hairy SQL */ ...) AS subquery
WHERE dummy.address_id=subquery.address_id;
Cette syntaxe n'est pas du SQL standard, mais elle est beaucoup plus pratique pour ce type de requête que le SQL standard. Je crois qu'Oracle (au moins) accepte quelque chose de similaire.
Vous utilisez la syntaxe UPDATE FROM
.
UPDATE
table T1
SET
column1 = t2.column1
FROM
table t2
INNER JOIN table t3 USING (column2)
WHERE
t1.column2 = t2.column2;
Références
S'il n'y a pas de gains de performances en utilisant une jointure, je préfère les expressions de table communes (CTE) pour la lisibilité:
WITH subquery AS (
SELECT address_id, customer, address, partn
FROM /* big hairy SQL */ ...
)
UPDATE dummy
SET customer = subquery.customer,
address = subquery.address,
partn = subquery.partn
FROM subquery
WHERE dummy.address_id = subquery.address_id;
IMHO un peu plus moderne.
update json_source_tabcol as d
set isnullable = a.is_Nullable
from information_schema.columns as a
where a.table_name =d.table_name
and a.table_schema = d.table_schema
and a.column_name = d.column_name;