时间:2021-07-01 10:21:17 帮助过:4人阅读
实现代码:
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;
存储过程已经写好了,开始调用存过:
begin
p_test;
end;
查询视图test_result:
代码实现:
select * from test_result;
数据如下:
到这一步,动态行转列就已经实现了!希望对小伙伴有帮助!
通过sql实现动态行转列
标签:固定 char tin create 图片 oop result bms procedure