get the result of database query in vertical way (Column=Value)

the database client sqlplus generate results one line per row. This function convert the sqlplus output to show the result vertically, in layout: Column_name=[Column_Value]. Very usefull for scripts.
Sample Output
$function vsqlplus(){ cn=0; ROWNUM=-2; sqlplus -s USER/PASSWORD@DATABASE < <(echo -e "set colsep \"'\"\nset timing off\nset feedback off\nset lines 32676\nset pages 8000\n$1";) | sed "/^$/d" | tr -d '\t' | tr -s ' ' | while read cols; do ((++ROWNUM == -1)) && eval "array_cols=( $(echo ${cols} | tr \' ' ') )"; (( ROWNUM > 0 )) && ( echo "ROWNUM=$ROWNUM"; for (( c = 0; c < ${#array_cols[*]}; c++ )); do echo "  ${array_cols[(c) % ${#array_cols[*]}]}=\"$(echo "$cols"|cut -d\' -f$((c+1))  )\""; done; ) done; }; vsqlplus "SELECT * FROM TABLE_NAME;";

2010-03-01 17:11:07

These Might Interest You

  • Similar output to using MySQL with the \G at the end of a Query. Displays one column per line. Other modes include: -column Query results will be displayed in a table like form, using whitespace characters to separate the columns and align the output. -html Query results will be output as simple HTML tables. -line Query results will be displayed with one value per line, rows separated by a blank line. Designed to be easily parsed by scripts or other programs -list Query results will be displayed with the separator (|, by default) character between each field value. The default. From inside the command line this can be also changed using the mode command: .mode MODE ?TABLE? Set output mode where MODE is one of: csv Comma-separated values column Left-aligned columns. (See .width) html HTML code insert SQL insert statements for TABLE line One value per line list Values delimited by .separator string tabs Tab-separated values tcl TCL list elements Show Sample Output

    sqlite3 -line database.db
    pykler · 2010-10-09 16:10:19 0
  • This command will "su" the execution of the command to the postgres user(implies that you are already logger as root), and export the result of the query to a file on the csv format. You'll need to adequate the fields and database information to one of your choice/need. Show Sample Output

    # su -c "psql -d maillog -c \"copy (select date,sender,destination,subject from maillog where destination like '') to '/tmp/mails.csv' with csv;\" " postgres
    Risthel · 2013-02-13 13:03:17 0
  • Outputs Windows Services service name and display name using "sc query", pipes the output to "awk" for processing, then "column" for formatting. List All Services: sc query state= all | awk '/SERVICE_NAME/{printf"%s:",$2;getline;gsub(/DISP.*:\ /,"");printf"%s\n",$0}' | column -ts\: List Started Services: sc query | awk '/SERVICE_NAME/{printf"%s:",$2;getline;gsub(/DISP.*:\ /,"");printf"%s\n",$0}' | column -ts\: List Stopped Services: sc query state= inactive| awk '/SERVICE_NAME/{printf"%s:",$2;getline;gsub(/DISP.*:\ /,"");printf"%s\n",$0}' | column -ts\: Show Sample Output

    sc query state= all | awk '/SERVICE_NAME/{printf"%s:",$2;getline;gsub(/DISP.*:\ /,"");printf"%s\n",$0}' | column -ts\:
    lowjax · 2015-02-15 22:35:10 2

  • 0
    mysql -u[user] -p[password] -h [hostname] -D [database] -ss -e "select * from mysql_tbl " | sed 's/\t/","/g;s/^/"/;s/$/"/;s/\n//g' > dump.csv
    mandroid · 2011-07-08 08:03:53 2

What Others Think

What's this? vsqlplus?? I think it's missing something, right? Regards!
Enirevlow · 365 weeks and 6 days ago
vsqlplus is a user function ... look at sample output
glaudiston · 135 weeks and 2 days ago

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this? is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands

Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.


Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: