Oracle Function
450446Nov 14 2005 — edited Nov 14 2005CREATE OR REPLACE FUNCTION FUND_TEAM(P_TEAMID NUMBER,P_CCID NUMBER, P_CCTID NUMBER) RETURN VARCHAR2 AS
CURSOR C_ORGNAME(P_ID NUMBER) IS
SELECT distinct o.NAME ORGNAME
FROM ORGANISATION o,
COST_TEAMS cct,
cost_centre cc
WHERE
o.IDENTIFIER = P_ID
AND
cct.ccentr_identifier = P_CCID
and
cct.identifier = P_CCTID
and
cct.ounit_identifier = o.identifier(+)
and
cc.identifier(+)=cct.ccentr_identifier;
TEAMNAME VARCHAR2 (30);
BEGIN
FOR X IN C_ORGNAME(P_TEAMID)
LOOP
TEAMNAME:= X.ORGNAME;
END LOOP;
RETURN(TEAMNAME);
END;
/
When I use the select statement alone, it gives me one team name as a result like
SELECT distinct o.NAME ORGNAME
FROM ORGANISATION o,
COST_TEAMS cct,
cost_centre cc
WHERE
o.IDENTIFIER = 38454
AND
cct.ccentr_identifier = 2334
and
cct.identifier = 1994
and
cct.ounit_identifier = o.identifier(+)
and
cc.identifier(+)=cct.ccentr_identifier;
But when I try to invoke the function in the select statement like
select fund_team(38454,2334,1994) from cost_teams
It gives me 36 values with the same team name, which is the total count in the cost_teams.
Could you kindly let me what has to be done to fetch the correct single value for the team name ?
Thanks a lot.