Connecting to Oracle DB using sqlplus


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Connecting to Oracle DB using sqlplus
# 15  
Old 05-05-2010
Hi.

(minus the connection string - that I'm not totally sure of the reason for) your script is OK, except for the single quotes

Code:
'select sysdate from dual;
EOF'

I'm not sure what purpose they serve.

Your echo statement afterwards (echo "Output_variable, x=$sysdate" is echoing a variable that you never set.
# 16  
Old 05-05-2010
Try this approach:

Code:
my_sql=$(sqlplus -s /nolog <<EOF
connect scott/tiger
set pages 0 
select sysdate from dual; 
exit
EOF)

echo $my_sql

In your case, you can certainly try the following, but you might just need to adjust your tnsnames.ora file so that you can work with just the label or the SID itself:

Code:
my_sql=$(sqlplus -s /nolog <<EOF
connect gidicm/gidicm@'(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=10.5.199.106)(PORT=1521))(CONNECT_DATA=(SID=PHYGD1)))'

set pages 0 
select sysdate from dual; 
exit
EOF)

echo $my_sql

# 17  
Old 05-05-2010
Quote:
#!/bin/bash
sqlplus gidicm/gidicm@"(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=10.5.199.106)(PORT=1521))(CONNECT_DATA=(SID=PHYGD1 )))"<< EOF
'select sysdate from dual;
EOF'
echo "Output_variable, x=$sysdate"

Further to "scottn". We lose the spurious single quotes and now try to get the output from the query into a unix environment variable called $myoutput . I've avoided using the name $sysdate for clarity.
The backticks `` enclose the entire sqlplus run and dump the output into the environment variable $sysdate (untested).

Code:
#!/bin/bash
myoutput=`sqlplus gidicm/gidicm@"(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=10.5.199.106)(PORT=1521))(CONNECT_DATA=(SID=PHYGD1)))"<< EOF
select sysdate from dual;
EOF
`
echo "Output_variable ${myoutput}"

If this works, we move to the next step which is getting Oracle to output a clean result without SQL prompts etc. .



Please also read curleb post above (which I had not read prior to posting) because it contains valuable advice. The lengthy connection string is unusual (and until today I didn't know that syntax). The $( ... commands ... ) syntax is a modern alternative to backticks and recommended by many posters.
The post from curleb also tackles many of the issues with unwanted output from the sqlplus program.

Last edited by methyl; 05-05-2010 at 06:35 PM.. Reason: Reference curleb
# 18  
Old 05-06-2010
@curleb - Thanks a lot for your help!! The code provided by you
Code:
my_sql=$(sqlplus -s /nolog <<EOF
connect gidicm/gidicm@'(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=10.5.199.106)(PORT=1521))(CONNECT_DATA=(SID=PHYGD1)))'

set pages 0 
select sysdate from dual; 
exit
EOF)

echo $my_sql

Does return a value for $my_sql variable. Now the only problem I am left with is - the output which it returns is as follows:

Connected. 06-MAY-10

I do not need the word 'Connected' in the output. I only need the date part.

Could you please let me know how do i get that?

Thanks a ton for all your help!

Highly appreciated!!

Please advise.

Last edited by shrutihardas; 05-06-2010 at 11:24 AM..
# 19  
Old 05-06-2010
Quote:
Originally Posted by shrutihardas
@curleb - The code provided by you
Code:
my_sql=$(sqlplus -s /nolog <<EOF
connect gidicm/gidicm@'(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=10.5.199.106)(PORT=1521))(CONNECT_DATA=(SID=PHYGD1)))'

set pages 0 
select sysdate from dual; 
exit
EOF)

echo $my_sql

Establishes a database connection and fires the query on the database. But the my_sql variable does not return anything. It returns a blank output. I guess the $ (....command....) doesnt seem to work at my end. Do you have any other suggestions? I am sure its a very small tweak somewhere in the code that I need. Wondering what am I missing!!

Please advise.
Hi shrutihardas,

I remember from a long time ago, that under some situations $(...) did not work with SQL*Plus (so long, that I don't remember which circumstances Smilie)

See if using back-ticks, instead, helps:
Code:
my_sql=`sqlplus -s /nolog <<EOF
connect gidicm/gidicm@'(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=10.5.199.106)(PORT=1521))(CONNECT_DATA=(SID=PHYGD1)))'

set pages 0 
select sysdate from dual; 
exit
EOF`

echo $my_sql

Both `...` and $(...) work for me, though.
# 20  
Old 05-06-2010
I am sorry - The code provided by curleb actually works like a charm!

@curleb - Thanks a lot for your help!!

The code Does return a value for $my_sql variable. Now the only problem I am left with is - the output which it returns is as follows:

Connected. 06-MAY-10

I do not need the word 'Connected' in the output. I only need the date part.

Could you please let me know how do i get that?

Thanks a ton for all your help!

Highly appreciated!!

Please advise.
# 21  
Old 05-06-2010
@shrutihardas: I'd think you're still echoing the "Connected." portion of the output from your script...possibly the same typo that was causing the seemingly blank output of the $my_sql variable.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sqlplus not connecting the 2nd time in for loop

Hi, I am trying to get the rows(First step is to get the poolid's and then second step run a loop to get the output based on each pool id and third connection is to get the member id and pool id based on a different condition) where based of certain conditions and storing it in a file. I wrote the... (6 Replies)
Discussion started by: ajayakunuri
6 Replies

2. Shell Programming and Scripting

Connecting sqlplus from UNIX with multiple select statement

hi, i have a requirement where i need to connect sqlplus from unix and i am able to do so by following command: cust_count=`sqlplus -s $ORACLE_USER/$ORACLE_PASS@$ORACLE_SID << EOF set pagesize 0 set feedback off set verify off ... (1 Reply)
Discussion started by: lovelysethii
1 Replies

3. Shell Programming and Scripting

Connect to Oracle using sqlplus

I have logged into oracle using SQLPLUS. When I type any kind of query, there is only 1 answer - '2'. What is wrong with it? (1 Reply)
Discussion started by: Subhasis
1 Replies

4. Red Hat

TNS Timeout Error when connecting to SQLPLUS through scripts only

Hi, I am facing a strange issue when connecting to SQLPLUS via a shell scripts. I am using Linux 2.6.18-274.18.1 and gbash shell. When I connect to SQLPLUS through scripts then it throws TNS Time Out error ""sometimes"" and connects successfully other times.This is only happening when... (9 Replies)
Discussion started by: aashish.sharma8
9 Replies

5. UNIX for Advanced & Expert Users

Connecting once using sqlplus and doing multiple queries

Hello everyone, It's my first week using unix and shell scripting. I tried creating a script that has a function that execute SQL query. my script looks something like this: ---------------------------------------------------- #!/bin/sh tableName="myTable" secondTable="secondTable"... (2 Replies)
Discussion started by: edlin_r
2 Replies

6. UNIX for Dummies Questions & Answers

Connecting to Oracle DB using sqlplus

Hi, I am very new to shell scripting and trying to write a simple shell script in which i am trying to achieve the following: 1. Connect to oracle database hosted on a different server 2. fire a query on the oracle db 3. store the output in a variable 4. use this variable for further logic... (1 Reply)
Discussion started by: shrutihardas
1 Replies

7. Shell Programming and Scripting

Formatting Oracle sqlplus output

a job extracts orcle data into unix as flat file. a single record breaks into two record in unix flat file. This is the case only for 6 records out of 60 lack records. (its not single record in two line. but its single record into record. ie., \n come into picture) can you tell me what... (6 Replies)
Discussion started by: Gopal_Engg
6 Replies

8. Shell Programming and Scripting

connecting through sqlplus

I am trying to connect to one of the oracle sever using uni through sqlplus command: sqlplus -s BOXI_ALPH_AUDITOR,Q078_audit$@Q047 But its not getting connected. I tried using some different server using same syntax its working. What differene i found is the password is having no special... (2 Replies)
Discussion started by: gander_ss
2 Replies

9. Shell Programming and Scripting

Showing errors when connecting to sqlplus in shell script

hi, I am trying to automate the compilation of procedures stored in .sql files in Unix. Is there any way in which we can sho err if there errors are raised in the compilation? I am using the following code to connect to the sqlplus sqlplus ${SQL_USER}/${SQL_PASSWORD} (5 Replies)
Discussion started by: silas.john
5 Replies

10. UNIX for Dummies Questions & Answers

error connecting to sqlplus

Hi, I wrote a shell script to call oracle procedure. But when i am trying to connet sqlplus with the fallowing statement It is giving me error " callproce.sh : sqlplus: not found". What could be the problem. sqlplus -s $CONNECT_STRING >$LOGFILE <<!! thank u all papachi (2 Replies)
Discussion started by: papachi
2 Replies
Login or Register to Ask a Question