我的程序没有返回预期结果
这是我的代码:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不完全是你所要求的,但值得一提:你可以写得更好,即更惯用的PL/SQL,如下所示:
创建或替换
甚至
你的问题的一个可能原因是你选择例如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
or even
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)
看看有没有被检查。
Look and see if it is checked.
首先,建议为变量和参数添加前缀,这样它们就不会与列名冲突。
在您的示例中,如果“customer_audit”包含 start_date 和 end_date 列,那么将优先使用这些列,而不是同名的 PL/SQL 变量或参数。
其次,原标题包含“the-parameter-always-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: