使用for ..循环从Student表中检索学生姓氏为“Black”的所有行。显示这些行。
解: 我成功完成了a,b和c部分,如下所示:
DROP TABLE IF EXISTS students;
CREATE TABLE students(
StudentId NUMBER(3),
LName VARCHAR2(30),
FNAme VARCHAR2(30),
Major VARCHAR2(30));
INSERT INTO students VALUES(130,'Black','Judy','Accounting');
INSERT INTO students VALUES(132,'Maw','Richard','Marketing');
INSERT INTO students VALUES(134,'Bessner','Bryan','Finance');
INSERT INTO students VALUES(136,'Shaw','Tim','Accounting');
INSERT INTO students VALUES(138,'Black','Conrad','Sales');
COMMIT;
DECLARE
cursor c_students IS
--for rec in (select * from students where
select * from students where major like 'Accounting';
v_sid Number(3);
v_lname VARCHAR2(30);
v_fname VARCHAR2(30);
v_major VARCHAR2(30);
BEGIN
open c_students;
Loop
Fetch c_students into v_sid, v_lname, v_fname, v_major;
IF c_students%NOTFOUND Then
Exit;
End If;
dbms_output.put_line(v_sid||' '||v_lname||' '||v_fname||' '||v_major);
End Loop;
close c_students;
End;
/
=============================================== ==================================
现在最后一部分,在c之后,声明我需要完成我上面用for循环做的同样的事情,我不能在我的生活中弄清楚如何在for循环中做到这一点。我知道for循环没有声明并在Begin部分开始,但我不知道从那里去哪里。任何帮助将不胜感激。谢谢!
答案 0 :(得分:1)
用以下内容替换开环等。然后,您可以通过rec
变量引用光标的列。
For rec in c_students
Loop
...
End loop;