How to redirect value from sql select statment to unix variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to redirect value from sql select statment to unix variable
# 8  
Old 12-05-2006
hi,

I have tried this script but this time i did not got the expected result, instead am getting the output diffrently

when i ran the follwoing script

export v_test=0
v_test=$(sqlplus -s apps/apps <<END
set feedback off
set heading off
select 6 from dual;
exit;
END)
echo out put is $v_test

then the output is :

sh test.sh
out put is Usage: SQLPLUS [<option>] [<user>[/<password>] [@<host>]] [@<startfile> [<parm1>] [<parm2>] ...] where <option> ::= { -s | -? } -s for silent mode and -? to obtain version number


but i was expecting:

out put is 6

can you please let me know what to do
# 9  
Old 12-05-2006
try one by one

Hi,
Iam not pretty sure,can u able to run the commands separately.
first sqlplus -s apps/apps like that
# 10  
Old 12-05-2006
how about this
Code:
sqlplus -s apps/apps <<END
set feedback off
set heading off
select 6 from dual;
exit;
END
v_test=$?
echo output is $v_test

# 11  
Old 12-05-2006
will return only the status not the value

Hi andryk,
the variable v_test=$? wont catch the value 6 it will catch only the return status of the previous command.
# 12  
Old 12-05-2006
tell what error u got? did u check your sql separatly if it is working correct??
I have so many scripts which does like this .. I dont know what you are doing wrong?..

v_syse=`${ORACLE_HOME}/bin/sqlplus -s user_id/password <<END
select '6' from dual ;
EXIT;
END`
# 13  
Old 12-05-2006
Quote:
Hi andryk,
the variable v_test=$? wont catch the value 6 it will catch only the return status of the previous command.
Smilie sorry, didnt look at the question!!!
But why dont you redirect the result to a file and work from there
Code:
sqlplus -s apps/apps <<END
set feedback off;
set head off;
spool /tmp/somefile.result;
select 6 from dual;
exit;
END
echo output is 
cat /tmp/somefile.result
rm /tmp/somefile.result

# 14  
Old 12-05-2006
NLS=`${ORACLE_HOME}/bin/sqlplus -s<<EOF
/ as sysdba
set heading off
select value from nls_database_parameters where parameter='NLS_CHARACTERSET';
EOF`


echo $NLS


works fine here
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Execute sql statment in korn shell

I am fairly new to writing scripts, and have gotten a lot of help from this site in the past with many of the posts. I have a question/issue with a script I am attempting to write and have a question regarding executing an sql statement inside of a loop (do while). I have in the past written... (1 Reply)
Discussion started by: josbor01
1 Replies

2. Shell Programming and Scripting

SQL output to UNIX variable

I have a sql statement , i need to assign to a variable in Unix sel count(*) AS num_files from TABLE_A; i need to use "num_files" in unix statements. let me know how to assign unix variable to above num_files (1 Reply)
Discussion started by: nani1984
1 Replies

3. Shell Programming and Scripting

UNIX variable to SQL statement

The following is my script : #!/bin/bash echo "please give app_instance_id" read app_instance_id echo "id is $app_instance_id" export app_id=app_instance_id sqlplus -s nnviewer/lookup@//nasolora008.enterprisenet.org:1521/LOAD3 @test.sql<<EOF SPOOL /home/tibco/MCH/Data/qa/raak/name.xls... (4 Replies)
Discussion started by: raakeshr
4 Replies

4. Shell Programming and Scripting

Redirect output from SQL to unix variable

Hi, I have a requirement to store oracle sqlplus output to some unix variable, count the records and then print the output on the screen. Can you please point me to any sample program for reference purpose. Thanks a lot for your time. (0 Replies)
Discussion started by: bhupinder08
0 Replies

5. Shell Programming and Scripting

Assigning value of a select count(*) from tablename ( run on db2 ) to a unix variable

Hi All, I have browsed through the forums for a similar topic, but there is no topic which explains about this problem, when the backend is DB2. I want to assign the output of a select count(*) from tablename to a unix variable in a shell script. I am using ksh. The database used to... (3 Replies)
Discussion started by: Siddarth
3 Replies

6. Shell Programming and Scripting

I want to get the Unix variable value in the sql stmt

Hi All I have a requirement where in I am stuck. There is a shell script that is being developed by me. It consist of the sql stmt also. I need to export a variable called HOMEPAGE with a value say www.abc.com. and then use this $HOMEPAGE variable in the sql stmt. My ultimate aim is to fetch all... (1 Reply)
Discussion started by: amitsinha
1 Replies

7. UNIX for Dummies Questions & Answers

using unix variable in select column in awk

Hi, I have file on below pattern, and i want to write a generic awk to handle all cases. input_file: col1~col2~col3~col4~col5~col6 I need to generate 4 files something like this File1: col1~~col2~~col3 File2: col1~~col2~~col4 File3: col1~~col2~~col5 File4: col1~~col2~~col6 (1 Reply)
Discussion started by: luckybalaji
1 Replies

8. Shell Programming and Scripting

Redirect Postgresql Select Query to Variable

How to redirect postgresql select query to a variable using shell scripts? I tried to use psql -c "select ip from servers" db | egrep '+\.+\+\+' | sed 's/^ *\(.*\) *$/\1/' Output: 10.10.10.180 10.10.10.181 It display ip addresses from the databasem, but how could i redirect the... (3 Replies)
Discussion started by: uativan
3 Replies

9. UNIX for Dummies Questions & Answers

select count(*) in sqlplus into variable unix shell

Need to select count(*) from table to check for zero result in unix script (2 Replies)
Discussion started by: struggle
2 Replies

10. Shell Programming and Scripting

sql query variable not exactly unix

I know htis isnt exactly unix.... but hopefully someone can help me or direct me someplace to get help. I can run sql queries in scripts against my informix db using: dbaccess mydb myquery.sql >> sql.output I need to write my script to select based on todays date. Its very... (5 Replies)
Discussion started by: MizzGail
5 Replies
Login or Register to Ask a Question