日期按行递增

时间:2017-09-28 21:45:03

标签: oracle

我有一个表(在Oracle数据库中),其中Date列为空(NULL)。表有100行。我希望从aug-15-2017开始填写(插入或更新)日期列,并在下一行中将日期增加一天。

我解释一下 - 第一行日期应该是8月15日至2017年,第二行日期应该是8月16日至2017年,第三行日期应该是8月17日至2017年等等... 请帮助我。

感谢。

2 个答案:

答案 0 :(得分:1)

UPDATE table
SET dt =  TO_DATE('AUG-14-2017','MON-DD-YYY') + ROWNUM
WHERE dt is null

答案 1 :(得分:0)

如果您有另一列(即java.lang.IllegalArgumentException: Async support must be enabled on a servlet and for all filters involved in async request processing. This is done in Java code using the Servlet API or by adding "<async-supported>true</async-supported>" to servlet and filter declarations in web.xml. Also you must use a Servlet 3.0+ container 列),您希望确保日期的顺序基于此时您可以使用:

id

不保证在没有MERGE INTO table_name dst USING ( SELECT ROWID rid, ROW_NUMBER() OVER ( ORDER BY id ) AS rn FROM table_name ) src ON ( dst.ROWID = src.RID ) WHEN MATCHED THEN UPDATE SET dt = DATE '2017-08-14' + rn; 子句的情况下使用ROWNUM按您希望的顺序生成行。您可以在以下示例中看到差异:

SQL Fiddle

Oracle 11g R2架构设置

ORDER BY

查询1

CREATE TABLE table_name ( id, dt1, dt2 ) AS
  SELECT LEVEL, CAST( NULL AS DATE ), CAST( NULL AS DATE )
  FROM   DUAL
  CONNECT BY LEVEL <= 10
  ORDER BY DBMS_RANDOM.VALUE;

UPDATE table_name
SET dt1 = DATE '2017-08-14' + ROWNUM
WHERE dt1 IS NULL;

MERGE INTO table_name dst
USING (
  SELECT ROWID rid,
         ROW_NUMBER() OVER ( ORDER BY id ) AS rn
  FROM   table_name
) src
ON ( dst.ROWID = src.RID )
WHEN MATCHED THEN
  UPDATE SET dt2 = DATE '2017-08-14' + rn;

<强> Results

SELECT * FROM table_name ORDER BY id