Dynamic row by row transfer sql

The last chapter we talked about fixed-line columns turn this chapter we will look at how to achieve dynamic line transfer columns. Because sometimes there are values need to train thousands of rows, then fixed-line method can not turn the column, otherwise you will crash out. Well, ado, let's go!
Common a table tmp_test, reads as follows:
Dynamic row by row transfer sql

Implementation code:
Create or Replace Procedure P_test IS
v_sql VARCHAR2 (2000);

cursor cursor_1 is
select distinct subject from tmp_test order by subject;

begin
v_sql := 'select username';
for v_subject in cursor_1 loop
v_sql := v_sql || ',' ||'sum(decode(subject,''' ||v_subject.subject ||''',source)) as ' ||v_subject.subject;
dbms_output.put_line(v_sql);
end loop;

v_sql := vql || ' from tmp_test group by username';
dbms_output.put_line(v_sql);

v_sql := 'create or replace view test_result as ' || v_sql;

execute immediate v_sql;

end;
the stored procedure has been written, start calling save too:
the begin
P_test;
end;

Query view test_result:
code for:
SELECT * from test_result;
data was as follows:
Dynamic row by row transfer sql

At this point, turn the dynamic line column has been achieved! We hope to a small partner to help!

Guess you like

Origin blog.51cto.com/12777507/2404745
Row