I have this table in the below format:
Persnbr | Userfieldcd | Value
01 | Port | Funds
01 | Vip1 | Systems
02 | Port | Bank
02 | Vip1 | Authority
This is how I want it:
Persnbr | Port | Vip1
01 | Funds | Systems
02 | Bank | Authority
As I dont know the all the fields in the userfieldcd column, I am trying to dynamically pivot the table. So I am using this procedure but I dont know how to call it in PL/SQL developer. I am using Oracle Database 11g Enterprise Edition Release 11.2.0.2.0 - 64bit Production
CREATE OR REPLACE procedure dynamic_pivot_po(p_cursor in out sys_refcursor)
as
sql_query varchar2(1000) := 'select persnbr ';
begin
for x in (select distinct userfieldcd from persuserfield order by 1)
loop
sql_query := sql_query ||
' , min(case when userfieldcd = '''||x.userfieldcd||''' then value else null end) as '||x.userfieldcd;
dbms_output.put_line(sql_query);
end loop;
sql_query := sql_query || ' from persuserfield group by persnbr order by persnbr';
dbms_output.put_line(sql_query);
open p_cursor for sql_query;
end;
/
When I call the procedure using:
VARIABLE x REFCURSOR
BEGIN
dynamic_pivot_po(:x)
END
/
it gives me
ORA-00900: Invalid SQL statement.