我的以下陈述出了什么问题:
UPDATE TableToUpdate SET ColumnToUpdate = (
SELECT ColumnWithNewValues
FROM (
SELECT ColumnWithNewValues, ROWNUM AS N
FROM Table1 t1, Table2 t2 -- join tables
WHERE t2.Schluessel = t1.Schluessel -- join condition
AND t1.DateFrom <= TableToUpdate.Date -- <==== Error, reference to TableToUpdate
AND t1.DatumTo >= TableToUpdate.Date
-- ... some other conditions, not important here ...
) tmp
WHERE tmp.N = 5 -- Use the fifth row to update the row of TableToUpdate
)
执行此操作后,我将从oracle收到错误:
ORA-00904: "TableToUpdate"."Date": Ungültiger Bezeichner
在英语中,我认为这意味着:
ORA-00904: "TableToUpdate"."Date": Invalid identifier
所以我似乎无法从SELECT-Statement中的相关子查询引用TableToUpdate。在MSSQL下,这可以替换oracle特定的ROWNUM 当然是一门等同的技术。
有人可以帮助我吗?
答案 0 :(得分:0)
您在子查询中引用最深两层,到最外层的表。限制是你只能提到一个级别。因此错误信息。
您可以通过将update语句重写为merge语句来规避此限制。例如,未经测试,如下:
merge into tabletoupdate t
using ( select datefrom
, datumto
, ColumnWithNewValues
from ( select t1.datefrom
, t1.datumto
, ColumnWithNewValues
, rownum as n
from table1 t1
inner join table2 t2 on (t2.Schluessel = t1.Schluessel) -- join condition
--where ... some other conditions, not important here ...
--order by ... some columns here, otherwise rownum is meaningless
) tmp
where tmp.n =5 -- Use the fifth row to update the row of TableToUpdate
)
on ( t1.DateFrom <= t.Date
and t1.DatumTo >= t.Date
)
when matched then
update set t.columntoupdate = tmp.columnwithnewvalues