【问题标题】:Trouble with Update-Set-From syntax in OracleOracle 中 Update-Set-From 语法的问题
【发布时间】:2018-04-11 19:51:36
【问题描述】:

我正在尝试在 Oracle 中执行一个简单的 SQL update-set-from 语句,如下所示:

update
table1 t1
set
t1.col1=t2.col1
from
(select distinct t1.col1 from table1 t1 where t1.col2='xxx') as t2
where
t1.col1='yyy';

我之前没有使用过fromupdate-set,但是这种语法对我来说看起来不错。但是它失败并出现此错误:

Error report -
SQL Error: ORA-00933: SQL command not properly ended
00933. 00000 -  "SQL command not properly ended"
*Cause:
*Action:

我想要的结果是内部选择返回单个记录,然后将其插入/更新到 table1 的col1

另外,考虑到我已经在更新语句中使用了t1,我是否应该在内部选择语句中为table1 使用不同于t1 的别名?

【问题讨论】:

标签: sql oracle sql-update


【解决方案1】:

语法应该是

update table t1 set
  t1.col1 = (select distinct t2.col1 
             from table1 t2
             where t2.col2 = 'xxx')
where t1.col1 = 'yyy';             

注意DISTINCT 并不一定意味着SELECT 将返回单个值;如果没有,您最终会出现TOO-MANY-ROWS 错误。

【讨论】:

    猜你喜欢
    • 2015-01-04
    • 2019-03-02
    • 1970-01-01
    • 1970-01-01
    • 2015-10-03
    • 1970-01-01
    • 2017-12-24
    • 1970-01-01
    • 2021-10-14
    相关资源
    最近更新 更多