Close Oracle database connection - sql

I am new to unix shell scripting and need a suggestion.
I have shell script which connects to the oracle db fetch the value and assign it outpt variable as mentioned below.
outpt=$(sqlplus Username/Pass#(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(Host=10.255.244.15)(Port=1674))(CONNECT_DATA=(SID=abc01))) <<EOF
set pages 0 echo off feed off
select max(date_sent) from test;
exit;
EOF
)
In windows we close the connection explicitly after executing the query. Is the code above implicitly do that for me or i have to explictly close connection.
If yes . Please help.

Related

SQL query is returning 0 when running shell script but 6 when checking db

I'm trying to run a shell script that has a SQL query in it. Now, I can't use a SQL script in the shell script because of story requirements. I have been trying to get the shell script to return the correct count which is '6' but it is only returning '0'.
#!/bin/ksh
. /apps/path/config/setenv.ksh
DATE=`date "+%m%d%Y`
returnMessage="`sqlplus username/password#$ORACLE_SID << EOF
WHENEVER OSERROR EXIT SQL.OSCODE ;
WHENEVER SQLERROR EXIT SQL.OSCODE ;
spool /apps/path/data/test.txt
SET HEADING OFF
SET FEEDBACK OFF
SET VERIFY OFF
SET ECHO ON
SET PAGES 0
SET LINESIZE 90
select count(*) from table where dt = to_date('06/18/2020','MM/DD/YYYY');
EOF
`
"
exitCode=$?
oracleError=`echo "$returnMessage" | grep ORA-`
if [ -n "$oracleError" -o "$exitCode" -ne 0 ]; then
log "An error occurred while looking up the $COUNT"
log "SQLPlus Exit Code = $exitCode"
log "SQLPlus Message is: $returnMessage"
return 1
fi
export COUNT=`echo $returnMessage"
return 0
The output is also given below
SQL> SET HEADING OFF
SQL> SET FEEDBACK OFF
SQL> SET VERIFY OFF
SQL> SET ECHO ON
SQL> SET PAGES 0
SQL> SET LINESIZE 90
SQL>
SQL> select count(*) from table where dt = to_date('06/18/2020','MM/DD/YYYY');
0
SQL>
THis is the output and the code I'm using. Not sure where it is going wrong since the query should return 6;
UnCOMMITted data is only visible within the session that created it (and will ROLLBACK at the end of the session if it has not been COMMITted). If you can't see the data from another session (i.e. in SQL*Plus invoked from the shell) then make sure you have issued a COMMIT command in the SQL client where you INSERTed the data.
Note: even if you connect as the same user, this will create a separate session and you will not be able to see the uncommitted data in the other session.
If you have issued a COMMIT and still can't see the data then make sure that both the SQL Client and the shell program are connecting to the same server and the same database and are querying the same user's schema of that database.

Create CSV Issue [duplicate]

I am running a file via batch file
Batch File:
sqlplus admin/admin#SERVER #abc.sql > output.txt
SQL File abc.sql:
set PAGESIZE 1000
set LINESIZE 550
set echo off
set head off
set FEEDBACK OFF
select * from S_ABC
exit;
Output.txt:
Connected To:
Oracle Database 11g................................
.
.
.
DATA
.
.
Disconnected from Oracle Database 11g .......
.
.
Please help me remove the extra data, in the starting and end of output.txt file.
-S seems to be what you're looking for;
sqlplus -S admin/admin#SERVER #abc.sql > output.txt
-S[ILENT]
Suppresses all SQL*Plus information and prompt messages, including the command prompt, the echoing of commands, and the banner normally displayed when you start SQL*Plus. If you omit username or password, SQL*Plus prompts for them, but the prompts are not visible. Use SILENT to invoke SQL*Plus within another program so that the use of SQL*Plus is invisible to the user.

Calling batch file from SQL CLP Script

Hi I need to run a batch file from a sql-clp script:
The script is
CONNECT TO MYTAB1 USER xxxx using yyyyyyy;
QUIESCE DATABASE IMMEDIATE FORCE CONNECTIONS;
CONNECT RESET;
BACKUP DATABASE MYTAB1 TO "C:\temp\bcks" WITHOUT PROMPTING;
CONNECT TO MYTAB1 USER xxxx using yyyyyyy;
UNQUIESCE DATABASE;
CONNECT RESET;
cmd.exe /c "C:\Users\xxxx\Desktop\backup_neu.bat C:\temp\bcks C:\temp\bcks\zips 7z");
It runs great until it reaches the last line.
I tried
cmd.exe /c
exec(' xp_cmdshell ''script_here');
EXEC master..xp_CMDShell '"script here "'
but nothing worked.
OI have DB2 v10 running.
Any ideas on how I can get the batch file running?
Thanks for all your help.
TheVagabond
Ok I found the solution....
really simple somehow, just needed
!C:\Users\xxxx\Desktop\backup_neu.bat C:\temp\bcks C:\temp\bcks\zips 7z
so only a ! that was it.

How to transfer to unix variable

I have written a script so that i can read from an oracle database and display it once i run the script. Is there any way that I can transfer the result to unix variable?
$ORACLE_HOME/bin/sqlplus -s /nolog<<EOF
connect useid/password#CFQ143
set pages 0 feed off
select count (platform) from platformspecific where platform='EF';
exit
EOF
I found the answer - rather than use single quotes to pass the value to a variable, you can use open and close brackets to transfer the values:
var=$($ORACLE_HOME/bin/sqlplus -s /nolog<<EOF
connect useid/password#CFQ143
set pages 0 feed off
select count (platform) from platformspecific where platform='EF';
exit
EOF)
echo $var

PLSQL SQL script combining parameters with connect

Does anyone know if what I'm trying to do in the below code is possible and if so what the syntax is? This issue is around the connect call, the username doesn't seem to generate correctly. The commented out connect call is another one I tried.
-- myscript.sql
-- #params:
-- 1 - Oracle database name eg. localhost
-- 2 - Site (site01, site02 site03)
connect systemname_%2_admin/mypassword#&1;
--connect "systemname_" || "%2" || "_admin"/mypassword#&1;
begin
--execution code here.
end;
/
disconnect;
NOTE: Call does need to be this way as this is going to be an automated script doing different things for different usernames.
Your arguments will be stored in substitution variables 1, 2 and so on.
You access them in your script with &1, &2 (so forget about %2, it's meaningless).
Now your problem is that &2_admin looks to sqlplus like a substitution variable named 2_admin so you just need to add a dot . after the 2. Dot is the character that separates the name of a substitution variable from what follows.
your connect will look like :
connect systemname_&2._admin/mypassword#&1
(With no ; : this is a sqlplus command not an sql statement).