Postgres 8.4を使って、私の目標は既存のテーブルを更新することです:
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
);
最初に私は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
私はupdate文に変換することに失敗しています。任意の助けは大歓迎です。
Postgresは可能にします:
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;
この構文は標準SQLではありませんが、標準SQLよりもこのタイプの問合せにははるかに便利です。オラクルは(少なくとも)同様のことを受け入れていると思います。
あなたはUPDATE FROM
構文に従っています。
UPDATE
table T1
SET
column1 = t2.column1
FROM
table t2
INNER JOIN table t3 USING (column2)
WHERE
t1.column2 = t2.column2;
参考文献
結合を使用してもパフォーマンスが向上しない場合は、読みやすくするためにCommon Table Expressions(CTE)を使用します。
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;
私見はもう少し現代的。
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;