Execute sqlplus sessions with jobs in powershell - oracle

I want to execute some sessions with various different scripts into a Oracle database. I want to do it with open jobs simultaneous but i think that i have problems with parameter pass. The $item_actual appears is null when start-job try to use.
$lista_scripts = #("insert","update","select")
For ($i=0; $i -le 2; $i++)
{
$item_actual=$lista_scripts[$i]
Write-Host "Ejecutandose $item_actual"
start-job {date;cat "$item_actual";cat $item_actual | sqlplus xxx/xxx#"(description=(address=(protocol=TCP)(host=xxx)(port=xxx))(connect_data=(SERVICE_NAME=xxx)))"; date;}
}
Thanks

$username = "scott"
$password = "tiger"
$connect_string ="test-ecdu"
$log_file = "c:\powershell_script\test\log.txt"
$lista_scripts = #("select 1 ,sysdate from dual ;","select 2,sysdate from dual;","select 3,sysdate from dual;")
For ($i=0; $i -le 2; $i++)
{
$item_actual=$lista_scripts[$i]
Write-Host "Ejecutandose $item_actual"
start-job -ArgumentList $username, $password,$connect_string,$item_actual, $log_file -ScriptBlock {param($username_, $password_,$connect_string_ , $item_actual_, $log_file_)
date >>$log_file_;
Write-output "RUN $item_actual_" >>$log_file_ ;
$sqlOutput= $item_actual_ | sqlplus -s $username_/$password_#$connect_string_;
Write-output $sqlOutput >>$log_file_
date >>$log_file_;
}
}
c:\powershell_script\test\log.txt
6 июля 2018 г. 13:20:45
RUN select 1 ,sysdate from dual ;
1 SYSDATE
---------- --------
1 06.07.18
Elapsed: 00:00:00.00
6 июля 2018 г. 13:20:45
6 июля 2018 г. 13:20:45
RUN select 2,sysdate from dual;
2 SYSDATE
---------- --------
2 06.07.18
Elapsed: 00:00:00.00
6 июля 2018 г. 13:20:45
6 июля 2018 г. 13:20:46
RUN select 3,sysdate from dual;
3 SYSDATE
---------- --------
3 06.07.18
Elapsed: 00:00:00.00
6 июля 2018 г. 13:20:46

Related

How to call the filename of running SQL script?

I'm trying to run a series of script files but I would like for each script file to print its filename, for reporting.
So, the intent is to call each SQL file from a general script:
#SQL_File_1;
#SQL_File_2;
#SQL_File_n;
But I need each SQL to print it's results, so I need each to print:
DBMS_OUTPUT.PUT_LINE({Filename} || ' updated ' || {Number of records});
How to retrieve the filename? Can it be easily done?
If you are running SQL*Plus, then it can be done as per the comment but only if you've set APPINFO, eg
SQL> select module from v$session where sid = sys_context('USERENV','SID');
MODULE
----------------------------------------------------------------
SQL*Plus
SQL> host cat x:\temp\myfile.sql
select module from v$session where sid = sys_context('USERENV','SID');
SQL> #x:\temp\myfile.sql
MODULE
----------------------------------------------------------------
SQL*Plus
SQL> SET APPINFO ON
SQL> #x:\temp\myfile.sql
MODULE
----------------------------------------------------------------
01# x:\temp\myfile.sql
That will also work with SQLcl.
I haven't used TOAD for a while but I think it also supports appinfo
The utility executes all sql files in the specified directory and complements the calls to sql files with prompt commands to display the sql file name.
For example input file
C:\upwork\powershell-oracle_git\sql
31.12.2020 11:42 28 SQL_FILE1.sql
31.12.2020 11:42 28 SQL_FILE2.sql
31.12.2020 11:43 28 SQL_FILE3.sql
31.12.2020 11:43 28 SQL_FILE4.sql
31.12.2020 11:43 28 SQL_FILE5.sql
For example output log
SET session NLS_LANG: AMERICAN_AMERICA.CL8MSWIN1251
===========================================================================================
Script start time : 2020-12-31 11:50:11
Found SQL file C:\upwork\powershell-oracle_git\sql\SQL_FILE1.sql
Found SQL file C:\upwork\powershell-oracle_git\sql\SQL_FILE2.sql
Found SQL file C:\upwork\powershell-oracle_git\sql\SQL_FILE3.sql
Found SQL file C:\upwork\powershell-oracle_git\sql\SQL_FILE4.sql
Found SQL file C:\upwork\powershell-oracle_git\sql\SQL_FILE5.sql
-------------------------------------------------------------------------------------------
For example output sqlplus short log, if set termout OFF
SQL*Plus: Release 11.2.0.4.0 Production on Thu Dec 31 11:48:58 2020
Copyright (c) 1982, 2013, Oracle. All rights reserved.
Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
SQL> SQL> SQL> SQL> SQL> SQL> SQL> SQL> SQL> SQL> SQL> SQL> SQL> SQL> SQL> SQL>
Session altered.
Elapsed: 00:00:00.00
SQL>
Session altered.
Elapsed: 00:00:00.00
SQL> SQL> SQL> Start script: #C:\upwork\powershell-oracle_git\sql\SQL_FILE1.sql
SQL> SQL> Stop script: #C:\upwork\powershell-oracle_git\sql\SQL_FILE1.sql
SQL> SQL> > --------------------------------------------------------------------------------------------------------
SQL> Start script: #C:\upwork\powershell-oracle_git\sql\SQL_FILE2.sql
SQL> SQL> Stop script: #C:\upwork\powershell-oracle_git\sql\SQL_FILE2.sql
SQL> SQL> > --------------------------------------------------------------------------------------------------------
SQL> Start script: #C:\upwork\powershell-oracle_git\sql\SQL_FILE3.sql
SQL> SQL> Stop script: #C:\upwork\powershell-oracle_git\sql\SQL_FILE3.sql
SQL> SQL> > --------------------------------------------------------------------------------------------------------
SQL> Start script: #C:\upwork\powershell-oracle_git\sql\SQL_FILE4.sql
SQL> SQL> Stop script: #C:\upwork\powershell-oracle_git\sql\SQL_FILE4.sql
SQL> SQL> > --------------------------------------------------------------------------------------------------------
SQL> Start script: #C:\upwork\powershell-oracle_git\sql\SQL_FILE5.sql
SQL> SQL> Stop script: #C:\upwork\powershell-oracle_git\sql\SQL_FILE5.sql
SQL> SQL> > --------------------------------------------------------------------------------------------------------
SQL> SQL> Disconnected from Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
For example sqlplus output full log, if set termout ON
SQL*Plus: Release 11.2.0.4.0 Production on Thu Dec 31 11:50:12 2020
Copyright (c) 1982, 2013, Oracle. All rights reserved.
Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
SQL> SQL> SQL> SQL> SQL> SQL> SQL> SQL> SQL> SQL> SQL> SQL> SQL> SQL> SQL> SQL>
Session altered.
Elapsed: 00:00:00.00
SQL>
Session altered.
Elapsed: 00:00:00.00
SQL> SQL> SQL> Start script: #C:\upwork\powershell-oracle_git\sql\SQL_FILE1.sql
SQL> SQL> select 1, sysdate from dual;
1 SYSDATE
---------- -------------------
1 31.12.2020 11:50:09
1 row selected.
Elapsed: 00:00:00.00
SQL> Stop script: #C:\upwork\powershell-oracle_git\sql\SQL_FILE1.sql
SQL> SQL> > --------------------------------------------------------------------------------------------------------
SQL> Start script: #C:\upwork\powershell-oracle_git\sql\SQL_FILE2.sql
SQL> SQL> select 2, sysdate from dual;
2 SYSDATE
---------- -------------------
2 31.12.2020 11:50:09
1 row selected.
Elapsed: 00:00:00.00
SQL> Stop script: #C:\upwork\powershell-oracle_git\sql\SQL_FILE2.sql
SQL> SQL> > --------------------------------------------------------------------------------------------------------
SQL> Start script: #C:\upwork\powershell-oracle_git\sql\SQL_FILE3.sql
SQL> SQL> select 3, sysdate from dual;
3 SYSDATE
---------- -------------------
3 31.12.2020 11:50:09
1 row selected.
Elapsed: 00:00:00.00
SQL> Stop script: #C:\upwork\powershell-oracle_git\sql\SQL_FILE3.sql
SQL> SQL> > --------------------------------------------------------------------------------------------------------
SQL> Start script: #C:\upwork\powershell-oracle_git\sql\SQL_FILE4.sql
SQL> SQL> select 4, sysdate from dual;
4 SYSDATE
---------- -------------------
4 31.12.2020 11:50:09
1 row selected.
Elapsed: 00:00:00.00
SQL> Stop script: #C:\upwork\powershell-oracle_git\sql\SQL_FILE4.sql
SQL> SQL> > --------------------------------------------------------------------------------------------------------
SQL> Start script: #C:\upwork\powershell-oracle_git\sql\SQL_FILE5.sql
SQL> SQL> select 5, sysdate from dual;
5 SYSDATE
---------- -------------------
5 31.12.2020 11:50:09
1 row selected.
Elapsed: 00:00:00.00
SQL> Stop script: #C:\upwork\powershell-oracle_git\sql\SQL_FILE5.sql
SQL> SQL> > --------------------------------------------------------------------------------------------------------
SQL> SQL> Disconnected from Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
This powershell script adds sqlplus command PROMPT sql file names to sql file before call sql script.
<# .SYNOPSIS
This script adds PROMPT sql file names to sql file before call sql script.
Author: Dmitry Demin dmitrydemin1973#gmail.com
.DESCRIPTION
This script adds PROMPT sql file names to sql file before call sql script.
.PARAMETER sql_file_input
Specify the input sql script.
.PARAMETER sql_file_output
Specify the output sql script.
.PARAMETER log_file
Specify the log file.
.EXAMPLE
This script adds PROMPT sql file names to sql file before call sql script.
.\add_prompt_file_names.ps1 -sql_file_input .\sql\start.sql -sql_file_output .\sql\start_prompt.sql -log_file log_file.log
#>
param(
[string]$sql_file_input="C:\upwork\powershell-oracle_git\sql\start.sql",
[string]$sql_file_output="C:\upwork\powershell-oracle_git\sql\start_prompt.sql",
[string]$log_file="log_add_prompt.log"
)
$upper_line = "PROMPT ""Start script: "
$bottom_line = "PROMPT ""---------------------------------------------------------------------------------------------------------"""
$date_time_start = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
Write-host "Script start time : $date_time_start "
try
{
echo "Script start time : $date_time_start ">>$log_file
}
catch {
Write-Host "Log File $log_file. Other type of error was found:"
Write-Host "Exception type is $($_.Exception.GetType().Name)"
exit
}
echo "===========================================================================================" | tee-object -Append -filepath $log_file
echo "Input sql file: $sql_file_input" | tee-object -Append -filepath $log_file
echo "Output sql file: $sql_file_output" | tee-object -Append -filepath $log_file
$data_file = Get-Content $sql_file_input
$null | Set-Content -Path $sql_file_output
foreach ($line_file in $data_file)
{
if ($line_file.TrimStart().StartsWith("#"))
{
$start_prompt_line= $upper_line + $line_file.TrimStart().replace("#","") + """"
Out-File -filepath $sql_file_output -append -inputobject $start_prompt_line -encoding default
Out-File -filepath $sql_file_output -append -inputobject $line_file.TrimStart() -encoding default
Out-File -filepath $sql_file_output -append -inputobject $bottom_line -encoding default
}
else
{
Out-File -filepath $sql_file_output -append -inputobject $line_file -encoding default
}
}
For example
C:\upwork\powershell-oracle_git>powershell .\add_prompt_file_names.ps1 -sql_file_input ./sql/start.sql -sql_file_output ./sql/start_output.sql
Script start time : 2021-01-03 16:32:41
============================================================================
Input sql file: ./sql/start.sql
Output sql file: ./sql/start_output.sql
Input file ./sql/start.sql
REM
REM
REM
---
rem start 1
#SQL_FILE1.sql
rem start 2
#SQL_FILE2.sql
rem start 3
#SQL_FILE3.sql
rem start 4
#SQL_FILE4.sql
rem start 5
#SQL_FILE5.sql
REM
REM
---
Output sql file: ./sql/start_output.sql
REM
REM
REM
---
rem start 1
PROMPT "Start script: SQL_FILE1.sql"
#SQL_FILE1.sql
PROMPT "---------------------------------------------------------------------------------------------------------"
rem start 2
PROMPT "Start script: SQL_FILE2.sql"
#SQL_FILE2.sql
PROMPT "---------------------------------------------------------------------------------------------------------"
rem start 3
PROMPT "Start script: SQL_FILE3.sql"
#SQL_FILE3.sql
PROMPT "---------------------------------------------------------------------------------------------------------"
rem start 4
PROMPT "Start script: SQL_FILE4.sql"
#SQL_FILE4.sql
PROMPT "---------------------------------------------------------------------------------------------------------"
rem start 5
PROMPT "Start script: SQL_FILE5.sql"
#SQL_FILE5.sql
PROMPT "---------------------------------------------------------------------------------------------------------"
REM
REM
---

Changing multiple actions chained into sqlplus to for loop in a bash script

I have this script which is obviously inefficient:
#!/bin/sh
for i in $(seq 1 10);
do
echo "CREATE TABLE testbenny_$i (id NUMBER NOT NULL);
! sleep 10
select * from testbenny_$i;
! sleep 10
select * from testbenny_$i;
! sleep 10
select * from testbenny_$i;
! sleep 10
select * from testbenny_$i;
! sleep 10
DROP TABLE testbenny_$i;" | sqlplus system/passwd &
done
I have one repetitive action I tried to enclose in a FOR loop but couldn't.
select * from testbenny_$i;
! sleep 10
I can't find the correct syntax to change it to a FOR. Working on Oracle 12.
Try the oracle sleep method as following:
#!/bin/sh
for i in $(seq 1 10);
do
echo "CREATE TABLE testbenny_$i (id NUMBER NOT NULL);
sys.DBMS_SESSION.sleep(10);
Begin
For var in 1..4 loop
select * from testbenny_$i;
sys.DBMS_SESSION.sleep(10)
End loop;
End;
/
DROP TABLE testbenny_$i;" | sqlplus system/passwd &
done
I have given just an example of how to use sys.DBMS_SESSION.sleep(10) method. Use it in your code according to your requirement.

BTEQ Teradata passing query variable to shell to script

I am new to Teradata.I am having a problem with my BTEQ program. The BTEQ job passes a variable shell script. The purpose of the program to check for any null values in the summary table that is 2 months delay. Here is an example of the table (the purpose of the program: when The Summary table is missing two months, for example, Oct and Nov then flag=Y. flag=Y means YES that there are null values in the Summary table for the month of Oct and Nov. flag=N means there are no null values):
dt_load maxmth summary
12-SEP Sep Sep
13 SEP Sep
29-SEP Sep
2-Oct Oct
3-Oct Oct
1-Nov Nov
2-Nov Nov
user="usr1"
pass="pass1"
SRC_DB="emp"
SOURCE_TABLE="summarytb"
FLAG =$(query_td)
td_query () { bteq << EOF |grep '^>' |sed -e "s/^>//"
${HOST}/${USER},${PASSWORD}
DATABASE $SRC_DB;
.set width 1000;
.set titledashes off;
SELECT COUNT(*)
FROM ${SRC_DB}.${SOURCE_TABLE};
( SELECT
dt_load,
summary ,
maxmth,
/*check for null 2 months back*/
MDIFF(summary, 2, maxmth, dt_load) AS diff1
FROM $SOURCE_TABLE
Group by dt_load) AS a
FROM
/*if summary field is null than insert 2*/
( SELECT *,
COALESCE(a.diff1,2) AS diff (Select FROM a) AS b
FROM
/* if diff= 2 than flag =Y else flag=N*/
( SELECT *,
CASE
When b.diff = 2 then ‘Y ‘ ELSE ‘N’
End ) AS flag (SELECT c.diff FROM b) c
FROM
/*pass flag variable to shell script*/
SELECT '>'||' c.flag';
$1
.LOGOFF;
.QUIT;
.EXIT
EOF
}
echo $FLAG
if [ $FLAG='Y' ] then echo Run file1;
exit 1
fi
IF [ $FLAG='N' ] then echo run file 2;
exit 1
fi

Crond execute shell many times

I have the following timey.cpp code in RedHat 2.6.32 x86_64:
using namespace std ;
int main()
{
while( 1 ){
char x[64]={0} ;
strcpy( x,"1234567890") ;
std::string s = x ;
std::cout << "(" << x << ")" << std::endl ;
struct timeval localtimex ;
long secs,usecs ;
gettimeofday(&localtimex,0x00) ;
secs = localtimex.tv_sec ;
usecs = localtimex.tv_usec ;
//long mills = (time.tv_sec * 1000) + (time.tv_usec / 1000 ) ;
printf("secs=(%d),usecs=(%d)\n",secs,usecs) ;
sleep( 1 ) ;
} //while
}
in /home/informix/test, compiled by g++ --std=c++0x timey.cpp -o timey.exe,
and the shell timey.sh:
source /etc/bashrc
nohup /home/informix/test/timey.exe &
Then I run timey.sh by:
/home/informix/test/timey.sh
and take a look if timey.exe runs by:
ps -ef | grep timey
It seems timey.exe runs as expected:
informix 41340 1 0 10:32 pts/10 00:00:00 /home/informix/test/timey.exe
What confuses me is that I add this shell to crontab:
38 10 * * 1-5 informix /home/informix/test/timey.sh
and restart crond:
/etc/init.d/crond restart
What surprises me is I see 4 copies of timey.exe running:
ps -ef | grep timey
informix 41498 1 0 10:38 ? 00:00:00 /home/informix/test/timey.exe
informix 41499 1 0 10:38 ? 00:00:00 /home/informix/test/timey.exe
informix 41529 1 0 10:38 ? 00:00:00 /home/informix/test/timey.exe
informix 41561 1 0 10:38 ? 00:00:00 /home/informix/test/timey.exe
What did I do wrong so that 4 copies of timey.exe are running?
I see the following in /var/log/cron:
Jul 2 10:38:01 localhost CROND[41440]: (informix) CMD (/home/informix/test/timey.sh )
Jul 2 10:38:01 localhost CROND[41439]: (informix) CMD (/home/informix/test/timey.sh )
Jul 2 10:38:01 localhost CROND[41491]: (informix) CMD (/home/informix/test/timey.sh )
Jul 2 10:38:01 localhost CROND[41533]: (informix) CMD (/home/informix/test/timey.sh )
It seems that crond really ran timey.sh 4 times, but why?
Also:
In RedHat 2.6.32-279.el6.x86_64, it works!
In RedHat 2.6.32-358.el6.x86_64, it does not work!

Iteration of batch script based on search string

I've a Program2.bat having Oracle query, which generates a file log.txt having content-structure either like:
SQL> SELECT SID, SERIAL#, STATUS, USERNAME, LOGON_TIME FROM GV$SESSION WHERE USERNAME = 'DBADMIN' and status in ('ACTIVE','INACTIVE','KILLED') ;
no rows selected
SQL> spool off;
OR like:
SQL> SELECT SID, SERIAL#, STATUS, USERNAME, LOGON_TIME FROM GV$SESSION WHERE USERNAME = 'DBADMIN' and status in ('ACTIVE','INACTIVE','KILLED') ;
SID SERIAL# STATUS USERNAME LOGON_TI
---------- ---------- ---------- ---------- -------------
2388 54 Active DBADMIN 28-FEB-14
2391 37 Active DBADMIN 17-FEB-14
2395 111 Inactive DBADMIN 28-FEB-14
2780 325 Killed DBADMIN 26-FEB-14
2790 111 Killed DBADMIN 8-FEB-14
5 rows selected.
SQL> spool off
I'm trying to achieve following things:
Action-1: Call Program2.bat, which will generate log.txt
Action-2: Search for the string "no rows selected" in log.txt
Action-3: If string found then call program1.bat
Action-4: If string not found then do below:
call program2.bat again and
Do Action-2 again at an interval of 5 mins (means keep searching for string "no rows selected" in log.txt on an interval of 5 mins untill string "no rows selected" got found ).
#echo off
setlocal
Call Program2.bat
>nul findstr /c:"no rows selected" log.txt && (
call program1.bat
) || (
call program2.bat
)
How Point-2 of Action-4 can be achieved in above script or do we have any other way to achieve all in the easiest way?

Resources