本文介绍了进一步处理给定 dbms_output 中返回的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

**我已经创建了 plsql 程序,它将特定日期的输出日期作为 dbms_output现在我想计算 dbms_output 的两个日期之间的差异,我该如何使用它dbms_outputenglishcalendar - lag(englishcalendar)(按 englishcalendar 排序)**

**I have created plsql program which will give the output date of certain date as dbms_outputNow i want to calculate the difference between two dates of dbms_output, how can i use this in dbms_outputenglishcalendar - lag(englishcalendar)(over order by englishcalendar)**

SET SERVEROUTPUT ON;
declare

start_date    date:='01-02-2019';
end_date       date:='01-05-2019';

cursor c_data is
    SELECT
        cregflow6.*
    FROM
    cregflow6
    WHERE
    englishcalendar BETWEEN start_date
    AND  end_date
    and  m_flg ='M'
    or englishcalendar= end_date
    or englishcalendar= start_date ;

type t__data is table of c_data%rowtype index by binary_integer;
t_data t__data;

begin

open c_data;
loop
    fetch c_data bulk collect into t_data limit 10000;

    exit when t_data.count = 0;

    for i in t_data.first .. t_data.last loop
        dbms_output.put_line(t_data(i).englishcalendar);    
    end loop;

end loop;
close c_data;

end;
/

DBMS_OUTPUT 示例

01-02-2019
12-02-2019
14-03-2019
13-04-2019
01-05-2019

预期在 DBMS_OUTPUT 中

01-02-2019          0
12-02-2019          11          [01-02-2019-12-02-2019  ]
14-03-2019          33          [12-02-2019 -14-03-2019 ]
13-04-2019          32          [14-03-2019 -13-04-2019 ]
01-05-2019          17          [13-04-2019 - 01-05-2019]   

开始日期和结束日期可能因不同情况而异

推荐答案

您可以声明一个变量,用于保存上次日期.您可以在下一次迭代中使用它.

you can declare a variable where you will save last date. you can use it in the next iteration.

declare
  v_last_date date; -- is null
  ...
begin
  ...

  for i in t_data.first .. t_data.last loop
    if v_last_date is null then

      dbms_output.put_line(to_char(t_data(i).englishcalendar,'dd-mm-yyyy')||' 0' ); 
    else
      dbms_output.put_line(to_char(t_data(i).englishcalendar,'dd-mm-yyyy')||' ' 
                          ||to_char(t_data(i).englishcalendar - v_last_date)
                          ||' [' ||to_char(t_data(i).englishcalendar,'dd-mm-yyyy')||'-'
                          || to_char(v_last_date ,'dd-mm-yyyy')||']' ); 
    end if;
    v_last_date := t_data(i).englishcalendar; -- save a date of the iteration   
  end loop;
end;

我没有测试过.

这篇关于进一步处理给定 dbms_output 中返回的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 03:54