Oracle Procedure and multi value of optional param - sql

I'm writing a procedure on Oracle. I need to use it for a search.
The form search have 8 params, each params can contains many values.
For exemple you can add two movies title (you can choose to not type whole title).
you also can tyoe a year and not a title..
I don't know if I can have multi values for my parameters ?
I want to build only on sql query for it but it is possible ? Because my where clause containt 8 params.. I'm lost with this search !
I have this procedure (doesn't worked)
create or replace procedure listerFilms (
unTitreFilm Film.titre%TYPE DEFAULT NULL,
uneAnneeMin Film.annee%TYPE DEFAULT NULL,
uneAnneeMax Film.annee%TYPE DEFAULT NULL,
uneVoFilm Film.langue%TYPE DEFAULT NULL,
unPaysProd Pays.pays%TYPE DEFAULT NULL,
unGenreFilm Genre.genre%TYPE DEFAULT NULL,
unNomRea Equipe_Tournage.nomComplet%TYPE DEFAULT NULL,
unNomActeur Equipe_Tournage.nomComplet%TYPE DEFAULT NULL)
is
titreFilm Film.titre%TYPE;
anneeFilm Film.annee%TYPE;
cursor lignesFilm(leTitreFilm Film.titre%TYPE, laAnneeMin Film.annee%TYPE, laAnneeMax Film.annee%TYPE, laVoFilm Film.langue%TYPE, lePaysProd Pays.pays%TYPE, leGenreFilm Genre.genre%TYPE, leNomRea Equipe_Tournage.nomComplet%TYPE, leNomActeur Equipe_Tournage.nomComplet%TYPE) is
select distinct
f.titre, f.annee
from
Film f, Pays p, Genre g, Equipe_Tournage rea, Equipe_Tournage act
where
f.titre like '%'||leTitreFilm||'%' and f.annee >= laAnneeMin and f.annee <= laAnneeMax
and f.langue like '%'||laVoFilm||'%' and p.pays like '%'||lePaysProd||'%'
and g.genre like '%'||leGenreFilm||'%' and rea.nomComplet like '%'||leNomRea||'%'
and act.nomComplet like '%'||leNomActeur||'%';
begin
open lignesFilm(unTitreFilm,uneAnneeMin, uneAnneeMax, uneVoFilm, unPaysProd, unGenreFilm, unNomRea, unNomActeur);
DBMS_OUTPUT.PUT_LINE('---------------------');
DBMS_OUTPUT.PUT_LINE('-- Liste des Films --');
DBMS_OUTPUT.PUT_LINE('---------------------');
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('Aucun film ne correpond au(x) critere(s) de recherche');
WHEN OTHERS THEN
RAISE_APPLICATION_ERROR(-20001,'Exception levée par la procédure');
loop
fetch lignesFilm into titreFilm, anneeFilm;
-- fetch retoune la ligne suivante
EXIT WHEN lignesFilm%NOTFOUND;
-- quit lorsque cursor fini
DBMS_OUTPUT.PUT_LINE(titreFilm || ' (' || anneeFilm || ')');
end loop;
close lignesFilm;
end;
/
Please help me

It is possible if you use a bit of PL/SQL magic. Here's an example how you can search for multiple titles (just add other parameters that you need).
Firstly, you need a table type as a parameter to hold multiple titles - that's the t_str_tab type.
Secondly, you use the TABLE function on that table parameter to be able to query it, and you access its values by using the pseudocolumn COLUMN_VALUE.
Thirdly, you use a subquery to check if currently processed row has a title matching any of the titles provided in table parameter.
CREATE TABLE movies (id NUMBER, title VARCHAR2(20), release_year NUMBER);
CREATE TYPE t_str_tab IS TABLE OF VARCHAR2(20);
CREATE TYPE t_num_tab IS TABLE OF NUMBER;
INSERT INTO movies VALUES (1, 'First movie', 2010);
INSERT INTO movies VALUES (2, 'Second movie', 2010);
INSERT INTO movies VALUES (3, 'Third movie', 2010);
COMMIT;
CREATE OR REPLACE PROCEDURE search_proc(p_titles IN t_str_tab, p_years IN t_num_tab DEFAULT NULL) AS
CURSOR c_movies IS
SELECT m.id, m.title, m.release_year
FROM movies m
WHERE
((SELECT COUNT(1) FROM TABLE(p_titles)) = 0
OR EXISTS (
SELECT 1
FROM TABLE(p_titles) -- magic
WHERE upper(m.title) LIKE '%' || UPPER(column_value) || '%')
)
AND
((SELECT COUNT(1) FROM TABLE(p_years)) = 0
OR EXISTS (
SELECT 1
FROM TABLE(p_years)
WHERE m.release_year = column_value)
)
;
BEGIN
FOR v_movie IN c_movies
LOOP
dbms_output.put_line(
'Id: ' || v_movie.id ||
', title: ' || v_movie.title ||
', release year: ' || v_movie.release_year);
END LOOP;
END;
/
BEGIN
dbms_output.put_line('First search:');
search_proc(t_str_tab('d'));
dbms_output.put_line('Second search:');
search_proc(t_str_tab('st', 'nd'));
END;
Output:
First search:
Id: 2, title: Second movie, release year: 2010
Id: 3, title: Third movie, release year: 2010
Second search:
Id: 1, title: First movie, release year: 2010
Id: 2, title: Second movie, release year: 2010
Edit: changed the code to allow optional parameter.

Related

How to search for a month that is input by the user

I am working on some homework and have been stuck on this for a week. I have tried using TO_CHAR, MONTH(search), and EXTRACT(MONTH from...) and they all end up with either identifier 'JAN'(the month I am searching for) is not declared, or expression is of the wrong type. This assignment is to display all the rows for pledges made in a specified month. The column PLEDGEDATE is of type Date in the format 'dd-mmm-yy'. Any ideas how to make this work?
Declare
Pledges UNIT_2_ASSIGNMENT%ROWTYPE;
SEARCH DATE;
Begin
SEARCH := &M0NTH;
FOR PLEDGES IN
(SELECT IDPLEDGE, IDDONOR, PLEDGEAMT,
CASE
WHEN PAYMONTHS = 0 THEN 'LUMP SUM'
ELSE'MONHTLY - '||PAYMONTHS
END AS MONTHLY_PAYMENT
FROM UNIT_2_ASSIGNMENT
WHERE TO_CHAR(PLEDGEDATE,'MMM') = 'SEARCH'
ORDER BY PAYMONTHS)
LOOP
DBMS_OUTPUT.PUT_LINE('Pledge ID: '||UNIT_2_ASSIGNMENT.IDPLEDGE||
' Donor ID: '||UNIT_2_ASSIGNMENT.IDDONOR||
' Pledge Amount: '||TO_CHAR(UNIT_2_ASSIGNMENT.PLEDGEAMT)||
' Lump Sum: '||MONTHLY_PAYMENT);
END LOOP;
END;
You can use (comments on changes are inline):
DECLARE
Pledges UNIT_2_ASSIGNMENT%ROWTYPE;
SEARCH VARCHAR2(3); -- Use VARCHAR2 not DATE data type.
BEGIN
SEARCH := 'JAN'; -- Replace with your substitution variable.
FOR PLEDGES IN (
SELECT IDPLEDGE,
IDDONOR,
PLEDGEAMT,
CASE
WHEN PAYMONTHS = 0
THEN 'LUMP SUM'
ELSE 'MONHTLY - '||PAYMONTHS
END AS MONTHLY_PAYMENT
FROM UNIT_2_ASSIGNMENT
WHERE TO_CHAR(PLEDGEDATE,'MON') = SEARCH -- Unquote variable and use MON not MMM
ORDER BY PAYMONTHS
)
LOOP
DBMS_OUTPUT.PUT_LINE(
'Pledge ID: '||Pledges.IDPLEDGE|| -- Use rowtype variable name not table name.
' Donor ID: '||Pledges.IDDONOR||
' Pledge Amount: '||TO_CHAR(Pledges.PLEDGEAMT)||
' Lump Sum: '||Pledges.MONTHLY_PAYMENT
);
END LOOP;
END;
/
Which, for the sample data:
CREATE TABLE unit_2_assignment( idpledge, iddonor, pledgeamt, pledgedate, paymonths ) AS
SELECT LEVEL,
'Donor' || LEVEL,
LEVEL * 1000,
ADD_MONTHS( DATE '2020-01-01', LEVEL - 1 ),
LEVEL
FROM DUAL
CONNECT BY LEVEL <= 12;
Outputs:
Pledge ID: 1 Donor ID: Donor1 Pledge Amount: 1000 Lump Sum: MONHTLY - 1
You should enclose your substitution variable into single quotes ('&MONTH') because SQLPlus treats it as simple word and can substitute anything, according to examples in so old 8i reference. And it can be figured out by the error message: he tries to use JAN as identifier, so it is not properly enclosed.
Declare
Pledges UNIT_2_ASSIGNMENT%ROWTYPE;
SEARCH DATE;
Begin
SEARCH := '&M0NTH';
What it says:
For example, if the variable SORTCOL has the value JOB and the variable
MYTABLE has the value EMP, SQL*Plus executes the commands
SQL> BREAK ON &SORTCOL
SQL> SELECT &SORTCOL, SAL
2 FROM &MYTABLE
3 ORDER BY &SORTCOL;
But for your task there's no need to use PL/SQL, just format an output of SQLPlus script in appropriate way (have no SQLPlus console to put direct formatting options, but better to read the doc on SQLPlus by yourself).

PLS-00394: Wrong number of values in the INTO list of a fetch statement

This is my attempt in creating a cursor in a stored procedure
--Second Stored Procedure--
CREATE OR REPLACE PROCEDURE sp_GetDiscountedRate (DiscountCode IN
VARCHAR2,Percentage IN NUMBER, ReserveDate IN DATE)
IS --Code declaration section--
--variables to store column values returned from select into
pPassengerID VARCHAR2(10);
pFirst VARCHAR2(20);
pMiddle VARCHAR2(20);
pLast VARCHAR2(20);
pPassengerType CHAR (1);
uUSMilitaryID VARCHAR (8);
uMilitaryBranch VARCHAR2 (20);
uMilitaryDiscountCode VARCHAR2(8);
rFlightNumber VARCHAR2(6);
rReservationCost NUMBER(6);
rReservationDate DATE;
--Declare Cursor
CURSOR cur_USMilitary IS
--Query cursor will point to results
SELECT P.PassengerID, P.First, P.Middle, P.Last, P.PassengerType,
U.USMilitaryID, U.MilitaryBranch,
U.MilitaryDiscountCode, R.FlightNumber, R.ReservationCost,
R.ReservationDate,
CASE U.MilitaryDiscountCode WHEN DiscountCode THEN
Percentage*R.ReservationCost
ELSE R.ReservationCost END "REVISED_RESERVATION_COST"
FROM PASSENGER P, US_Military U, RESERVATION R
WHERE P.PassengerID = U.MPassengerID
AND P.PassengerID = R.PassengerID
AND U.MilitaryDiscountCode = DiscountCode
AND R.ReservationDate = ReserveDate;
--Start Execution section--
BEGIN
--Open Cursor
OPEN cur_USMilitary; -- open cursor for use
--loop to display each record returned by cursor
--Use PL/SQL language control or loop to display each record pointed by cursor
LOOP
--Fetch cursor data
FETCH cur_USMilitary INTO pPassengerID,
pFirst,pMiddle,pLast,pPassengerType,
uUSMilitaryID,uMilitaryBranch,uMilitaryDiscountCode,rFlightNumber,
rReservationCost,rReservationDate;
EXIT WHEN cur_USMilitary%NOTFOUND;
--Display each record
--Displaying the results
DBMS_OUTPUT.PUT_LINE ('CUSTOMER INFORMATION: ');
DBMS_OUTPUT.PUT_LINE ('The PassengerID is: ' ||pPassengerID);
DBMS_OUTPUT.PUT_LINE ('First Name of passenger is: ' ||pFirst);
DBMS_OUTPUT.PUT_LINE ('Middle Name of passenger is: ' ||pMiddle);
DBMS_OUTPUT.PUT_LINE ('Last Name of passenger is: ' ||pLast);
DBMS_OUTPUT.PUT_LINE ('Passenger Type of customer is: ' ||pPassengerType);
DBMS_OUTPUT.PUT_LINE ('US Military ID of Passenger is: ' ||uUSMilitaryID);
DBMS_OUTPUT.PUT_LINE ('Military Branch of passenger is: ' ||uMilitaryBranch);
DBMS_OUTPUT.PUT_LINE ('Military Discount code of passenger is: ' ||uMilitaryDiscountCode);
DBMS_OUTPUT.PUT_LINE ('Flight number of passenger is: ' ||rFlightNumber);
DBMS_OUTPUT.PUT_LINE ('Reservation Cost of passenger is: ' ||rReservationCost);
DBMS_OUTPUT.PUT_LINE ('Reservation Date of passenger is: ' ||rReservationDate);
END LOOP;
CLOSE cur_USMilitary; --close cursor
END sp_GetDiscountedRate;
I get this error:
Error(36,9): PL/SQL: SQL Statement ignored
Error(36,9): PLS-00394: wrong number of values in the INTO list of a FETCH statement
I fully understand the error. I checked the number of columns and it looks like to me that they match the number of columns within the query. I've also checked the datatypes to make sure it was correct.
You have
12 columns in your cursor's SELECT part
but
11 columns in FETCH statement part
, i observe
CASE U.MilitaryDiscountCode WHEN DiscountCode THEN
Percentage*R.ReservationCost
ELSE R.ReservationCost END "REVISED_RESERVATION_COST"
part is missing in FETCH( or should be omitted in the SELECT part ).
It also represents a column, and it spoils 1-1 correspondence, which yields that error.
This is what happens when you overcomplicate things, I'm afraid. I don't see any need for all those variables and the corresponding fetch into that is hard to keep in sync. Why not just:
create or replace procedure sp_getdiscountedrate
( discountcode in varchar2
, percentage in number
, reservedate in date )
is
begin
for r in (
select p.passengerid
, p.first
, p.middle
, p.last
, p.passengertype
, u.usmilitaryid
, u.militarybranch
, u.militarydiscountcode
, r.flightnumber
, r.reservationcost
, r.reservationdate
, case u.militarydiscountcode
when discountcode then percentage * r.reservationcost
else r.reservationcost
end as revised_reservation_cost
from passenger p
join us_military u
on u.mpassengerid = p.passengerid
join reservation r
on r.passengerid = p.passengerid
and r.discountcode = u.militarydiscountcode
and r.reservedate = r.reservationdate
)
loop
dbms_output.put_line('CUSTOMER INFORMATION:');
dbms_output.put_line('The PassengerID is: ' || r.passengerid);
dbms_output.put_line('First Name of passenger is: ' || r.first);
dbms_output.put_line('Middle Name of passenger is: ' || r.middle);
dbms_output.put_line('Last Name of passenger is: ' || r.last);
dbms_output.put_line('Passenger Type of customer is: ' || r.passengertype);
dbms_output.put_line('US Military ID of Passenger is: ' || r.usmilitaryid);
dbms_output.put_line('Military Branch of passenger is: ' || r.militarybranch);
dbms_output.put_line('Military Discount code of passenger is: ' || r.militarydiscountcode);
dbms_output.put_line('Flight number of passenger is: ' || r.flightnumber);
dbms_output.put_line('Reservation Cost of passenger is: ' || r.reservationcost);
dbms_output.put_line('Reservation Date of passenger is: ' || r.reservationdate);
dbms_output.put_line('Revised reservation cost is: ' || r.revised_reservation_cost);
end loop;
end sp_getdiscountedrate;
(Untested)
You go through all the trouble of calculating "REVISED_RESERVATION_COST", and then you don't read it from the cursor.
No wonder Oracle is complaining. You need a variable for this column as well.
I would also advise you to learn to use modern, proper, explicit JOIN syntax.

For loop using VARCHAR2

I'm trying to display the info for each student using this code
DECLARE
CURSOR cursor1 IS SELECT STUDENTNAME, COURSEID, COURSEDESCRIPTION, COURSECREDITS, GRADE
FROM STUDENTINFO;
S_NAME STUDENTINFO.STUDENTNAME%TYPE;
S_COURSEID STUDENTINFO.COURSEID%TYPE;
S_COURSEDESCRIPTION STUDENTINFO.COURSEDESCRIPTION%TYPE;
S_COURSECREDITS STUDENTINFO.COURSECREDITS%TYPE;
S_GRADE STUDENTINFO.GRADE%TYPE;
BEGIN
OPEN CURSOR1;
LOOP
FETCH CURSOR1 INTO S_NAME, S_COURSEID, S_COURSEDESCRIPTION, S_COURSECREDITS, S_GRADE;
EXIT WHEN cursor1%NOTFOUND;
DBMS_OUTPUT.PUT_LINE('Student Name: ' || S_NAME);
DBMS_OUTPUT.PUT_LINE(S_COURSEID || S_COURSEDESCRIPTION || S_COURSECREDITS || S_GRADE);
DBMS_OUTPUT.PUT_LINE(CHR(10));
END LOOP;
CLOSE CURSOR1;
END;
/
My output should be similar to
Student Name: John
CMIS 101 Intro to Info. Systems 3 B
CMIS 301 System Analysis 3 C
CMIS 451 Client/Server Systems 3 C
I'm pretty sure I should be using a for loop which I created
DECLARE
CURSOR cursor2 IS SELECT STUDENTNAME, COURSEID, COURSEDESCRIPTION, COURSECREDITS, GRADE
FROM STUDENTINFO;
search_student STUDENTINFO.STUDENTNAME%TYPE;
BEGIN
FOR v_Record IN cursor2
LOOP
IF v_Record.STUDENTNAME = &SEARCH_STUDENT THEN
DBMS_OUTPUT.PUT_LINE('Student Name: ' || STUDENTNAME);
DBMS_OUTPUT.PUT_LINE(COURSEID || COURSEDESCRIPTION || COURSECREDITS || GRADE);
DBMS_OUTPUT.PUT_LINE(CHR(10));
END IF;
END LOOP;
END;
/
However, when I type in a name for search_student, I'm just given the error
Identifier "insertnamehere"
Can I not use VARCHAR2 when searching? Is it only usable with numbers?
There are a few issues with your code.
For a start, why use PL/SQL to do this? You should just use a SQL statement directly. I'm going to assume that this is a homework question though (*sigh* - surely there are better examples to use?!).
1. IF v_Record.STUDENTNAME = &SEARCH_STUDENT - when you pass in a value for search_student, the client replaces the term &search_student. So, either you must ensure that the single quotes to specify the studentname is a string when you define the search_student (ie. when prompted for search_student you enter "'SomeName'") OR put the single quotes around the &SEARCH_STUDENT - ie. IF v_Record.STUDENTNAME = '&SEARCH_STUDENT'
2. When referencing the fields returned by the cursor in the for loop, you need to reference the record that the values were fetched into. Therefore DBMS_OUTPUT.PUT_LINE('Student Name: ' || STUDENTNAME); should be DBMS_OUTPUT.PUT_LINE('Student Name: ' || v_Record.STUDENTNAME);
3. Finally, if you're looking to output a single student record at a time, put the filter in the cursor, not in the loop.

SQL Function and Anonymous block

I'm taking a class about databases. I am very new to it, so excuse me if this is an obvious error, but I've been working on this problem for hours and not sure what else to do.
The code is suppose to create a function to that
A.) Outputs the average netwin for a year given by a parameter
(given by the formula (SeasonW-Seasonl) + (PlayoffW - PlayoffL)
B.) Outputs all of the coaches that have a netwin over the average
C.) Returns the number of coaches that fits this criteria.
D.)Have a anonymous block that calls this function, and outputs two different messages depending on the return on the function.
Now I have sucessfully done parts A, C, and D. But for some reason my function will not be created when I insert part B.
create or replace function GOOD_COACHES(season IN INT)
return INT
IS
netwin INT;
CNT INT;
BEGIN
--Calculated netwin
select AVG((SEASON_WIN-SEASON_LOSS) + (PLAYOFF_WIN-PLAYOFF_LOSS)) into netwin from COACHESSEASON where YEAR = season;
--Prints out A
dbms_output.put_line('Average Netwin is: ' || netwin);
--This Line messes up the function, I don't know why
select T.FIRSTNAME, T.LASTNAME from COACHESSEASON T where ((T.SEASON_WIN-T.SEASON_LOSS) + (T.PLAYOFF_WIN-T.PLAYOFF_LOSS))>netwin and YEAR = season;
--Calculates the number of teams that satisfy average
select count(T.FIRSTNAME) into CNT from COACHESSEASON T where ((T.SEASON_WIN-T.SEASON_LOSS) + (T.PLAYOFF_WIN-T.PLAYOFF_LOSS))>netwin and YEAR = season;
return CNT;
END;
--End of the Function
--Start of the Anonymous Block
DECLARE
x int := GOOD_COACHES(1998);
BEGIN
if x = 0 then dbms_output.put_line('We didn''t find any good_coaches!');
else dbms_output.put_line('The No. of good coaches is ' || x);
end if;
END;
/
That third line in the function messes it up and doesn't allow it to be called. If I comment it out, it works properly.
When I take it out of the function and make it into a regular SQL statement, it works.
select T.FIRSTNAME, T.LASTNAME from COACHESSEASON T where ((T.SEASON_WIN-T.SEASON_LOSS) + (T.PLAYOFF_WIN-T.PLAYOFF_LOSS))>0 and YEAR = /*RandYear*/;
If anyone understands why the function can not be created with that line it in, I would appreciate the advice. I also do not know how I would print out the results of the selected row in the function.
You are not selecting the T.FIRSTNAMEs and T.LASTNAMEs into anything so the statement is not valid for inclusion in a PL/SQL block.
There are likely to be multiple "good coaches" so you can't put the values into a single variable and will have to use a cursor or a collection.
Using collections, if you create a table type to collect the first and last names into:
CREATE TYPE VARCHAR2s_Table AS TABLE OF VARCHAR2(30);
/
Then you can use this to collect the first and last names (and as a bonus the collection size will tell you how many good coaches there are and you can skip the last query):
create or replace function GOOD_COACHES(season IN INT)
return INT
IS
netwin INT;
CNT INT;
firstnames VARCHAR2s_Table;
lastnames VARCHAR2s_Table;
BEGIN
--Calculated netwin
select AVG((SEASON_WIN-SEASON_LOSS) + (PLAYOFF_WIN-PLAYOFF_LOSS))
into netwin
from COACHESSEASON
where YEAR = season;
--Prints out A
dbms_output.put_line('Average Netwin is: ' || netwin);
--This Line messes up the function, I don't know why
select T.FIRSTNAME, T.LASTNAME
BULK COLLECT INTO firstnames, lastnames
from COACHESSEASON T
where ((T.SEASON_WIN-T.SEASON_LOSS) + (T.PLAYOFF_WIN-T.PLAYOFF_LOSS))>netwin
and YEAR = season;
FOR i IN 1 .. firstnames.COUNT LOOP
DBMS_OUTPUT.put_line( firstnames(i) || ' ' || lastnames(i) );
END LOOP;
return firstnames.COUNT;
END;
/
SQLFIDDLE

