Sr7
Sr7

Reputation: 341

pl sql function select into variable in loop on a returning table

I have a type object as below Emptable type is empname,empid,rank

Then I have a Plsql function as below and this errors out. I need to run a sql select statement against the returned list of empids and load it the returned list. and below code keeps erroring..

create or replace function emp_details return emptable
  is
     l_result_col  emptable := emptable();
     n integer := 0;
     rxvalue number;
  begin
     for r in (select empname,empid from table)
     loop
        l_result_col.extend;
        n := n + 1;
        (select sum(xyz) into rxvalue from A inner join B on A.x=B.x and A.id=r.empid);
       l_result_col(n) := t_col(r.empname, r.empid,rxvalue);
     end loop;
     return l_result_col;
  end;
  /

Any Help is appreciated.. Thank you!

Upvotes: 0

Views: 3445

Answers (2)

Egor Skriptunoff
Egor Skriptunoff

Reputation: 23737

Why not do it straightforward without involving PL/SQL code?

select t_col(r.empname, r.empid, sum(xyz))
bulk collect into l_result_col
from table r
left join (A join B on A.x = B.x) on A.id = r.empid
group by r.empname, r.empid;

Upvotes: 1

GolezTrol
GolezTrol

Reputation: 116110

I think your select shouldn't be in parentheses. In this context it is not a subselect but a separate PL/SQL statement.

Upvotes: 0

Related Questions