是的,我知道,但这是一项大学任务,我想用 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 上下文之间的切换。