Converting Rows to Column Using SQL
declare
TYPE c_refcur IS REF CURSOR;
l_str VARCHAR2(4000);
l_col VARCHAR2(4000);
c_ref c_refcur;
l_num number;
l_query varchar2(4000) :='select column_name from all_tab_columns where table_name like ''MTL_SYSTEM_ITEMS_B'' and rownum < 6 order by 1 desc' ;
l_dlmtr varchar2(10) := ',';
BEGIN
OPEN c_ref FOR l_query;
LOOP
FETCH c_ref INTO l_col;
l_str := l_str || l_dlmtr || l_col;
EXIT WHEN c_ref%NOTFOUND;
END LOOP;
CLOSE c_ref;
dbms_output.put_line( 'select '||SUBSTR(l_str,2)|| ' from MTL_SYSTEM_ITEMS_B');
EXCEPTION
WHEN OTHERS THEN
l_str := SQLERRM;
IF c_ref%ISOPEN THEN
CLOSE c_ref;
END IF;
END;
/
OUTPUT: select SECONDARY_UOM_CODE,EAM_ACT_SHUTDOWN_STATUS,EAM_ACT_NOTIFICATION_FLAG,DUAL_UOM_DEVIATION_HIGH,DUAL_UOM_CONTROL,DUAL_UOM_CONTROL from MTL_SYSTEM_ITEMS_B
TYPE c_refcur IS REF CURSOR;
l_str VARCHAR2(4000);
l_col VARCHAR2(4000);
c_ref c_refcur;
l_num number;
l_query varchar2(4000) :='select column_name from all_tab_columns where table_name like ''MTL_SYSTEM_ITEMS_B'' and rownum < 6 order by 1 desc' ;
l_dlmtr varchar2(10) := ',';
BEGIN
OPEN c_ref FOR l_query;
LOOP
FETCH c_ref INTO l_col;
l_str := l_str || l_dlmtr || l_col;
EXIT WHEN c_ref%NOTFOUND;
END LOOP;
CLOSE c_ref;
dbms_output.put_line( 'select '||SUBSTR(l_str,2)|| ' from MTL_SYSTEM_ITEMS_B');
EXCEPTION
WHEN OTHERS THEN
l_str := SQLERRM;
IF c_ref%ISOPEN THEN
CLOSE c_ref;
END IF;
END;
/
OUTPUT: select SECONDARY_UOM_CODE,EAM_ACT_SHUTDOWN_STATUS,EAM_ACT_NOTIFICATION_FLAG,DUAL_UOM_DEVIATION_HIGH,DUAL_UOM_CONTROL,DUAL_UOM_CONTROL from MTL_SYSTEM_ITEMS_B
No comments:
Post a Comment