All along, I think it's very easy to use the \ G parameter in MySQL to change the display mode of the output result set, especially in the command-line interface. But ORACLE database doesn't have this function. Today, when I searched a blog post of master Tom, I found that master used a stored procedure, print table, to implement such functions. It's just that we ordinary people don't know about it. We specially organize it here for the convenience of checking by ourselves or those in need.
CREATE OR REPLACE PROCEDURE print_table(p_query IN VARCHAR2)AUTHID CURRENT_USER
IS
l_thecursor INTEGER DEFAULT dbms_sql.open_cursor;l_columnvalue VARCHAR2(4000);l_status INTEGER;
l_desctbl dbms_sql.desc_tab;l_colcnt NUMBER;BEGIN
EXECUTE IMMEDIATE 'alter session set nls_date_format=''dd-mon-yyyy hh24:mi:ss'' ';dbms_sql.parse(l_thecursor, p_query, dbms_sql.native);dbms_sql.describe_columns (l_thecursor, l_colcnt, l_desctbl);FOR i IN 1 .. l_colcnt LOOPdbms_sql.define_column (l_thecursor, i, l_columnvalue, 4000);END LOOP;
l_status := dbms_sql.EXECUTE(l_thecursor);
WHILE ( dbms_sql.Fetch_rows(l_thecursor) > 0 ) LOOP
FOR i IN 1 .. l_colcnt LOOPdbms_sql.column_value (l_thecursor, i, l_columnvalue);dbms_output.Put_line (RPAD(L_desctbl(i).col_name, 30)|| ': '
|| l_columnvalue);END LOOP;
dbms_output.put_line('-----------------');
END LOOP;
EXECUTE IMMEDIATE 'alter session set nls_date_format=''dd-MON-rr'' ';EXCEPTION
WHEN OTHERS THENEXECUTE IMMEDIATE'alter session set nls_date_format=''dd-MON-rr'' ';RAISE;END;
/
The tests are as follows:
SQL> set serveroutput on size 99999;SQL> execute print_table('select * from v$session where sid=997');SADDR : 000000085FA35CA0SID : 997SERIAL# : 1AUDSID : 0PADDR : 000000085F6B7E70USER# : 0
USERNAME :COMMAND : 0OWNERID : 2147483644TADDR :LOCKWAIT :STATUS : ACTIVESERVER : DEDICATEDSCHEMA# : 0
SCHEMANAME : SYSOSUSER : oraclePROCESS : 5036MACHINE : xxxxPORT : 0TERMINAL : UNKNOWN
PROGRAM : oracle@xxxxx (DBW0)TYPE : BACKGROUNDSQL_ADDRESS : 00SQL_HASH_VALUE : 0SQL_ID :SQL_CHILD_NUMBER : 0PREV_SQL_ADDR : 00PREV_HASH_VALUE : 0PREV_SQL_ID :PREV_CHILD_NUMBER : 0PLSQL_ENTRY_OBJECT_ID :PLSQL_ENTRY_SUBPROGRAM_ID :PLSQL_OBJECT_ID :PLSQL_SUBPROGRAM_ID :MODULE :
MODULE_HASH : 0ACTION :
ACTION_HASH : 0CLIENT_INFO :FIXED_TABLE_SEQUENCE : 0ROW_WAIT_OBJ# : -1ROW_WAIT_FILE# : 0ROW_WAIT_BLOCK# : 0ROW_WAIT_ROW# : 0LOGON_TIME : 04-jul-2018 21:15:52LAST_CALL_ET : 5272838PDML_ENABLED : NO
FAILOVER_TYPE : NONE
FAILOVER_METHOD : NONE
FAILED_OVER : NO
RESOURCE_CONSUMER_GROUP :PDML_STATUS : DISABLEDPDDL_STATUS : DISABLEDPQ_STATUS : DISABLEDCURRENT_QUEUE_DURATION : 0CLIENT_IDENTIFIER :BLOCKING_SESSION_STATUS : NO HOLDER
BLOCKING_INSTANCE :BLOCKING_SESSION :SEQ# : 34697EVENT# : 3EVENT : rdbms ipc messageP1TEXT : timeoutP1 : 300P1RAW : 000000000000012CP2TEXT :P2 : 0P2RAW : 00P3TEXT :P3 : 0P3RAW : 00WAIT_CLASS_ID : 2723168908WAIT_CLASS# : 6WAIT_CLASS : IdleWAIT_TIME : 0SECONDS_IN_WAIT : 107STATE : WAITING
SERVICE_NAME : SYS$BACKGROUNDSQL_TRACE : DISABLEDSQL_TRACE_WAITS : FALSE
SQL_TRACE_BINDS : FALSE
ECID :-----------------
PL/SQL procedure successfully completed.SQL>
reference material:
https://asktom.oracle.com/pls/asktom/f?p=100:11:::::P11_QUESTION_ID:1035431863958,%7Bprint_table%7D