日期:2014-05-16 浏览次数:20971 次
DECLARE @sql VARCHAR(8000) SET @sql = 'select A3' SELECT @sql = @sql + ' ,max(case A1 when ''' + A1 + ''' then A2 else 0 end) [' + A1 + ']' FROM ( SELECT DISTINCT A1 FROM Table_1 ) AS a SET @sql = @sql + ' from Table_1 group by A3' EXEC(@sql)
declare var_sql_str varchar2(2000); type test_array is table of Table_1.A1%type INDEX BY BINARY_INTEGER; var_test_array test_array; begin select distinct A1 bulk collect into var_test_array from Table_1; var_sql_str:='select A3' for i in 1..var_test_array.count loop begin var_sql_str:=var_sql_str||', max(case A1 when '''||var_test_array(i)||''' then A2 else 0 end) as '||var_test_array(i)||'' end loop; var_sql_str:=var_sql_str||' from Table_1 group by A3' execute immediate var_sql_str; end ;