使用自联接更新 Sybase SQL
Sybase SQL 中使用自连接进行更新的正确语法是什么?例如,假设您有下表 (#tmptbl):
account | client |amount | date
-------------------------------------
ACT1 | CLIENTA | 12 |2010-12-30
ACT2 | CLIENTB | 5 |2010-12-30
ACT1 | CLIENTA | 17 |2010-12-31
ACT2 | CLIENTB | 6 |2010-12-31
我想用 2010 年 12 月 30 日的金额值覆盖 2010 年 12 月 31 日的金额。
我想写这样的东西:
update old
set old.amount = new.amount
from #tmptbl old, #tmptbl new
where
/*filter old*/
old.account = 'ACT1'
and old.date = '2010-12-30'
and old.client = 'CLIENTA'
/* self-join new and old*/
and old.account = new.account
and old.client = new.client
/* filter new */
and new.date = '2010-12-31'
但看起来 Sybase 不接受“更新<>”中的别名条款。这样做的正确方法是什么?
谢谢!
What's the right syntax in Sybase SQL to do an update with a self join? E.g. assuming you have the below table (#tmptbl):
account | client |amount | date
-------------------------------------
ACT1 | CLIENTA | 12 |2010-12-30
ACT2 | CLIENTB | 5 |2010-12-30
ACT1 | CLIENTA | 17 |2010-12-31
ACT2 | CLIENTB | 6 |2010-12-31
I want to overwrite the amounts on 2010-12-31 with the amount values from 2010-12-30.
I feel like writing something like this:
update old
set old.amount = new.amount
from #tmptbl old, #tmptbl new
where
/*filter old*/
old.account = 'ACT1'
and old.date = '2010-12-30'
and old.client = 'CLIENTA'
/* self-join new and old*/
and old.account = new.account
and old.client = new.client
/* filter new */
and new.date = '2010-12-31'
But it doesn't look like Sybase accepts an alias in the 'update <>' clause. What's the proper way of doing this?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是可行的:
如果您省略要更新的表的别名,即
set amount = new.amount
,则 Sybase 会将您要更新的表与第一个匹配表关联起来在 from 子句中,因此在这种情况下,要使更新正常工作,您需要 from 来读取from #tmptbl new, #tmptbl old
。输出:
This works:
If you leave out the alias of the table you are updating, i.e.
set amount = new.amount
then Sybase associates the table you are updating with the first matching table in the from clause, so in that case for your update to work you'd need the from to readfrom #tmptbl new, #tmptbl old
.Output:
你尝试过吗
Have you tried