How to Format the result driven from a SQL Query


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to Format the result driven from a SQL Query
# 1  
Old 06-12-2009
MySQL How to Format the result driven from a SQL Query

Hi All,

I want to format the result driven from the query into neat format.
For example pls find the below code,

#! /bin/sh

result='

sqlplus -s uname/passwrd@DBname

select no,name,address,ph_no, passport_no,salary,designation
from emp_table where salary>1000;

exit

EOF'

echo $result

The code results a huge set that is not fit into the screen.
the number of rows returned from the query is also not static.

please help me to the result in formatted manner.

please advice me if any coloring or formatting text style command are availabe.

Thankyou all in advance.Smilie
# 2  
Old 06-12-2009
use cut or awk. see man pages of cut.
# 3  
Old 06-12-2009
best way is to format in ORACLE and spool the output. use the set options to format your output.

somethng like this

Code:
 
#! /bin/sh

sqlplus -s "$connection_string"

spool ~/$HOME/sqloutput.txt

select no,name,address,ph_no, passport_no,salary,designation
from emp_table where salary>1000;

spool off

exit

EOF

# 4  
Old 06-19-2009
Hi Panyam/Rakeshawasthi,

Thank you for the assistance.
The 'spool' works fine for my requrement.

one more help please!!
please let me know, Can I use sqlplus command inside if condition.

i got error in the below code.

if [ $j -gt 1 ]
then
echo "*** Accounts in Server-5 $arg_5 ***"

sqlplus -s batchexc/rogerprod@FXGSM1U1 <<EOF

select * from cmf where account_no=80320398;

exit

EOF

fi

I got the error "./RunBill.sh[123]: Syntax error at line 127 : `<<' is not matched."

I really couldn't troubleshoot this. since i found , the code works fine before i include this if stmt.

moreover, the first line inside if also not get printed.

pls help me on this. Thanks in advance!!
# 5  
Old 06-19-2009
Code:
please let me know, Can I use sqlplus command inside if condition.

Yes you can use .

Check your code as the problem is not with what you have posted.
# 6  
Old 06-19-2009
Quote:
Originally Posted by little_wonder
Hi Panyam/Rakeshawasthi,

Thank you for the assistance.
The 'spool' works fine for my requrement.

one more help please!!
please let me know, Can I use sqlplus command inside if condition.

i got error in the below code.

if [ $j -gt 1 ]
then
echo "*** Accounts in Server-5 $arg_5 ***"

sqlplus -s batchexc/rogerprod@FXGSM1U1 <<EOF

select * from cmf where account_no=80320398;

exit

EOF

fi

I got the error "./RunBill.sh[123]: Syntax error at line 127 : `<<' is not matched."

I really couldn't troubleshoot this. since i found , the code works fine before i include this if stmt.

moreover, the first line inside if also not get printed.

pls help me on this. Thanks in advance!!

Not sure if writing exit is OK/necessary.
As a good practice login/passwd, you should give in the line after sqlplus...i.e.
Code:
sqlplus -s << EOF
batchexc/rogerprod@FXGSM1U1

# 7  
Old 06-24-2009
MySQL still I have the problem with sqlplus command inside 'if'.. pls help me..

Hi Panyam/Rakeshawasthi and All,

I have tried the 'sqlplus statement' inside 'if'. but i got the same error.

when i tried the same sqlplus without 'if' the query run perfectly.

I really really confused and can't get the solution.

pls assist me.

the below is my script (sql_if.sh)

#! /bin/sh

a=10

if [ $a -eq 10 ]
then

sqlplus -s << EOF
batchexc/rogerprod@FXCATU1

select * from student;

exit

EOF
fi


