postgres 9.0.13更新值(选择值)

时间:2017-04-06 08:26:28

标签: postgresql-9.0

基于文档https://www.postgresql.org/docs/9.1/static/sql-update.html我不想做的事情(在该页面的最底部)

=====

UPDATE accounts SET (contact_last_name, contact_first_name) =
    (SELECT last_name, first_name FROM salesmen
     WHERE salesmen.id = accounts.sales_id);

=====

有没有其他方法可以做到这一点?

    db=> update "Template" set ("MsgCategoryName","FromAddress","FromName","ToAddress","ToName","BccAddress","EmailSubject","EmailHTML","EmailPlanText") = (Select "MsgCategoryName","FromAddress","FromName","ToAddress","ToName","BccAddress","EmailSubject","EmailHTML","EmailPlanText" from "Template" where "AccountID" = 1016020479 and "LocaleID" = 'nl' and "TmplName" = 'Invoice/Nobrand/PDF') where "AccountID" = 1017069459 and "TmplName" = 'Invoice/Nobrand/PDF' and "LocaleID" = 'nl';
ERROR:  syntax error at or near "Select"
LINE 1: ...s","EmailSubject","EmailHTML","EmailPlanText") = (Select "Ms...

db=> select version();
                                                      version                                                      
-------------------------------------------------------------------------------------------------------------------
 PostgreSQL 9.0.13 on x86_64-unknown-linux-gnu, compiled by GCC gcc (GCC) 4.4.6 20110731 (Red Hat 4.4.6-3), 64-bit
(1 row)
                                                         ^

1 个答案:

答案 0 :(得分:0)

我没有要检查此旧版本,但我认为您无法使用CTE进行更新(what you can for sure with 9.1),在这种情况下,您可以plpgsql

do
$$
declare _r record;
begin
  for _r in (SELECT id,last_name, first_name FROM salesmen JOIN accounts on salesmen.id = accounts.sales_id) loop
    UPDATE accounts SET contact_last_name = _r.last_name, contact_first_name = _r.first_name where sales_id = _r.id;
  end loop;
end;
$$
;