Why am I getting PLS - 00382?

Here is my object def:
CREATE OR REPLACE TYPE FALCON.contacts AS OBJECT (phone VARCHAR2(50)
,phoneusage VARCHAR2(25)
,phonetype VARCHAR2(25)
,email VARCHAR2(150)
,phoneext VARCHAR2(25)
,anytext VARCHAR2(250))
Here is the table def:
CREATE OR REPLACE TYPE FALCON.contacttbl AS TABLE OF contacts
Here is my pipelined function
FUNCTION get_pcontacts(p_conttbl IN xmltypedefs_spec.conttbl)
RETURN falcon.contacttbl
PIPELINED
IS
l_contact falcon.contacts;
BEGIN
FOR n IN 1 .. p_conttbl.count
LOOP
PIPE ROW(**falcon.contacts**(p_conttbl(n).phone, p_conttbl(n).phoneusage, p_conttbl(n).phonetype, p_conttbl(n).email, p_conttbl(n).phoneext, p_conttbl(n).anytext));
END LOOP;
RETURN;
END get_pcontacts;
I am getting the error when I call the table function here:
FUNCTION get_pidxml(p_pidrec xmltypedefs_spec.pidtyp)
RETURN CLOB
IS
l_tmprec CLOB;
l_pxml xmltype;
l_bxml xmltype;
l_pcontacts xmltypedefs_spec.conttbl := p_pidrec.personalcont;
l_bcontacts xmltypedefs_spec.conttbl := p_pidrec.businesscont;
BEGIN
-- l_pxml := get_contacts(p_pidrec, 'p');
-- l_bxml := get_contacts(p_pidrec, 'b');
SELECT xmlelement("pid"
,xmlforest(p_pidrec.setid AS "setID"
,p_pidrec.patidexternal AS "patientIDExternal"
,p_pidrec.patientid AS "patientID"
,p_pidrec.patintasgnauth AS "patientIDInterAssignAuthority"
,p_pidrec.patinttypecd AS "patientIDInternalIDTypeCode"
,p_pidrec.patidalternate1 AS "patientIDAlernate1"
,p_pidrec.patlastname AS "patientLastName"
,p_pidrec.patfirstname AS "patientFirstName"
,p_pidrec.patmiddleinit AS "patientMiddleInitial"
,p_pidrec.patsuffix AS "patientSuffix"
,p_pidrec.patprefix AS "patientPrefix"
,p_pidrec.degree AS "degree"
,p_pidrec.familyname AS "familyName"
,p_pidrec.givenname AS "givenName"
,p_pidrec.mothermaidname AS "mothersMaidenName"
,p_pidrec.dob AS "dateOfBirth"
,p_pidrec.adminsex AS "administrativeSex"
,p_pidrec.patientalias AS "patientAlias"
,p_pidrec.race AS "race"
,p_pidrec.racetext AS "raceText"
,p_pidrec.pataddr1 AS "patientAddress1"
,p_pidrec.pataddr2 AS "patientAddress2"
,p_pidrec.patcity AS "patientCity"
,p_pidrec.patstate AS "patientState"
,p_pidrec.patzip AS "patientZip"
,p_pidrec.countrycode AS "countryCode"
,p_pidrec.addresstype AS "addressType"
,p_pidrec.othgeodesig AS "otherGeographicDesignation"
,p_pidrec.county AS "county"
,(SELECT xmlagg(xmlelement("contactInfo",
xmlforest(phone AS "phoneNumber",
phoneusage AS "telecomUseCode",
phonetype AS "telecomequiptype",
email AS "email",
phoneext AS "phonenumberextension",
anytext AS "anytext")))
FROM TABLE(**get_pcontacts(l_pcontacts**))) AS "personalContact"
http://pls-00382.ora-code.com/
PLS-00382: expression is of wrong type
Since I don't know how xmltypedefs_spec.conttbl is defined, I removed the input parameter from the pipelined function and just had it generate fake data on the fly:
CREATE OR REPLACE FUNCTION get_contacts
RETURN contacttbl PIPELINED
IS
-- converts some structure to pipe of contacts
BEGIN
FOR n IN 1 .. 5 LOOP
PIPE ROW(
contact(
'877-867-5309',
'Work',
'Cell',
'jenny#gmail.com',
n,
'WTF?'
)
);
END LOOP;
RETURN;
END get_contacts;
The subquery now executes without error:
SELECT
xmlagg(
xmlelement("contactInfo",
xmlforest(
phone AS "phoneNumber",
phoneusage AS "telecomUseCode",
phonetype AS "telecomequiptype",
email AS "email",
phoneext AS "phonenumberextension",
anytext AS "anytext"
)
)
)
FROM
TABLE( get_contacts( ) )
This tells me there is probably something wrong with xmltypedefs_spec.conttbl, perhaps in using a collection type within an SQL statement? Not sure. What if you changed xmltypedefs_spec.pidtyp to use the falcon.contacttbl instead of xmltypedefs_spec.conttbl. Seems like you've got one package type and one object type that are doing the same thing?
xmltypedefs_spec defines record types that correspond to XML elements. These record types are used to shred and build XML. Originally, the XML did not use repeating elements, but now must. I am attempting to take a table of xmltypedefs_spec.pidtyp and use the pipelined function to return 'rows' of data from an associative table. It is in this fashion that I want to send rows of array records to build xml.