Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, everyone. I am translating a sql stored procedure to postgre. And I successfully did it, but when I tried to test it, it throw me an error:

ERROR: table name "my_factors" specified more than once CONTEXT: SQL statement "update my_factors set factor = (my_factors.to_weight/dni_totals.dni_sum) from my_factors join dni_totals on my_factors.transport_order = dni_totals.to_id"


The code from pqslq function is above, I executed the stored procedure in sql and it executed successfully. The code from sql stored procedure is here:

update #myFactors set Factor = (#myFactors.TOWeight/#DNITotals.DNISum)
from #myFactors join #DNITotals on #myFactors.TransportOrder = #DNITotals.TOId 


Can anyone give me an advice how to handle this error?

What I have tried:

I don't know what to do, I haven't tried anything.
Posted
Updated 24-Jun-19 23:33pm

1 solution

Try
SQL
UPDATE my_factors
    SET factor = my_factors.to_weight/dni_totals.dni_sum
    FROM dni_totals
    WHERE my_factors.transport_order = dni_totals.to_id;

PostgreSQL doesn't like the SET table.column syntax, so you use the unadorned column name there. Additionally, you only need to specify table names if it would be ambiguous which table the column comes from. Whether you omit the table names or not would be an issue of style.

There's a quick tutorial on update/join here: PostgreSQL UPDATE Join with A Practical Example[^]
 
Share this answer
 
Comments
Member 13992723 25-Jun-19 5:38am    
Thanks
Maciej Los 25-Jun-19 15:39pm    
5ed!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900