The output is: ./sql_if.sh[5]: Syntax error at line 8 : `<<' is not matched.

When I comment the 'if' statement this script works fine.. Smilie

I really can't analyse this scenario, pls guide me. Smilie
Thanks in advance!!Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Oracle simple SQL query result in: ORA-08103: object no longer exists

Dear community, please help with a query on Oracle. I'm using SQLPlus (but with SQLDeveloper is the same) to accamplish a sinple query like: select count(*) from ARCHIT_D_TB where (TYP_ID=22 OR TYP_ID=23) and SUB_TM like '%SEP%' and CONS=1234This is a very simple query that works perfect until... (5 Replies)
Discussion started by: Lord Spectre
5 Replies

2. Shell Programming and Scripting

Get SQL query result to file in putty

How to Get SQL query result to file in putty? I have one SQL query and I want that query output to be redirected to the file. uname -a SunOS XXX 5.8 Generic_117350-58 sun4u sparc SUNW,Sun-Fire-480R Please suggest. (7 Replies)
Discussion started by: pamu
7 Replies

3. Shell Programming and Scripting

From sql Insert Query to XML format

Hi How do I translate Let say Cat inserts.sql gives Insert into PM9_TAXATION_ROUNDING (STATE_GECODE, TAX_TYPE, TAX_AUTHORITY, SYS_CREATION_DATE, SYS_UPDATE_DATE, APPLICATION_ID, DL_SERVICE_CODE, ROUNDING_METHOD) Values ('xx', 'xx', 'x', TO_DATE('10/26/2012 13:01:20',... (3 Replies)
Discussion started by: anuj87in
3 Replies

4. UNIX for Advanced & Expert Users

Output the SQL Query result to a File

Hello Guys, This message is somewhat relates with last thread. But I need to re-write thing. I start over a little. I am stuck now and need your help. Here is my script- #! /bin/ksh export ORACLE_HOME=/opt/oracle/app/oracle/product/9.2 /opt/oracle/app/oracle/product/9.2/bin/sqlplus -s... (5 Replies)
Discussion started by: thepurple
5 Replies

5. Shell Programming and Scripting

How to format sql result as amount - ksh

I am currently returning an sql result with a number value that I want to format as an amount. The sql runs smoothly on its own, but when run inside my ksh script an error is encountered: ERROR at line 3: ORA-01481: invalid number format model My sql is -- select distinct ... (6 Replies)
Discussion started by: avillanueva
6 Replies

6. Shell Programming and Scripting

Convertion of Date Format using SQL query in a shell script

When I write Select date_field from TableA fetch first row only I am getting the output as 09/25/2009. I want to get the output in the below format 2009-09-25 i.e., MM-DD-YYYY. Please help (7 Replies)
Discussion started by: dinesh1985
7 Replies

7. Shell Programming and Scripting

How to use sql data file in unix csv file as input to an sql query from shell

Hi , I used the below script to get the sql data into csv file using unix scripting. I m getting the output into an output file but the output file is not displayed in a separe columns . #!/bin/ksh export FILE_PATH=/maav/home/xyz/abc/ rm $FILE_PATH/sample.csv sqlplus -s... (2 Replies)
Discussion started by: Nareshp
2 Replies

8. Shell Programming and Scripting

Redirecting sql select query result to txt file

Hi Yogesh, Lucky that i caught you online. Yeah i read about DBI and the WriteExcel module. But the server is not supporting these modules. It said..."Cannot locate DBI"..."Cannot locate Spreadsheet::WriteExcel" I tried creating a simple text file to get the query output, but the... (1 Reply)
Discussion started by: dolphin123
1 Replies

9. Shell Programming and Scripting

Redirecting sql select query result to txt file

Hi , I just found you while surfing for the string 'Redirecting sql select query output from within a shell script to txt file/excel file' Could you find time sending me the code for the above question? It'll be great help for me. I have a perl file that calls the sql file... (1 Reply)
Discussion started by: dolphin123
1 Replies

10. Shell Programming and Scripting

pass result from one query to another

Can any one help me how to pass the value of result of one query to another query. I to pass the value of result of 'select max(id) from a' into another query like update table set col =<value from last query> where ....; updatestaging() { xx=`$ORACLE_HOME/bin/sqlplus -s... (1 Reply)
Discussion started by: u263066
1 Replies
Login or Register to Ask a Question