更新查询在 SQL Server 中按我的预期工作,但在 Postgresql 中却不然
我有一个 ETL 过程,似乎在 SQL Server 中运行良好,但我试图将其移植到 Postgresql 并遇到一些我不理解的问题。
我在第一步中创建了一个名为 c_product
的表。然后,我想根据与名为 previous_type_year 的查找表的联接来更新
在 SQL Server 中,以下代码可以正常工作:c_Production.myValue
的所有值。
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
但是,如果我尝试对于 Postgresql,我一次又一次地为 myValue
获得相同的值。我真的不明白它在做什么。有什么提示可以告诉我为什么这两个系统以如此不同的方式处理这个查询吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 PostgreSQL 和 SQL Server 中,他们通过添加 FROM 子句对 SQL 标准进行了扩展,并且都选择以不同的方式实现它。因此,将更新从一个供应商复制到另一供应商时需要小心。
请参阅此链接中标题为“从多个来源更新”的部分。
http://www.commandprompt.com/ppbook/x7050
https://sqlserverfast.com/blog/hugo/2008/03/lets-deprecate- update-from/
在您的情况下,通过执行以下操作更新 SQL for PostgreSQL 来工作:
在我个人看来,PostgreSQL 的扩展做得更好,该扩展比 SQL Server UPDATE FROM 的工作方式更符合逻辑。
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:
In my personal opinion PostgreSQL did a better job with the extension that is more logical than how SQL Server UPDATE FROM works.