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
# 15  
Old 12-21-2006
Quote:
Originally Posted by abdulhafeez
Hi All

I have tried the following script but am not getting any output, can any one help me to know the problem in my 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

Thanks & Regards
Abdul Hafeez Shaik


Hello abdul,

you have used ' instead of ` in the above code.
look for the above symbol above tab button in keyboard .. :-)
# 16  
Old 12-24-2006
dude,

why not just redirect the output to a file first so you can analyze what is wrong with the output. since from experience when you perform sqlplus there are some garbage which you do not need. then start from there.

Code:
sqlplus -s apps/apps <<endsql > output.txt
set feedback off
set heading off
select 6 from dual;
exit;
endsql

or

Code:
sqlplus -s apps/apps <<endsql
set feedback off
set heading off
select 6 from dual;
spool output.txt;
r;
exit;
endsql

also from experience sometimes when you put the query into script it will give you no output. so try to spool the output then you can start from there or either way try them both then filter out the garbage so you can have what you want. a log file is also good for troubleshooting but if you do not like you can just redirect the result to a variable.

hope this helps.
# 17  
Old 12-26-2006
SQL-> Variable

$Test_Var=
`sqlplus -s $USERNAME/$PASSWD@$DATABASE <<START
set echo off;
set head off;
select 10 from dual;
exit;
START | tr -d " " `

Output
$echo $Test_Var
10
$
# 18  
Old 10-22-2008
Hi Team,

I have tried code to get the value from oracle. but it is not working. The error shows invalid identifier. but if i execute same query in oracle SQL*PLUS it is working fine.

v_test=$(sqlplus -s apps/apps <<END
set feedback off;
set heading off;
select c_code from tab_code where c_name = 'NAM';
EXIT;
END)
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