无法修改递归视图中的日期字段
Can't modify date field in the recursive view
我正在尝试使用递归视图生成一些数据,但它的工作方式与我的预期不同。每从开始日期减去一天:
with x (id, start_date, tmp) as
(
select id, start_date, 1 from my_table
union all
select id+1, start_date + tmp, tmp+1 from x where id <=5
)
select * from x
结果:
2015-03-01 00:00:00.0
2015-02-28 00:00:00.0
2015-02-27 00:00:00.0
2015-02-26 00:00:00.0
2015-02-25 00:00:00.0
然后我尝试用一个更简单的例子来测试它并得到错误:
with x (id, date_test) as
(
select 1, trunc(to_date('01/01/2015','dd/mm/yyyy')) from dual
union all
select id+1, date_test from x where id <=5
)
select * from x
错误:
Error: ORA-01790: expression must have same datatype as corresponding
expression SQLState: 42000 ErrorCode: 1790 Position: 114
查询
select 1, trunc(to_date('01/03/2015','dd/mm/yyyy'))
from dual
有效:
1 2015-01-01 00:00:00.0
实际上我使用 connect by 解决了问题,但我想知道为什么会这样。我曾经使用 Sql 服务器工作并且这种方法有效,但在 oracle 中我仍然找不到它发生的原因。
在第一个示例中,您是否可能只是因为行未按您期望的顺序出现而感到困惑?由于查询中没有明确的 ORDER BY
,您不应假定返回的第一行就是您认为的 "first" 行。
你的第二个例子就像为我写的那样(在 Oracle 11g 中),没有错误。
遗憾的是,这是由于 11.2 中的一个错误。根据 MOS 错误 11840579,已在 12.1 中修复。
我正在尝试使用递归视图生成一些数据,但它的工作方式与我的预期不同。每从开始日期减去一天:
with x (id, start_date, tmp) as
(
select id, start_date, 1 from my_table
union all
select id+1, start_date + tmp, tmp+1 from x where id <=5
)
select * from x
结果:
2015-03-01 00:00:00.0
2015-02-28 00:00:00.0
2015-02-27 00:00:00.0
2015-02-26 00:00:00.0
2015-02-25 00:00:00.0
然后我尝试用一个更简单的例子来测试它并得到错误:
with x (id, date_test) as
(
select 1, trunc(to_date('01/01/2015','dd/mm/yyyy')) from dual
union all
select id+1, date_test from x where id <=5
)
select * from x
错误:
Error: ORA-01790: expression must have same datatype as corresponding expression SQLState: 42000 ErrorCode: 1790 Position: 114
查询
select 1, trunc(to_date('01/03/2015','dd/mm/yyyy'))
from dual
有效:
1 2015-01-01 00:00:00.0
实际上我使用 connect by 解决了问题,但我想知道为什么会这样。我曾经使用 Sql 服务器工作并且这种方法有效,但在 oracle 中我仍然找不到它发生的原因。
在第一个示例中,您是否可能只是因为行未按您期望的顺序出现而感到困惑?由于查询中没有明确的 ORDER BY
,您不应假定返回的第一行就是您认为的 "first" 行。
你的第二个例子就像为我写的那样(在 Oracle 11g 中),没有错误。
遗憾的是,这是由于 11.2 中的一个错误。根据 MOS 错误 11840579,已在 12.1 中修复。