如何在PostgreSQL Update语句中使用From子句

问题描述 投票:0回答:1

我正在将SQL存储过程转换为PostgreSQL存储函数。在Update里面的这个存储函数中有From子句。

我想知道如何在Update里面使用From?因为我得到错误table name "account" specified more than once

CREATE OR REPLACE FUNCTION ETL_Insert_ModifyData( instID numeric)

  RETURNS void
  LANGUAGE 'sql'

AS $$ 

        --Account
        UPDATE account
        SET  namount = stg_account.namount, 
             slocation = stg_account.sLocation, 
             ndept_id = stg_account.ndept_id ,
             account.naccount_cpc_mapping_id = stg_account.naccount_cpc_mapping_id
        FROM account 
            INNER JOIN stg_account ON account.naccount_id = stg_account.naccount_id
            INNER JOIN department ON stg_account.ndept_id = department.ndept_id
            INNER JOIN accountcpcmapping ON stg_account.naccount_cpc_mapping_id = accountcpcmapping.naccount_cpc_mapping_id

        WHERE account.ninst_id = instID
            AND department.ninst_id = instID
            AND accountcpcmapping.ninst_id = instID

        --print 'Account completed '    

    $$
postgresql stored-functions
1个回答
1
投票

JOIN条件与account表和stg_account移动到WHERE子句。另外,你不需要在account中引用SET

此外,更喜欢较短的别名(如一个或两个字母),而不是使用完整的表名。

UPDATE account
SET
    namount   = stg_account.namount,
    slocation = stg_account.sLocation,
    ndept_id  = stg_account.ndept_id ,
    naccount_cpc_mapping_id = stg_account.naccount_cpc_mapping_id
FROM stg_account
INNER JOIN department ON stg_account.ndept_id = department.ndept_id
INNER JOIN accountcpcmapping ON
    stg_account.naccount_cpc_mapping_id = accountcpcmapping.naccount_cpc_mapping_id
WHERE account.naccount_id   = stg_account.naccount_id
    AND account.ninst_id    = instID
    AND department.ninst_id = instID
    AND accountcpcmapping.ninst_id = instID
© www.soinside.com 2019 - 2024. All rights reserved.