【问题标题】:the cursor doesn't print any data - two parameters procedure光标不打印任何数据 - 两个参数过程
【发布时间】:2021-08-20 01:22:55
【问题描述】:

我有一个任务要解决。

创建一个程序,基于该程序将列出在定义的期间(任何变量)内已经受雇过一次的所有员工(job_history 表)。报告将说明:employee_id、first_name、last_name、start_date、end_date。

如果在给定期间没有雇员,则处理该案例。

调用 1997 年到 2007 年的过程。

程序已编译,当我调用它时,答案是:PL/SQL 程序成功完成。但我的数据库中没有数据。

我的代码在这里:

create or replace procedure zamestnanci
  (s_date in job_history.start_date%type, e_date in job_history.end_date%type)
is 
begin
  for cur_r in 
    (select employees.employee_id, employees.first_name, employees.last_name 
     from employees join job_history on employees.employee_id = job_history.employee_id
     where start_date = s_date and end_date = e_date
    )
  loop
    dbms_output.put_line(cur_r.employee_id ||', '|| cur_r.first_name ||', '|| cur_r.last_name ||', '||
     s_date ||', '|| e_date );
  end loop;
  exception when no_data_found then
            dbms_output.put_line('error');
end zamestnanci;

exec zamestnanci('01.01.97', '31.12.07');

你能帮帮我吗?谢谢。

【问题讨论】:

  • 在 IDE 中启用 DMBS 输出,运行程序后您应该能够在编辑器窗口中看到 put_line 语句。
  • dbms_output 是否有效,例如dbms_output.put_line('Hello')?还是只是这个程序?能不能在调试器中跟踪一下,看看是否进入循环?
  • 顺便说一句,exception when no_data_found then dbms_output.put_line('error'); 不会被调用,因为没有 select into 或集合查找,如果有,那真的不是最好的处理方法。
  • start_dateend_date 是日期吗?还是弦乐?您正在传递字符串,但名称暗示它们应该是日期。实际上是否有员工在 1997 年 1 月 1 日开始并在 2007 年 12 月 31 日离开?他们似乎不太可能。该问题要求在提供的时间间隔内的某个时间点受雇的任何人 - 考虑可能发生的潜在方式。一种方法是 start_date 在 1997-01-01 之前,end_date 在 1997-01-01 之后(或缺失,具体取决于您的数据模型的工作方式)。还有其他条件需要考虑。
  • @WiliamRobertson - 谢谢。是的 dbms_output 完全有效,它只是这个过程。我不知道如何在调试器中跟踪它。

标签: sql database oracle plsql cursor


【解决方案1】:

您的代码获取在 '01.01.97' 开始工作并在 '31.12.07' 离职的员工

试试下面的代码。还要注意日期格式

create or replace procedure zamestnanci
  (s_date in job_history.start_date%type, e_date in job_history.end_date%type)
is 
begin
  for cur_r in 
    (select employees.employee_id, employees.first_name, employees.last_name 
     from employees join job_history on employees.employee_id = job_history.employee_id
     where start_date between s_date and e_date
     or end_date between s_date and e_date
     or s_date between start_date and end_date
     or e_date between start_date and end_date
    )
  loop
    dbms_output.put_line(cur_r.employee_id ||', '|| cur_r.first_name ||', '|| cur_r.last_name ||', '||
     s_date ||', '|| e_date );
  end loop;
  exception when no_data_found then
            dbms_output.put_line('error');
end zamestnanci;

exec zamestnanci(to_date('01.01.1997', 'dd.mm.yyyy'), to_date('31.12.2007', 'dd.mm.yyyy')); 

【讨论】:

  • 如果它有效,你为什么不接受这个答案,@Kristina?
猜你喜欢
  • 2021-08-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多