【问题标题】:updating random dates to a single column (dates repeates)将随机日期更新为单列(日期重复)
【发布时间】:2013-07-15 20:24:23
【问题描述】:

好的,首先我确定特定日期的日期整数,然后我需要将随机值更新到dpm_dateto 列:

查询:

select to_char(to_date('15/05/2013','dd/mm/yyyy'), 'J') from dual;

结果:

2456428

现在,我尝试使用以下查询更新 dpm_dateto 列:

update t_dailypm
set dpm_dateto = 
     (select to_date(trunc(dbms_random.value(2456428,2456428+76)), 'J') from dual)
where dpm_loc = 'P2' and dpm_department like '%IN%';

结果:

900 rows updated.

但是,问题在于dpm_dateto 列中的每个不同值都使用相同的日期更新。我无法理解这一点。请帮忙。

【问题讨论】:

    标签: sql oracle random checksum newid


    【解决方案1】:

    不要使用子查询。
    只需设置=your_expression,如下例所示。
    Oracle 优化子查询并只计算一次,但表达式将针对每一行计算:

    create table xyz(
      abc date
    );
    
    insert into xyz
    select sysdate from dual
    connect by level < 6;
    
    select * from xyz;
    
    ABC    
    --------
    13/07/17 
    13/07/17 
    13/07/17 
    13/07/17 
    13/07/17 
    

    现在:

    update xyz set abc = to_date(trunc(dbms_random.value(2456428,2456428+76)), 'J');
    
    select * from xyz;
    
    ABC    
    --------
    13/06/14 
    13/07/23 
    13/07/26 
    13/06/24 
    13/07/10
    

    【讨论】:

    • 感谢更新 kordirko..我刚刚删除了子查询,瞧!!...再次感谢!
    • P.S. - 用简单的 to_date(trunc.. 结构替换了 select(to_date(trunc...
    猜你喜欢
    • 2019-09-16
    • 2016-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-03
    • 2022-01-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多