Oracle相关更新
我在使用 Oracle 10g 语法进行相关更新时遇到困难。我正在 PL/SQL 过程中处理此代码。
我会这样处理 SQL Server 中的问题:
UPDATE table_a a
SET a.prov_id=pn.prov_id,
a.step=1
from (
SELECT p.prov_id
FROM note n
INNER JOIN provider p ON n.author_id=p.user_id
where n.enc_id=a.enc_id
AND TRUNC(n.note_time)=a.attr_date
) pn
等效的 Oracle 语法是:
UPDATE table_a a
SET a.prov_id=(
SELECT p.prov_id
FROM note n
INNER JOIN provider p ON n.author_id=p.user_id
where n.enc_id=a.enc_id
AND TRUNC(n.note_time)=a.attr_date
),
a.step=1
WHERE EXISTS (
SELECT *
FROM note n
INNER JOIN provider p ON n.author_id=p.user_id
where n.enc_id=a.enc_id
AND TRUNC(n.note_time)=a.attr_date
)
这实际上运行子查询两次吗?还有比这更缩写的语法吗?
I'm having difficulty with Oracle 10g syntax for a correlated UPDATE. I am processing this code in a PL/SQL procedure.
I would approach the problem in SQL Server as so:
UPDATE table_a a
SET a.prov_id=pn.prov_id,
a.step=1
from (
SELECT p.prov_id
FROM note n
INNER JOIN provider p ON n.author_id=p.user_id
where n.enc_id=a.enc_id
AND TRUNC(n.note_time)=a.attr_date
) pn
The equivalent Oracle syntax is:
UPDATE table_a a
SET a.prov_id=(
SELECT p.prov_id
FROM note n
INNER JOIN provider p ON n.author_id=p.user_id
where n.enc_id=a.enc_id
AND TRUNC(n.note_time)=a.attr_date
),
a.step=1
WHERE EXISTS (
SELECT *
FROM note n
INNER JOIN provider p ON n.author_id=p.user_id
where n.enc_id=a.enc_id
AND TRUNC(n.note_time)=a.attr_date
)
Does this in fact run the sub-query twice? Is there a more-abbreviated syntax than this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
至于子查询是否运行两次,使用EXPLAIN PLAN。
我喜欢使用 merge 命令而不是 update 来进行这些相关的更新,例如:(未经测试,如果您想要经过测试的答案,请提供 DDL 和插入语句。)
联视图,例如:
有时您可以更新内 查看版本并不总是有效。无法找到有关错误的信息,但本质上内联视图需要有一个唯一的键,Oracle 可以将其与有问题的表联系起来。
As to whether the sub-query runs twice, use EXPLAIN PLAN.
I like to use the merge command instead of update for these correlated updates, something like: (Not tested, if you want a tested answer, please provide DDL and insert statements.)
Sometimes you can update an inline view, something like:
The inline view version won't always work. Can't find infor on the error, but essentially the inline view needs to have a unique key that Oracle can tie back to the tables in question.