I’m just learning plsql and I’m having problems running simple sql queries in procedures.
I wanted to write a procedure that displays all the records from a table.
create or replace procedure display_all_students
as
begin
dbms_output.put_line('Listing all the student records');
select * from student;
end;
I get this as a result :
Warning: Procedure created with compilation errors.
What am I missing, from what I understand is that plsql is an extension to sql is there some other way of achieving this instead?
Updated code, I’m still facing the same problem. Is there a way by which we can debug these errors one by one?
-- procedure to display the table
create or replace procedure display_all_students
as
-- declarations
cursor cur_student is
select * from student;
student_record student%rowtype;
begin
dbms_output.put_line('Listing all the student records');
for student_record in cur_student
loop
dbms_output.put_line(student_record);
end loop;
end;
You cannot just
select * from student;. That doesn’t mean anything, what does it do with the returned data?Instead you need to either create a cursor which
selects ... from studentorSELECT columns INTO variables FROM student;.So for you problem you need to create a cursor which selects from student and then loop through it and output each row. For example:
The same thing written the long way is probably better to learn as a beginner as it teaches you the various aspects of cursors etc. This is the same code written the long way.
The
FOR variable IN cursorsyntax takes care of some things for you:The difference is syntax only. The actual execution and performance of both is almost identical.