3

I have an ETL process that seems to work well in SQL Server, but I am trying to port it to Postgresql and running into something I don't understand.

I create a table called c_production in the first step. I then want to update all the values of c_production.myValue based on a join with a lookup table called previous_type_year. In SQL Server the following code works fine:

update c_production 
    set myValue =  PTY.myValue
FROM c_production CP JOIN previous_type_year PTY
    on CP.StFips = PTY.STFIPS
    AND CP.comcode = PTY.comcode
    AND CP.Year = PTY.YEAR

However if I try this with Postgresql I get the same value over and over for myValue. I really don't grok what it's doing. Any tips that would point me to why these two systems treat this query so differently?

0

1 Answer 1

3

In PostgreSQL and SQL Server they added an extension to the SQL standard by adding the FROM clause and both chose to implement it differently. So you need to be careful when copying your updates from one vendor to another.

See section titled "Updating from Several Sources" from this link. http://www.commandprompt.com/ppbook/x7050

https://sqlserverfast.com/blog/hugo/2008/03/lets-deprecate-update-from/

In your case update the SQL for PostgreSQL to work by doing this:

UPDATE c_production 
    set myValue =  PTY.myValue
FROM previous_type_year PTY
WHERE c_production.StFips  = PTY.STFIPS
  AND c_production.comcode = PTY.comcode
  AND c_production.Year    = PTY.YEAR;

In my personal opinion PostgreSQL did a better job with the extension that is more logical than how SQL Server UPDATE FROM works.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for both the good reference and the detailed answer. I figured it was something like this, but my google foo was not able to extract a good answer.
Quick note. I think you can still alias the table: UPDATE c_production CP set myValue = PTY.myValue FROM previous_type_year PTY WHERE CP.StFips = PTY.STFIPS AND CP.comcode = PTY.comcode AND CP.Year = PTY.YEAR;

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.