i'm a beginner at pl-sql and i am trying to write a code of a function to read in a coursename and display the lecturerName, coursename, and the title for which matches with the coursename.
However i am unable to get a decent output disregarding the different methods which i've tried, and i have gotten quite a few different error, currently below is the code that is able to compile but does not give any results.
Can anyone help me with this function and tell me where did i go wrong?.
Set echo on
set serveroutput on
CREATE OR REPLACE FUNCTION Courses(coursename IN VARCHAR2) RETURN VARCHAR2
IS
results VARCHAR2(100);
l VARCHAR2(30);
c VARCHAR2(30);
t VARCHAR2(30);
BEGIN
FOR course IN(select lecturerName, coursename, title into l,c,t from course where Coursename = coursename)
LOOP
results := results || l || c || t;
END LOOP;
RETURN results;
END Courses;
/
SELECT Courses('SQL') from dual;
Courses('SQL')
If you want the function to return all matches based on the course name, then the result will be similar to a table. I'm guessing you don't want to return 1 match, there may be more in your table. Also you may not want all the matches to be treated as one row, i.e. all of them merged together.
For this you need a pipelined function which returns a dataset, represented by a custom type.
First, create the TYPE:
The function becomes:
For input table:
and query:
Produces the output: