0

I am converting SQL stored procedure to PostgreSQL stored function. In this stored function inside Update there is From clause.

I want to know how to use From inside Update? Because I am getting error 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 '    

    $$

1 Answer 1

1

Move the JOIN condition with account table and stg_account to WHERE clause. Also, you need not refer to account in the SET.

Further, prefer shorter aliases (like one or 2 letters) rather than using complete table names.

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.