I am automating a process where I have to connect to Oracle database and run a script automatically.
Can somebody help me in achieving this.
I have written a script but when I am executing the below scripts it's not returning any output.
Code :
#echo off
set user_name=<username>
set password=<password>
set net_service_name= <tns_name>
echo exit | sqlplus -s %user_name%/%password%#%net_service_name% #f:\test.sql
pause
I am using Oracle 11g. I am connecting to database of a specific region.
Also, I need to connect to database as an admin user.
I think the problem is in the space in net_service_name. "-S" is silent mode for sqlplus. For diagnostics, remove this key and add rem #echo off.
For example 1. The space in net_service_name.
rem #echo off
set user_name=scott
set password=tiger
set net_service_name= esmd
echo exit | sqlplus -s %user_name%/%password%#%net_service_name% #C:\upwork\stackoverflow\bat_sql\sqltest.sql
pause
output:
C:\upwork\stackoverflow\bat_sql>echo exit | sqlplus -s scott/tiger# esmd #C:\upwork\stackoverflow\bat_sql\sqltest.sql
Usage: SQLPLUS [ [<option>] [<logon>] [<start>] ]
where <option> ::= -H | -V | [ [-L] [-M <o>] [-R <n>] [-S] ]
<logon> ::= <username>[/<password>][#<connect_string>] | / | /NOLOG
<start> ::= #<URI>|<filename>[.<ext>] [<parameter> ...]
"-H" displays the SQL*Plus version banner and usage syntax
"-V" displays the SQL*Plus version banner
"-L" attempts log on just once
"-M <o>" uses HTML markup options <o>
"-R <n>" uses restricted mode <n>
"-S" uses silent mode
C:\upwork\stackoverflow\bat_sql>pause
For example 2.
rem #echo off
set user_name=scott
set password=tiger
set net_service_name=esmd
echo exit | sqlplus -s %user_name%/%password%#%net_service_name% #C:\upwork\stackoverflow\bat_sql\sqltest.sql
pause
output:
C:\upwork\stackoverflow\bat_sql>echo exit | sqlplus -s scott/tiger#esmd #C:\upwork\stackoverflow\bat_sql\sqltest.sql
SYSDATE
--------
24.01.19
Elapsed: 00:00:00.00
C:\upwork\stackoverflow\bat_sql>pause
For example 3. Connect as sysdba.
rem #echo off
set user_name=sys
set password=manager
set net_service_name=esmd as sysdba
echo exit | C:\oracle\instantclient_11_2\sqlplus.exe -s %user_name%/%password%#%net_service_name% #C:\upwork\stackoverflow\bat_sql\sqltest.sql
pause
output:
C:\upwork\stackoverflow\bat_sql>echo exit | C:\oracle\instantclient_11_2\sqlplus.exe -s sys/manageresmd#esmd as sysdba #C:\upwork\stackoverflow\bat_sql\sqltest.sql
SYSDATE
--------
24.01.19
C:\upwork\stackoverflow\bat_sql>pause
For example 4. Connect as sysdba.
rem #echo off
set user_name=sys
set password=manager
set net_service_name=esmd as sysdba
rem
(
echo conn %user_name%/%password%#%net_service_name%
echo #C:\upwork\stackoverflow\bat_sql\sqltest.sql
echo exit
)| sqlplus -s /nolog
pause
output:
C:\upwork\stackoverflow\bat_sql>(
echo conn sys/manageresmd#esmd as sysdba
echo #C:\upwork\stackoverflow\bat_sql\sqltest.sql
echo exit
) | sqlplus -s /nolog
Connected.
SYSDATE
--------
24.01.19
Elapsed: 00:00:00.00
For example sqltest.sql.
C:\upwork\stackoverflow\bat_sql>more sqltest.sql
select sysdate from dual;
Related
I am trying to call pl/sql block in unix script using sqlplus. I just tried to print a statement but nothing is getting printed and I am not getting any error as well.
Result=`sqlplus -s $TgtUsrID/$TgtPswd#$TgtServer <<eof
whenever sqlerror exit sql.sqlcode;
SET SERVEROUTPUT ON;
BEGIN
DBMS_OUTPUT.PUT_LINE('Hello World!');
END;
eof
`
current_time=`date`
echo " Script execution finished at $current_time"
Just need to prepend the variable(Result) with the dollar operator after the last EOF
along with an echo command such as
Result=`sqlplus -S /nolog << EOF
conn $TgtUsrID/$TgtPswd#$TgtServer
whenever sqlerror exit sql.sqlcode
set feedback off
SET SERVEROUTPUT ON;
BEGIN
DBMS_OUTPUT.PUT_LINE('Hello World!');
END;
/
EOF`
echo $Result
echo Script execution finished at $(date)
would yield such a result
Hello World!
Script execution finished at Tue Feb 14 00:15:51 +03 2021
where quotes for the description after the last echo is redundant, and prefer using connection with /nolog as being securer, since the password of the schema would be unveiled whenever ps -ef command issued in the current case.
I am new to the shell scripting hence need help. I am trying to execute sql query against Oracle DB. Once sql query result is received, I need to iterate through the result (as it will return multiple rows and columns.).
Goal here is to invoke a REST api using curl for each record retrieved in db result. Input to REST api will be ROOT_PROC_ID column value.
Below is the shell script I have developed so far and sample output of the sql query.
#!/bin/bash
#Update below properties as per enviornment
export ENV=DEV;
export SERVERHOST=localhost
export SERVERPORT=9000
export SERVERUSER=admin
export SERVERPASSWORD=admin123
export DBHOST=localhost
export DBPORT=1537
export DBSID=ORCL
export DBUSER=SCOTT
export DBPASS=TIGER
export LOGDIR=/usr/app/$USER/data/logs/
#-------------------------------------------------------------------------
#----------- DO NOT EDIT AFTER THIS LINE ---------------------------------
#-------------------------------------------------------------------------
#create directory structure if not exists for storing log files
mkdir -p $LOGDIR/process_cancellation
mkdir -p $LOGDIR/process_cancellation/old
mkdir -p $LOGDIR/process_cancellation/halted
export old_proc_cancellation_logfile=$LOGDIR/process_cancellation/old/log_$(date "+%Y%m%d%H%M%S").log;
export halted_proc_cancellation_logfile=$LOGDIR/process_cancellation/halted/log_$(date "+%Y%m%d%H%M%S").log;
#execute sql query to fetch halted process data from database
echo
echo "Enviornment : $ENV"
echo
echo "Connecting to - $DBUSER/$DBPASS#$DBHOST:$DBPORT/$DBSID"
echo
echo "Retrieving halted process data logged before : $(date -d "15 days ago" +%d/%m/%Y) 20:00:00"
echo
sqlplus -s $DBUSER/$DBPASS#$DBHOST:$DBPORT/$DBSID << FIN_SQL > $halted_proc_cancellation_logfile
set head off
set line 1024
set pages 9999
SET SERVEROUTPUT ON;
SELECT ROOT_PROC_ID, PROC_ID, PROC_NAME, START_DATE, STATUS, ORDER_REF
FROM USER.PROC_STATUS
WHERE START_DATE<(SYSDATE - 15) AND (STATUS='proc_halted' OR STATUS='proc_failed')
ORDER BY START_DATE DESC;
SET SERVEROUTPUT OFF;
FIN_SQL
echo "Please check log file for more details : $(readlink -f $halted_proc_cancellation_logfile)"
exit
Sample SQL query output:
ROOT_PROC_ID PROC_ID PROC_NAME START_DATE STATUS ORDER_REF
pvm:0a123akpd pvm:0a123akkh FunctionalErrorProcess 28-NOV-19 01.24.35.115000000 PM pi_halted 2642277
pvm:0a122utrn pvm:0a122uun0 TechnicalErrorProcess 22-NOV-19 02.28.17.217000000 PM pi_halted 2642278
pvm:0a122utl2 pvm:0a122uu1t TechnicalErrorProcess 22-NOV-19 02.27.54.024000000 PM pi_halted 2642279
pvm:0a122utln pvm:0a122uu22 TechnicalErrorProcess 22-NOV-19 02.27.50.287000000 PM pi_halted 2642280
Assuming your sql query output is in output.txt:
awk 'NR!=1' output.txt | while read rootprocid undef
do
callApi $rootprocid
done
NR!=1 is to skip the 1st line which contains the header.
read rootprocid undef reading only the 1st column in rootprocid, rest goes to variable undef since it is not of interest.
callApi $rootprocid callAPI will be replaced with your actual api call.
I am able to connecting to remote oracle database using powershell and sqlplus. Also I am able to capture the output of the command/query executed in the database into a powershell variable.
PFB example.
$username ="hr"
$password ="hr"
$tnsalias ="orcl"
$outputfilepath="C:\Users\Desktop"
$sqlquery=#"
set serveroutput off
set feedback off
set heading off
set echo off
select sysdate from dual;
"#
Running the above query in oracle database as below
$xval=$sqlquery | sqlplus -silent $username/$password#$tnsalias
echo $xval
The output will be
23-APR-18
The problem is when I am running a PL SQL function as below.
$sqlquery=#"
set serveroutput off
set feedback off
set heading off
set echo off
declare
x varchar2:=null;
exec x:=MYTEST_FUN('a','b');
"#
There is no value captured in the $xval variable and also the function is not running.
How to execute the function such that the return value of the function is captured in a powershell variable.
One way is to make it set serveroutput on and use DBMS_OUTPUT.PUT_LINE() within BEGIN..END
Another option is to use the sqlplus' PRINT command.
set serveroutput off
set feedback off
set heading off
set echo off
variable x VARCHAR2 --declare bind variable x
exec :x := MYTEST_FUN('a','b'); --a colon before x needed.
PRINT x;
Worked for me:
$sqlQuery = #"
set serveroutput off
set feedback off
set heading off
set echo off
variable x NUMBER;
select count(*) into :x from TABLE;
PRINT x;
exit
"#
($sqlQuery | sqlplus -S user/pass | Write-Output | Out-String).Trim()
Late entry, most concisely:
$dateVar = ("set heading off
select sysdate from dual;" | sqlplus -s $yourConnectionString | Out-String).Trim()
im trying to save the output of a query in a variable. I followed some answers here in stackoverflow but no luck. Currently i'm using this
billerrors=$(sqlplus -s $username/$password#$SID << EOF
set pagesize 0 feedback off verify off heading off echo off;
SELECT ERROR from temp_table
exit;
EOF
)
echo $billerrors
The output i always get is
SQLPlus: Release 11.2.0.1.0 Production Copyright (c) 1982, 2009,
Oracle. All rights reserved. Use SQLPlus to execute SQL, PL/SQL and
SQL*Plus statements.
The output of the above query SELECT ERROR from temp_table will be in the below format
Bill payment errors in the last 30 minutes: XX : 70. YY : 20.
Incase of no bill errors, the output will be
Bill payment errors in the last 30 minutes: 0.
I would really appreciate if someone could help
You use the wrong names in the script of the variable username and password.
If you use the correct names in the script for the variables username and password, then your script must work.
Exapmple 1. Correct names in the script for the variables $username and $password.
#!/bin/sh
username=SCOTT
password=Tiger
## esmd is TNS alias in tnsnames.ora
SID=esmd
echo username: $username
echo password: $password
echo tns: $SID
billerrors=$(sqlplus -s $username/$password#$SID << EOF
set pagesize 0 feedback off verify off heading off echo off;
show user;
SELECT 'Date: '||to_char(sysdate,'DD-MM-YYYY HH24:MI')||' The test is passed' from dual;
exit;
EOF
)
echo $billerrors
oracle#esmd:~> ./test.sh
username: SCOTT
password: Tiger
tns: esmd
USER is "SCOTT" Date: 25-01-2018 08:32 The test is passed
Example 2.
I make an error in the name of the variable $UserName, when I call sqlplus
#!/bin/sh
username=SCOTT
password=Tiger
## esmd is TNS alias in tnsnames.ora
SID=esmd
echo username: $username
echo password: $password
echo UserName: $UserName
echo tns: $SID
billerrors=$(sqlplus -s $UserName/$password#$SID << EOF
set pagesize 0 feedback off verify off heading off echo off;
show user;
SELECT 'Date: '||to_char(sysdate,'DD-MM-YYYY HH24:MI')||' The test is passed' from dual;
exit;
EOF
)
echo $billerrors
oracle#esmd:~> ./test.sh
username: SCOTT
password: Tiger
UserName:
tns: esmd
SQL*Plus: Release 11.2.0.3.0 Production Copyright (c) 1982, 2011, Oracle. All rights reserved. Use SQL*Plus to execute SQL, PL/SQL and SQL*Plus
statements. Usage 1: sqlplus -H | -V -H Displays the SQL*Plus version and the
usage help. -V Displays the SQL*Plus version. Usage 2: sqlplus [ [<option>] [{logon | /nolog}] [<start>] ] <option> is: [-C <version>] [-L] [-M "<options>"]
[-R <level>] [-S] -C <version> Sets the compatibility of affected commands to the version specified by <version>. The version has the form "x.y[.z]".
For example, -C 10.2.0 -L Attempts to log on just once, instead of reprompting
on error. -M "<options>" Sets automatic HTML markup of output. The options have the form: HTML [ON|OFF] [HEAD text] [BODY text] [TABLE text] [ENTMAP {ON|OFF}]
[SPOOL {ON|OFF}] [PRE[FORMAT] {ON|OFF}] -R <level> Sets restricted mode to disable SQL*Plus commands that interact with the file system. The level can be
1, 2 or 3. The most restrictive is -R 3 which disables all user commands
interacting with the file system. -S Sets silent mode which suppresses the
display of the SQL*Plus banner, prompts, and echoing of commands. <logon> is:
{<username>[/<password>][#<connect_identifier>] | / } [AS {SYSDBA | SYSOPER | SYSASM}] [EDITION=value] Specifies the database account username, password and connect identifier for the database connection. Without a connect identifier, SQL*Plus connects to the default database. The AS SYSDBA, AS SYSOPER and AS SYSASM options are database administration privileges. <connect_identifier> can be in the form of Net Service Name or Easy Connect. #[<net_service_name> | [//]Host[:Port]/<service_name>] <net_service_name> is a simple name for a service that resolves to a connect descriptor. Example: Connect to database using Net Service Name and the database net service name is ORCL. sqlplus myusername/mypassword#ORCL Host specifies the host name or IP address of the database server computer. Port specifies the listening port on the database server. <service_name> specifies the service name of the database you want to access. Example: Connect to database using Easy Connect and the Service name is ORCL. sqlplus myusername/mypassword#Host/ORCL The /NOLOG option starts SQL*Plus without connecting to a database. The EDITION specifies the value for Session Edition. <start> is: #<URL>|<filename>[.<ext>] [<parameter> ...] Runs the specified SQL*Plus script from a web server (URL) or the local file system (filename.ext) with specified parameters that will be assigned to substitution
variables in the script. When SQL*Plus starts, and after CONNECT commands, the
site profile (e.g. $ORACLE_HOME/sqlplus/admin/glogin.sql) and the user profile
(e.g. login.sql in the working directory) are run. The files may contain
SQL*Plus commands. Refer to the SQL*Plus User's Guide and Reference for more
information.
Example 3.
I make an error in the name of the variable $Password, when I call sqlplus
#!/bin/sh
username=SCOTT
password=Tiger
## esmd is TNS alias in tnsnames.ora
SID=esmd
echo username: $username
echo password: $password
echo UserName: $UserName
echo Password: $Password
echo tns: $SID
billerrors=$(sqlplus -s $username/$Password#$SID << EOF
set pagesize 0 feedback off verify off heading off echo off;
show user;
SELECT 'Date: '||to_char(sysdate,'DD-MM-YYYY HH24:MI')||' The test is passed' from dual;
exit;
EOF
)
echo $billerrors
oracle#esmd:~> ./test.sh
username: SCOTT
password: Tiger
UserName:
Password:
tns: esmd
SQL*Plus: Release 11.2.0.3.0 Production Copyright (c) 1982, 2011,
Oracle. All rights reserved. Use SQL*Plus to execute SQL, PL/SQL and SQL*Plus
statements. Usage 1: sqlplus -H | -V -H Displays the SQL*Plus version and the
Example 4
I make an error in the name of the variable $Sid, when I call sqlplus
#!/bin/sh
username=SCOTT
password=Tiger
## esmd is TNS alias in tnsnames.ora
SID=esmd
echo username: $username
echo password: $password
echo SID: $SID
echo Sid: $Sid
billerrors=$(sqlplus -s $username/$password#$Sid << EOF
set pagesize 0 feedback off verify off heading off echo off;
show user;
SELECT 'Date: '||to_char(sysdate,'DD-MM-YYYY HH24:MI')||' The test is passed' from dual;
exit;
EOF
)
echo $billerrors
oracle#esmd:~> ./test.sh
username: SCOTT
password: Tiger
SID: esmd
Sid:
SQL*Plus: Release 11.2.0.3.0 Production Copyright (c) 1982, 2011, Oracle. All rights reserved. Use SQL*Plus to execute SQL, PL/SQL and SQL*Plus
Example 5
I make an error in the value of the variable $password=tiger, when I call the sqlplus program
#!/bin/sh
username=SCOTT
## True password is Tiger
password=tiger
## esmd is TNS alias in tnsnames.ora
SID=esmd
echo username: $username
echo password: $password
echo SID: $SID
billerrors=$(sqlplus -s $username/$password#$SID << EOF
set pagesize 0 feedback off verify off heading off echo off;
show user;
SELECT 'Date: '||to_char(sysdate,'DD-MM-YYYY HH24:MI')||' The test is passed' from dual;
exit;
EOF
)
echo $billerrors
oracle#esmd:~> ./test.sh
username: SCOTT
password: tiger
SID: esmd
ERROR: ORA-01017: invalid username/password; logon denied SP2-0306: Invalid
option. Usage: CONN[ECT] [{logon|/|proxy} [AS {SYSDBA|SYSOPER|SYSASM}]
[edition=value]] where <logon> ::= <username>[/<password>]
[#<connect_identifier>] <proxy> ::= <proxyuser>[<username>][/<password>]
[#<connect_identifier>] SP2-0306: Invalid option. Usage: CONN[ECT]
ORACLE after 3 attempts, exiting SQL*Plus
Example 6
I make an error in the value of the variable $SID=esm.
#!/bin/sh
username=SCOTT
password=tiger
## esmd is TNS alias in tnsnames.ora
## True is esmd
SID=esm
echo username: $username
echo password: $password
echo SID: $SID
billerrors=$(sqlplus -s $username/$password#$SID << EOF
set pagesize 0 feedback off verify off heading off echo off;
show user;
SELECT 'Date: '||to_char(sysdate,'DD-MM-YYYY HH24:MI')||' The test is passed' from dual;
exit;
EOF
)
echo $billerrors
oracle#esmd:~> ./test.sh
username: SCOTT
password: tiger
SID: esm
ERROR: ORA-12154: TNS:could not resolve the connect identifier specified
SP2-0306: Invalid option. Usage: CONN[ECT] [{logon|/|proxy} [AS
YSDBA|SYSOPER|SYSASM}] [edition=value]] where <logon> ::= <username>[<username>]
[/<password>][#<connect_identifi>] SP2-0157: unable to CONNECT to ORACLE after 3
attempts, exiting SQL*Plus
Example 7
If the Username is case sensitive.
#!/bin/sh
username=\"Scott\"
password=TigeR
## esmd is TNS alias in tnsnames.ora
SID=esmd
echo username: $username
echo password: $password
echo SID: $SID
billerrors=$(sqlplus -s $username/$password#$SID << EOF
set pagesize 0 feedback off verify off heading off echo off;
show user;
SELECT 'Date: '||to_char(sysdate,'DD-MM-YYYY HH24:MI')||' The test is passed' from dual;
exit;
EOF
)
echo $billerrors
oracle#esmd:~> ./test.sh
username: "Scott"
password: TigeR
SID: esmd
USER is "Scott" Date: 25-01-2018 09:20 The test is passed
oracle#esmd:~> ./test.sh
username: "Scott"
password: TigeR
SID: esmd
USER is "Scott" Date: 25-01-2018 09:23 The test is passed
I want to call a procedure in sqlplus oracle using shell script,
my procedure name is getdate_proc with two parameters, startdate and enddate.
I want to set startdate = sysdate and enddate = sysdate + 5 days
for example :execute getdate_proc(to_date('05/05/2015', 'MM-DD-YYYY'),to_date('05/09/2015','MM-DD-YYYY'))
below is my code:
#!/usr/bin/ksh
sqlplus -s /nolog << EOF
connect scott/tiger
--execute procedure with parameter
execute getdate_proc(to_date(sysdate, 'MM-DD-YYYY'),to_date(sysdate + 5days,'MM-DD-YYYY'))
--set spooling to save in csv
set underline off
SET RECSEP OFF
set verify off
set colsep ','
set linesize 300
set trimspool on
spool /home/user/project/samp.csv
select * from att2;
spool off
set verify off
i solved my problem with this code:
#!/usr/bin/ksh
date1='date'
date1=$(/bin/date --date="$date1" -d "+0 day" +"%F")
date2=$(/bin/date --date="$date1" -d "+4 day" +"%F")
echo $date1
echo $date2
sqlplus -s /nolog << EOF
connect scott/tiger
--execute procedure with parameter
execute attendance(to_date('$date1', 'YYYY-MM-DD'),to_date('$date2','YYYY-MM-DD'))
--set spooling to save in csv
set underline off
SET RECSEP OFF
set verify off
set colsep ','
set linesize 300
set trimspool on
spool /home/user/project/samp.csv
select * from att2;
spool off
set verify off
:) cheers