我的程序没有返回预期结果

发布于 2024-09-19 02:18:24 字数 749 浏览 4 评论 0原文

这是我的代码:

create or replace    
procedure date_report (start_date  timestamp , end_date  timestamp  )
is
  cursor cursor_audit is
    select audit_id, audit_action, audit_user, audit_date
    from customer_audit
    where audit_date between start_date and end_date
    ;
  rec cursor_audit%rowtype;
begin
  open cursor_audit;
  fetch cursor_audit into rec;
  while cursor_audit%found
  loop
    dbms_output.put_line('User : '  || rec.audit_user
                    || ', ID :'     || rec.audit_id
                    || ', Action: ' || rec.audit_action
                    || ', Date : '  || rec.audit_date );
    fetch cursor_audit into rec;
  end loop;
  close cursor_audit;
end;

我想查询特定日期之间的所有行,但它似乎没有报告任何内容。

Here is my code:

create or replace    
procedure date_report (start_date  timestamp , end_date  timestamp  )
is
  cursor cursor_audit is
    select audit_id, audit_action, audit_user, audit_date
    from customer_audit
    where audit_date between start_date and end_date
    ;
  rec cursor_audit%rowtype;
begin
  open cursor_audit;
  fetch cursor_audit into rec;
  while cursor_audit%found
  loop
    dbms_output.put_line('User : '  || rec.audit_user
                    || ', ID :'     || rec.audit_id
                    || ', Action: ' || rec.audit_action
                    || ', Date : '  || rec.audit_date );
    fetch cursor_audit into rec;
  end loop;
  close cursor_audit;
end;

I want to query all rows between specific dates but it seems to not report anything.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

倚栏听风 2024-09-26 02:18:24

不完全是你所要求的,但值得一提:你可以写得更好,即更惯用的PL/SQL,如下所示:

创建或替换

procedure date_report (start_date  timestamp , end_date  timestamp  )
is
  cursor cursor_audit is select audit_id, audit_action, audit_user, audit_date
                         from customer_audit
                         where audit_date between start_date and end_date
                     ;
begin
  for rec in cursor_audit
  loop
    dbms_output.put_line('User : '||rec.audit_user ||', ID :' ||rec.audit_id||', Action: ' 
                         || rec.audit_action||', Date : ' ||rec.audit_date );
  end loop;
end;

甚至

procedure date_report (start_date  timestamp , end_date  timestamp  )
is
begin
  for rec in (select audit_id, audit_action, audit_user, audit_date
                from customer_audit
                where audit_date between start_date and end_date)
  loop
    dbms_output.put_line('User : '||rec.audit_user ||', ID :' ||rec.audit_id||', Action: ' 
                         || rec.audit_action||', Date : ' ||rec.audit_date );
  end loop;
end;

你的问题的一个可能原因是你选择例如09-16-2010到09- 16-2010,即 09-16-2010 00:00:00 至 09-16-2010 00:00:00。要查找 2010 年 9 月 16 日的所有行,您必须明确传递一个时间(或者只需在 end_date 上加 1,这在大多数情况下就足够了)

Not exactly what you were asking for, but worth mentioning: You can write better, i.e. more idiomatic PL/SQL like this:

create or replace

procedure date_report (start_date  timestamp , end_date  timestamp  )
is
  cursor cursor_audit is select audit_id, audit_action, audit_user, audit_date
                         from customer_audit
                         where audit_date between start_date and end_date
                     ;
begin
  for rec in cursor_audit
  loop
    dbms_output.put_line('User : '||rec.audit_user ||', ID :' ||rec.audit_id||', Action: ' 
                         || rec.audit_action||', Date : ' ||rec.audit_date );
  end loop;
end;

or even

procedure date_report (start_date  timestamp , end_date  timestamp  )
is
begin
  for rec in (select audit_id, audit_action, audit_user, audit_date
                from customer_audit
                where audit_date between start_date and end_date)
  loop
    dbms_output.put_line('User : '||rec.audit_user ||', ID :' ||rec.audit_id||', Action: ' 
                         || rec.audit_action||', Date : ' ||rec.audit_date );
  end loop;
end;

A possible cause for your problem is that you select e.g. 09-16-2010 to 09-16-2010, which means 09-16-2010 00:00:00 to 09-16-2010 00:00:00. To find all rows from 09-16-2010, you have to explicitely pass a time (or just add 1 to the end_date, which is sufficient in most cases)

孤独患者 2024-09-26 02:18:24

看看有没有被检查。

替代文字

Look and see if it is checked.

alt text

唔猫 2024-09-26 02:18:24

首先,建议为变量和参数添加前缀,这样它们就不会与列名冲突。

在您的示例中,如果“customer_audit”包含 start_date 和 end_date 列,那么将优先使用这些列,而不是同名的 PL/SQL 变量或参数。

其次,原标题包含“the-parameter-always-null”。如果参数为空,则条件将不为真,并且不会返回任何行。

如果你想满足这一点,你需要编写一些逻辑,例如:

> select audit_id, audit_action, audit_user, audit_date
> from customer_audit
> where (audit_date >= i_start_date or i_start_date is null)
> and   (audit_date <= i_end_date or i_end_date is null)

Firstly, it is advisable to prefix variables and parameters so they do not conflict with column names.

In your example, if "customer_audit" contained start_date and end_date columns then these would be used in preference over PL/SQL variables or parameters of the same name.

Secondly, the original title included "the-parameter-always-null". If the parameters are null then the condition will not be true and no rows will be returned.

If you want to cater for this, you need to code in some logic like:

> select audit_id, audit_action, audit_user, audit_date
> from customer_audit
> where (audit_date >= i_start_date or i_start_date is null)
> and   (audit_date <= i_end_date or i_end_date is null)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文