動態行轉列

yuanzai32發表於2014-01-16
declare 
v_sql varchar2(4000);
begin
for i in (select level a from dual connect by level<=4) loop
  v_sql := v_sql||'sum(decode('||i.a||','||i.a||',''a'')), ';
  dbms_output.put_line(v_sql);
  end loop;
  end;

列印出來的結果
sum(decode(1,1,'a')), 
sum(decode(1,1,'a')), sum(decode(2,2,'a')), 
sum(decode(1,1,'a')), sum(decode(2,2,'a')), sum(decode(3,3,'a')), 
sum(decode(1,1,'a')), sum(decode(2,2,'a')), sum(decode(3,3,'a')), sum(decode(4,4,'a')), 
怎麼實現只顯示最下面一個

--發現列印寫在迴圈裡了
把列印拿出來,加上rtrim去掉最後一個逗號
declare 
v_sql varchar2(4000);
begin
for i in (select level a from dual connect by level<=4) loop
  v_sql := v_sql||'sum(decode('||i.a||','||i.a||',''a'')),';
    end loop;
  v_sql := rtrim(v_sql,',');
  dbms_output.put_line(v_sql);
  end;

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/26477854/viewspace-1071670/,如需轉載,請註明出處,否則將追究法律責任。

相關文章