【问题标题】:Update entire table with sequence number in oracle用oracle中的序列号更新整个表
【发布时间】:2014-01-14 10:09:03
【问题描述】:

我想用序列号更新表,我不能创建过程或序列对象,所以需要一个更新查询。我有一个带有日期列的表,根据我需要生成一个数字的最短日期。

表格数据:-

Date_Value
----------
5th Feb 11
2nd Jan 11
11th Jan 11

After Update :- 
SrNo     Date_Value
------------------- 
1   2nd Jan 11
2   11th Jan 11
3   5th Feb 11

【问题讨论】:

  • 请展示一个示例(至少 2-3 行),说明您拥有的数据是什么样的以及您需要将其转换为什么。展示您迄今为止尝试过的内容也会有所帮助。
  • 您并没有真正将日期存储为 varchar 格式,是吗?
  • 抱歉给您带来了困惑。数据以日期格式存储,但是当我们通过视图进行 fecthing 时,我们正在使用 to_char。下面提到的两个答案都可以正常工作。为腿部工作道歉:P
  • 啊!很好(我见过太多数据库,他们实际上做了这么愚蠢的事情......)

标签: sql oracle


【解决方案1】:
merge into foo
using
(
   select rowid as rid,
          row_number() over (order by date_value) as seqno
   from foo
) t on (foo.rowid = t.rid)
when matched then update
   set srno = t.seqno;

SQLFiddle 演示:http://sqlfiddle.com/#!4/d8cc5/2

【讨论】:

    【解决方案2】:

    您的示例显示您使用自定义表示将日期存储为字符串 - 不符合日期格式。因此,我们需要将您的日期转换为日期数据类型,以便能够相应地对它们进行排序。

    SQL> create table TB_DATES
      2  (
      3    DATE_VALUE VARCHAR2(11)
      4  )
      5  /
    
    Table created
    
    SQL> 
    SQL> insert into tb_dates(date_value)
      2    select '5th Feb 11' from dual union all
      3    select '2nd Jan 11' from dual union all
      4    select '6th Feb 11' from dual union all
      5    select '11th Jan 11' from dual
      6  ;
    
    4 rows inserted
    
    SQL> commit;
    
    Commit complete
    
    SQL> select * from tb_dates;
    
    DATE_VALUE
    -----------
    5th Feb 11
    2nd Jan 11
    6th Feb 11
    11th Jan 11
    
    SQL> alter table tb_dates add srno number;
    
    Table altered
    
    QL> select * from tb_dates;
    
    DATE_VALUE        SRNO
    ----------- ----------
    5th Feb 11     null  
    2nd Jan 11     null
    6th Feb 11     null
    11th Jan 11    null
    
    SQL> merge into  tb_dates t
      2  using(
      3  select rownum rn
      4       , Date_value
      5       , rid
      6    from ( select to_date(concat(lpad(dy, 2, '0'), tr), 'dd Mon yy') dt
      7                , Date_value
      8                , rid
      9             from (select regexp_substr(substr(Date_value, 1, regexp_instr(date_value, '[[:space:]]') - 1), '[[:digit:]]+') dy
     10                        , substr(Date_value, regexp_instr(date_value, '[[:space:]]'), length(Date_value)) tr
     11                        , Date_value
     12                        , rowid rid
     13                    from tb_dates)
     14   order by 1
     15  ) ) q
     16  on (q.rid = t.rowid)
     17  when matched
     18  then update set t.srno = q.rn
     19  ;
    
    Done
    
    SQL> commit;
    
    Commit complete
    
    select * from tb_dates;
    
    DATE_VALUE        SRNO
    ----------- ----------
    5th Feb 11           3
    2nd Jan 11           1
    6th Feb 11           4
    11th Jan 11          2
    
    SQL> select * from tb_dates order by srno;
    
    DATE_VALUE        SRNO
    ----------- ----------
    2nd Jan 11           1
    11th Jan 11          2
    5th Feb 11           3
    6th Feb 11           4
    

    【讨论】:

    • 抱歉给您带来了困惑。数据以日期格式存储,但是当我们通过视图进行 fecthing 时,我们正在使用 to_char。为腿部工作道歉:P 谢谢
    猜你喜欢
    • 1970-01-01
    • 2020-01-01
    • 2021-02-18
    • 2015-06-25
    • 1970-01-01
    • 2022-01-15
    • 1970-01-01
    • 2020-11-13
    • 2011-06-23
    相关资源
    最近更新 更多