0

下面是 PL/SQL 查询的代码,当提供姓名时,我必须获取有资格教授特定课程的教师的姓名,以及他们教授这门课程的次数和最后一次(年份和学期) 当他们教授这门课程时。

我已经完成了大部分问题,但无法弄清楚如何获得 max(o.co_year) 即术语的相应详细信息

declare
gname varchar2(20);
count_id number(2);
id varchar(20);
year1 number(4);

cursor abc   // cursor 1
     is
        SELECT  i.i_gname
          into gname
        FROM INSTRUCTOR I 
        WHERE i.i_id in (
        SELECT t.i_id  FROM TeachingQualification T
        WHERE t.c_id in (SELECT c.c_id  FROM  COURSE C
        WHERE c.c_title = 'Advanced Database App')) ;


cursor bcd // cursor two
    is
        select o.i_id, count(o.i_id), max(o.co_year)
          into id, count_id, year1
        from courseoffering o
        where (o.i_id = i_id and o.c_id = 1234567)
        group by o.i_id;


Begin
open abc;
open bcd; 

loop

FETCH abc into gname;
exit when abc%NOTFOUND;

FETCH bcd into id, count_id, year1;
exit when bcd%NOTFOUND;

DBMS_OUTPUT.PUT_LINE ('NAME: ' || gname || '  Number of times taught ' || count_id || ' Year ' || year1 || );  // want to output corresponding column details for the year1 attribute.

end loop;
close bcd;
close abc;
end;

PL/SQL 我想要 MAX(O.CO_YEAR) 的对应列。怎么做?

4

2 回答 2

2

是的,我知道,但这是一项大学任务,我想用 PL/SQL 来实现。

呸!我宁愿你的大学教你正确使用PLSQL和SQL,并给你一个有意义的任务。如果你可以用 SQL 做某事,那就用 SQL 做。
另外:为什么您的光标包含INTO关键字?为什么需要循环?似乎您只期望 1 个返回值?

我对您的代码进行了一些剖析:

--instructors who are qualified to teach a course
SELECT i.i_gname --instructor name
  FROM INSTRUCTOR I 
 WHERE i.i_id IN ( SELECT t.i_id --instructor_id
                     FROM TeachingQualification T
                    WHERE t.c_id in (SELECT c.c_id --course_id
                                       FROM  COURSE C
                                      WHERE c.c_title = 'Advanced Database App'));


--instructors who taught a course, with amount and last time
SELECT o.i_id instructor_id, count(o.i_id) times_taught, max(o.co_year) last_time_taught
  FROM courseoffering o
 WHERE o.c_id = 1234567 --course_id
 GROUP BY o.i_id;

--all instructors (ID) who taught advanced database app, how many times, and last time
--consider that this may produce NO_DATA_FOUND
SELECT o.i_id instructor_id, count(o.i_id) times_taught, max(o.co_year) last_time_taught
  FROM course c
  JOIN courseoffering o 
    ON c.c_id = o.c_id
 WHERE c.c_title = 'Advanced Database App'
 GROUP BY o.i_id; 

-- i don't think teachingqualification is required. The last select providers instructor IDs.
-- There is no need to go through that table, unless it would contain extra data you'd want to
-- filter by. Since courseoffering is being queried, and it has instructors, it stands to reason
-- that those instructor are qualified to teach the course.
SELECT  (SELECT i_gname FROM instructor WHERE i_id = o.i_id) instructor
       ,count(o.i_id) times_taught
       , max(o.co_year) last_time_taught
  FROM course c
  JOIN courseoffering o 
    ON c.c_id = o.c_id
 WHERE c.c_title = 'Advanced Database App'
 GROUP BY o.i_id;

-- Look, if you do want a PLSQL block for this, go ahead.
BEGIN
   FOR r IN (SELECT  (SELECT i_gname FROM instructor WHERE i_id = o.i_id) instructor
                    ,count(o.i_id) times_taught
                    , max(o.co_year) last_time_taught
               FROM course c
               JOIN courseoffering o 
                 ON c.c_id = o.c_id
              WHERE c.c_title = 'Advanced Database App'
              GROUP BY o.i_id)
   LOOP
      DBMS_OUTPUT.PUT_LINE ('NAME: ' || r.instructor || '  Number of times taught ' || r.times_taught || ' Year ' || r.last_time_taught);
   END LOOP;
END;

哦,请给你的专栏起有意义的名字。调用它instructor_id而不是i_id例​​如。

我设置了一些示例数据:

create table course (id number(5,0), cname varchar2(50), constraint course_pk primary key (id))
/
create table courseoffering(id number(5,0), course_id number(5,0), instructor_id number(5,0), course_year number(5,0), constraint offering_pk primary key (id), constraint course_fk foreign key (course_id) references course (id))
/
insert into course values (1, 'Tech I');
insert into course values (2, 'Basic SQL');
insert into course values (3, 'Advanced SQL');
--Instructor 1
insert into courseoffering values (1, 1, 1, 2009); --Tech I
insert into courseoffering values (2, 1, 1, 2010); --Tech I
insert into courseoffering values (3, 1, 1, 2011); --Tech I
insert into courseoffering values (4, 2, 1, 2011); --Basic SQL
insert into courseoffering values (5, 2, 1, 2012); --Basic SQL
--Instructor 2
insert into courseoffering values (6, 2, 2, 2008); --Basic SQL
insert into courseoffering values (7, 2, 2, 2009); --Basic SQL
insert into courseoffering values (8, 2, 2, 2010); --Basic SQL
insert into courseoffering values (9, 3, 2, 2010); --Advanced SQL
insert into courseoffering values (10, 3, 2, 2011); --Advanced SQL
insert into courseoffering values (11, 3, 2, 2012); --Advanced SQL
insert into courseoffering values (12, 1, 2, 2009); --Tech I
insert into courseoffering values (13, 1, 2, 2010); --Tech I
commit;

运行这个:

SELECT c.cname, o.instructor_id, count(o.instructor_id) times_taught, max(o.course_year) last_time_taught
  FROM course c
  JOIN courseoffering o 
    ON c.id = o.course_id
 GROUP BY c.cname, o.instructor_id
 ORDER BY c.cname, o.instructor_id; 

产生:

CNAME          INSTRUCTOR_ID  TIMES_TAUGHT   LAST_TIME_TAUGHT
Advanced SQL   2              3              2012
Basic SQL      1              2              2012
Basic SQL      2              3              2010
Tech I         1              3              2011
Tech I         2              2              2010

您甚至可以轻松地将所需数据转换为视图。不需要 PLSQL。SQL 中只有几行。而且,如果您希望在 PLSQL 中使用它,您仍然可以使用循环来覆盖每门课程的多名讲师,或者如果您将其缩小到一门课程和一名讲师,则可以使用一些变量。始终尽量减少 SQL 和 PLSQL 上下文之间的切换。

于 2012-10-29T14:25:21.590 回答
0

Cursors in Oracle can have parameters. See http://docs.oracle.com/cd/B19306_01/appdev.102/b14261/sqloperations.htm#BABHICAF for details.

How it might work in your code:

cursor bcd(p_c_id courseoffering.c_id%type) // cursor two
is
    select o.i_id, count(o.i_id), max(o.co_year)
      into id, count_id, year1
    from courseoffering o
    where (o.i_id = i_id and o.c_id = p_c_id)
    group by o.i_id;
于 2012-10-29T08:06:11.103 回答