【问题标题】:Oracle - update only one row without using rowidOracle - 仅更新一行而不使用 rowid
【发布时间】:2016-05-19 08:42:23
【问题描述】:

我正在尝试update view 表,但只有第一个结果。 我不能使用rowid - 不能在view 表上工作。 有没有办法只更新第一个row?正如我所说的使用rowid 解决方案无法正常工作。 select查询示例:

select addr
from addrView
where (tl = '7' and tr = '2')

返回 4 results,但在使用 update 时:

update addrView
set home='current'
where (tl = '7' and tr = '2')

我还想更新第一行。

【问题讨论】:

  • 使用 where rownum = 1
  • 第一行,根据什么排序逻辑?
  • 基于结果集。
  • @MarcusH - 结果查询中的rownum?
  • 在更新查询的 where 子句中,添加 rownum = 1

标签: oracle


【解决方案1】:

ROWID 是数据库中每一行的唯一标识符。

ROWNUM 是结果集中每一行的唯一标识符。

您应该使用ROWNUM 版本,但您需要ORDER BY 来强制执行排序顺序,否则您将无法保证查询返回的“第一”行是什么,您可能会更新另一行。

update addrView
set home='current'
where (tl, tr) = (
   select tl, tr 
   from (select tl, tr
         from addrView 
         where (tl = '7' and tr = '2')
         order by col_1
             , col_2
             , col_3 etc.
        ) result_set
    where rownum = 1);

但是,如果您不关心查询返回的第一行中的数据是什么,那么您只能使用rownum = 1

update addrView
set home = 'current'
where (tl = '7' and tr = '2')
    and rownum = 1;

【讨论】:

  • 尝试并发布结果。
猜你喜欢
  • 1970-01-01
  • 2017-03-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-18
  • 1970-01-01
  • 2012-08-29
相关资源
最近更新 更多