开发者

Equivalent of MySQL's \G in Oracle's SQL*Plus

开发者 https://www.devze.com 2023-01-25 13:36 出处:网络
In Oracle\'s SQL*Plus, the results of a SELECT are displayed in a tabular manner. Is there a way to display a row in a key-value manner (like MySQL\'s \\G option)?

In Oracle's SQL*Plus, the results of a SELECT are displayed in a tabular manner. Is there a way to display a row in a key-value manner (like MySQL's \G option)?

The database I am wo开发者_如何学Pythonrking on (the schema is not defined by me) has a bunch of columns named e.g. YN_ENABLED (YN = yes no) which are CHAR(1). So when I do a query I get a result like

ID_MYTABLE   Y Y Y
------------ - - -
3445         Y N Y

So it's not really clear which columns have which values (without having the schema open in another window).


Not built in to SQL PLus, but Tom Kyte has provided a procedure called print_table that does this. You would run it like this:

SQL> exec print_table ('select * from mytable where id_mytable=123');

And see results like:

ID_MYTABLE      : 123
YN_ENABLED      : Y
YN_SOMETHING    : N
...


I know your question is about SQL*PLus, but you might be interested to know that Oracle's SQL Developer can do this. The feature can be used by right clicking on the Query Results and selecting "Single Record View...".


Here's a trick that may do in a pinch if you don't want to (or cannot) install a new procedure on your server:

  1. Select the row (or rows) of interest in the Oracle SQL Developer query results window.
  2. Use shift-control-c in Oracle SQL Developer to copy the rows and headers to the clipboard.
  3. Paste into your favorite spreadsheet (e.g., MS Excel). Now you have records in rows.
  4. Copy the rows that you just pasted into the spreadsheet
  5. Use the "Paste Special - Transpose" feature of your spreadsheet program to paste the values into a new spreadsheet. Now your records should be in columns.


Coming in late, but I found this

SQL> select * from xmltable('ROWSET/ROW/*' passing xmltype(cursor(select * from emp where rownum = 1
)) columns name varchar2(30) path 'node-name(.)', value varchar2(30) path '.');

Found here

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号