在sqlplus,如果列比较多,往往会显示不够清晰,这时如果能把查询语句行转列就明了多了,在网上看到print_table存储过程:里面所设置的日期格式,可根据自己习惯修改
1 CREATE OR REPLACE PROCEDURE PRINT_TABLE(P_QUERY IN VARCHAR2) AUTHID CURRENT_USER IS 2 L_THECURSOR INTEGER DEFAULT DBMS_SQL.OPEN_CURSOR; 3 L_COLUMNVALUE VARCHAR2(4000); 4 L_STATUS INTEGER; 5 L_DESCTBL DBMS_SQL.DESC_TAB; 6 L_COLCNT NUMBER; 7 BEGIN 8 EXECUTE IMMEDIATE 'alter session set nls_date_format=''dd-mon-yyyy hh24:mi:ss'' '; 9 DBMS_SQL.PARSE(L_THECURSOR, P_QUERY, DBMS_SQL.NATIVE); 10 DBMS_SQL.DESCRIBE_COLUMNS(L_THECURSOR, L_COLCNT, L_DESCTBL); 11 FOR I IN 1 .. L_COLCNT LOOP 12 DBMS_SQL.DEFINE_COLUMN(L_THECURSOR, I, L_COLUMNVALUE, 4000); 13 END LOOP; 14 L_STATUS := DBMS_SQL.EXECUTE(L_THECURSOR); 15 16 WHILE (DBMS_SQL.FETCH_ROWS(L_THECURSOR) > 0) LOOP 17 FOR I IN 1 .. L_COLCNT LOOP 18 DBMS_SQL.COLUMN_VALUE(L_THECURSOR, I, L_COLUMNVALUE); 19 DBMS_OUTPUT.PUT_LINE(RPAD(L_DESCTBL(I).COL_NAME, 30) || ': ' ||L_COLUMNVALUE); 21 END LOOP; 22 DBMS_OUTPUT.PUT_LINE('-------------------------------------'); 23 END LOOP; 24 EXECUTE IMMEDIATE 'alter session set nls_date_format=''dd-MON-rr'' '; 25 EXCEPTION 26 WHEN OTHERS THEN 27 EXECUTE IMMEDIATE 'alter session set nls_date_format=''dd-MON-rr'' '; 28 RAISE; 29 END;
存储过程编译后,在sqlplus里 可以通过 show error查看是否有编译错误,或查询USER_ERRORS视图
注意这里声明Authid Current_User后则就是调用者权限,以调用者身份执行;
在sqlplus 显示的效果:
13:11:41 SYS@XXX> exec print_table('SELECT * FROM user_objects where rownum=1'); ------------------------------------- OBJECT_NAME : /1000e8d1_LinkedHashMapValueIt SUBOBJECT_NAME : OBJECT_ID : 31050 DATA_OBJECT_ID : OBJECT_TYPE : JAVA CLASS CREATED : 07-7月 -2010 12:54:47 LAST_DDL_TIME : 07-7月 -2010 12:54:47 TIMESTAMP : 2010-07-07:12:54:47 STATUS : VALID TEMPORARY : N GENERATED : N SECONDARY : N ------------------------------------- PL/SQL 过程已成功完成。
在pl/sql可如下调用:
BEGIN DBMS_OUTPUT.ENABLE(BUFFER_SIZE => NULL); --这里设置output时,大小不受限制 EA_IPMP.PRINT_TABLE('SELECT * FROM user_objects where rownum=1'); END;
如有错误,欢迎交流学习.