SQLPLUS query in Unix script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting SQLPLUS query in Unix script
# 1  
Old 03-01-2010
SQLPLUS query in Unix script

Hi,

I am using sqlplus query to get results in a csv format in unix. I am using ksh, and below is the query.

Code:
echo "select r.num|| ',' || p.path ||',"' || r.issue_description ||'",' ||p.timestamp from events r, messagepath p;">> $QUERY_FILE
 
sqlplus -s $LOGIN @ $QUERY_FILE>>$OUTFILE

The query runs fine and creates OUTFILE. The problem is instead of value of r.issue_description coming in the results, it is being outputted as it is. The output is like below.

123,temp.node.leaf, ||r.issue_description||,20100301
126,temp.node1.node2.leaf, ||r.issue_description||,20100301
236,temp.node.node3.leaf, ||r.issue_description||,20100301
589,temp.node.node4.leaf, ||r.issue_description||,20100301

The same query runs fine when ran against sql developer. I am assuming its something to do with the unix single and double quotes. Please can someone help.
# 2  
Old 03-01-2010
Try to remove the double quotes inside the string:

Code:
echo "select r.num|| ',' || p.path ||',' || r.issue_description ||',' ||p.timestamp from events r, messagepath p;" >> $QUERY_FILE

# 3  
Old 03-01-2010
Hi,

Earlier the query was same without the double quotes and works fine. But the problem is, since the issue_discription has commas in its value, while opening the csv file, the formatting is all lost. Hence I included the quotes to enclose the text value as should be done normally while running any DB query.

---------- Post updated at 05:06 AM ---------- Previous update was at 04:28 AM ----------

Hi, I got the issue. Just use the escape character and it works!!

Code:
echo "select r.num|| ',' || p.path ||',\"' || r.issue_description ||'\",' ||p.timestamp from events r, messagepath p;">> $QUERY_FILE
 
sqlplus -s $LOGIN @ $QUERY_FILE>>$OUTFILE

Thanks all.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Control not returning from Sqlplus to calling UNIX shell script.

Hello All, I have a UNIX script which will prepare anonymous oracle pl/sql block in a temporary file in run time and passes this file to sqlplus as given below. cat > $v_Input_File 2>>$v_Log << EOF BEGIN EXECUTE IMMEDIATE 'ALTER SESSION FORCE PARALLEL DML PARALLEL 16'; EXECUTE... (1 Reply)
Discussion started by: vikas_trl
1 Replies

2. Shell Programming and Scripting

Shell Script (ksh) - SQLPlus query filter using a string variable

Using ksh, I am using SQLPlus to execute a query with a filter using a string variable. REPO_DB=DEV1 FOLDER_NM='U_nmalencia' FOLDER_CHECK=$(sqlplus -s /nolog <<EOF CONNECT user/pswd_select@${REPO_DB} set echo off heading off feedback off select subj_name from subject where... (5 Replies)
Discussion started by: nkm0brm
5 Replies

3. Shell Programming and Scripting

How to pass Variable from shell script to select query for SqlPlus?

echo "set echo off"; echo "set feedback off"; echo "set linesize 4000"; echo " set pagesize 0"; echo " set sqlprompt ''"; echo " set trimspool on"; Select statement is mentioned below echo "select res.ti_book_no from disney_ticket_history res where res.ti_status =${STATUS} and... (7 Replies)
Discussion started by: aroragaurav.84
7 Replies

4. Shell Programming and Scripting

Problems with storing oracle sqlplus query output shell script

Hello everyone, I have a RHEL 5 system and have been trying to get a batch of 3-4 scripts each in a separate variables and they are not working as expected. I tried using following syntax which I saw a lot of people on this site use and should really work, though for some reason it doesn't... (3 Replies)
Discussion started by: rockf1bull
3 Replies

5. UNIX for Dummies Questions & Answers

calling a unix shell script from sqlplus

I want to execute a shell script from sqlplus prompt and get its output back to sqlplus. Is this possible? if yes just give me an example for doing that. (2 Replies)
Discussion started by: boopathyvasagam
2 Replies

6. Shell Programming and Scripting

(Urgent):Creating flat file using sql script and sqlplus from UNIX Shell Script

Hi, I need help urgently for following issue. Pls help me to resolve this issue. I am calling sql script file(file1.sql) from UNIX Shell Script(script1.ksh) using sql plus and trying to create flat file that contains all records returned from SQL query in SQL script(file1.sql) I given... (6 Replies)
Discussion started by: praka
6 Replies

7. Shell Programming and Scripting

In a csh script, can I set a variable to the result of an SQLPLUS select query?

Can someone tell me why I'm getting error when I try to run this? #!/bin/csh -f source ~/.cshrc # set SQLPLUS = ${ORACLE_HOME}/bin/sqlplus # set count=`$SQLPLUS -s ${DB_LOGIN} << END select count(1) from put_groups where group_name='PC' and description='EOD_EVENT' and serial_number=1;... (7 Replies)
Discussion started by: gregrobinsonhd
7 Replies

8. Solaris

SQLPLUS unable to execte query automatically from solaris script

Hi, I am trying to execte the .sql file(which contain the two different sql query) from solaris script using sqlplus command, the script is executing successfully but the issue is when i execute the script, it is not terminating automatically, for example if i put one sql statement then i have... (2 Replies)
Discussion started by: jyotisree
2 Replies

9. UNIX for Advanced & Expert Users

Executing SQLPLUS in UNIX Script from JAVA

Hi ALL, I would like to execute one SQL query(ORACLE) in UNIX shell script. For this I used sqlplus in script and tested locally. It worked fine. But my requiremnt is to execute the script from Java. In this case the UNIX part is working but sqlplus is not returning anything The JAVA code used... (0 Replies)
Discussion started by: anooptech
0 Replies

10. Shell Programming and Scripting

help me in sending parameters from sqlplus script to unix shell script

Can anybody help me out in sending parameters from sql*plus script to unix shell script without using flat files.. Initially in a shell script i will call sql*plus and after getting some value from some tables, i want that variable value in unix shell script. How can i do this? Please tell me... (2 Replies)
Discussion started by: Hara
2 Replies
Login or Register to Ask a Question