简体   繁体   English

SQL-从复杂的查询更新Oracle 10g数据库

[英]SQL - updating Oracle 10g database from a complex query

trying to update a single column based on criteria. 尝试根据条件更新单个列。 The rows that need to be updated are returned by the query 查询返回需要更新的行

SELECT 
it.objectid,
it.versionnumber,
it.itemnumber "Item Number", 
it.itemtype,
itcovprem.basisofsettlement,
itcovprem.coverage "Coverage"
FROM 
itemcoveragesandpremiums itcovprem,
items it,
policies pl
WHERE 
pl.objectid = it.parentobjectid AND 
pl.policytype in ('H','F') AND
it.objectid = itcovprem.itemobjectid AND
it.itemtype in ('SHOA','SHOB','SHOC','SHOD','SHOK','SHOL') and
it.versionnumber = itcovprem.itemversionnumber AND
((itcovprem.coverage like ('A - Dwg Bldg%')) or
(itcovprem.coverage like ('#42 - Repl Cost Plus%')) or
(itcovprem.coverage like ('#42 - Limited GRC%'))) and
it.effectivedate >= TO_DATE('01-JAN-2006', 'DD-MON-YYYY') and
exists (select * from itemcoveragesandpremiums icp where icp.itemobjectid = it.objectid and icp.itemversionnumber = it.versionnumber and icp.coverage like ('#42%')) and
itcovprem.basisofsettlement not in ('LGRC')

I've tried a few options to convert this to an update query, but no joy. 我尝试了一些选项将其转换为更新查询,但没有任何乐趣。

I get Error - line 3 - SQL command not properly ended when using 我收到错误-第3行-使用时,SQL命令未正确结束

update itemcoveragesandpremiums
set basisofsettlement = 'LGRC'
from itemcoveragesandpremiums as itcovprem
inner join items as it
on it.objectid = itcovprem.itemobjectid and it.versionnumber = itcovprem.itemversionnumber
inner join policies as pl
on pl.objectid = it.parentobjectid 
where [cut, rest of query was below]

I get Error - line 6 - missing right parenthesis when trying use an inline query 我收到错误-第6行-尝试使用内联查询时缺少右括号

update
(
SELECT 
itcovprem.basisofsettlement as OLD
FROM 
itemcoveragesandpremiums as itcovprem inner join items as it on it.objectid = itcovprem.itemobjectid and it.versionnumber = itcovprem.itemversionnumber inner join policies AS pl on pl.objectid = it.parentobjectid 
WHERE [query snipped]
) t
set t.old = 'LGRC'

Seems that SQL*Plus just wants to stop looking at the update before it gets to the meat of my select query. 似乎SQL * Plus只是想在了解我的select查询之前停止查看更新。 I'm not sure if I'm formatting it incorrectly or going at it from the wrong direction. 我不确定是否格式化错误或方向错误。 Any advice? 有什么建议吗?

In your first attempt, the error at line 3 is because update doesn't support a from or join clause. 第一次尝试,第3行的错误是因为update不支持fromjoin子句。 In your second attempt the immediate error at line 6 is because you're trying to alias a table with as itcovprem ; 在您的第二次尝试中,第6行的立即错误是因为您尝试使用as itcovprem为表as itcovprem but you can only use as for column aliases, not for table aliases . 但是您只能将as用于列别名,而不能用于表别名 (The first attempt is trying to do that too, but it isn't getting as far as encountering that problem). (第一个尝试也是尝试这样做,但是它并没有遇到这个问题。) But you can't generally update an inline-view with joins anyway, so it would still error with that - something like an ORA-01779. 但是您通常无论如何都无法使用连接来更新内联视图,因此它仍然会出错-类似于ORA-01779。

You would need to do something like: 您将需要执行以下操作:

update itemcoveragesandpremiums
set basisofsettlement = 'LGRC'
where (itemobjectid, itemversionnumber, basisofsettlement, coverage) in (
  select it.objectid,
    it.versionnumber,
    itcovprem.basisofsettlement,
    itcovprem.coverage
  from ...
)

Assuming that itemobjectid, itemversionnumber, basisofsettlement, coverage identifies a row sufficiently such that you don't risk updating anything you shouldn't. 假设itemobjectid, itemversionnumber, basisofsettlement, coverage充分标识了一行,这样您就不必冒任何不应该更新的风险。 It might be safer to add a rowid to the select and use that for the update instead to avoid ambiguity: rowid添加到select并将其用于更新可能更安全,以避免歧义:

update itemcoveragesandpremiums
set basisofsettlement = 'LGRC'
where rowid in (
  select itcovprem.rowid as target_rowid
  from ...
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM