I have the following script segment in a Linux script:
sqlplus /
<<QUERY_1
UPDATE BATCH_FILE SET BATCH_ID = 0 WHERE BATCH_ID = -1;
COMMIT;
exit
QUERY_1
I am expecting the update to occur and the script to exit sqlplus
What actually happens is the query is not executed, and the script exits leaving sqlplus logged into my database with a SQL> prompt. I can execute the statements from the prompt, but of course, that is not what I want to do.
My current version of Oracle is 12.2.0.1
The output of the HERE-document is intended for the std input of sqlplus, but for the shell a command should be on a single line. Adding a backslash will make the shell ignore the line-end, combining the two physical lines into one logical line:
sqlplus / \
<<QUERY_1
UPDATE BATCH_FILE SET BATCH_ID = 0 WHERE BATCH_ID = -1;
COMMIT;
exit
QUERY_1
Or just:
sqlplus / <<QUERY_1
UPDATE BATCH_FILE SET BATCH_ID = 0 WHERE BATCH_ID = -1;
COMMIT;
exit
QUERY_1
I have to generate excel file from tables from Oracle Database. The code is working fine but however the column names are not coming completely, there are just coming as the length of there data length.
I want complete header/column names
The column names are coming like this
ST STAT_TYPE_DESC ST S NXT_STATME DELAY_DAYS ANN_PREM_LIM_LOW ANN_PREM_LIM_HI CONTRIB_HIST_LEN EVENT_DO C P
But I want is complete names of the columns, for example ST is STATEMENT_TYPE_ID
#!/bin/ksh
FILE="A.csv"
sqlplus -s lifelite/lifelite#dv10 <<EOF
SPOOL $FILE
SET HEADING ON
SET HEADSEP OFF
SET FEEDBACK OFF
SET LINESIZE 250
SET PAGESIZE 5000 embedded ON
SET COLSEP ","
SELECT * FROM TLLI_01150STATTYPE;
EOF
SPOOL OFF
EXIT 0
Before your select add a new one with the column names:
SELECT 'STATEMENT_TYPE_ID' || ',' || 'STAT_TYPE_DESC' || ',' || ... FROM dual;
And set heading off
My shell script executes a SQL to fetch the data in the following format:
JOB_ID_001|[PROD] This is a mail subject one ${application_date}|a#example.com,b#example.com
JOB_ID_002|[PROD] This is a mail subject two ${application_date}|c#example.com,b#example.com
I want to split this pipe-separated string, but the output looks very odd as follows:
JOB_ID_001[0]
JOB_ID_001[1]
JOB_ID_001[2]
This[0]
This[1]
This[2]
is[0]
is[1]
is[2]
a[0]
a[1]
a[2]
mail[0]
mail[1]
mail[2]
subject[0]
subject[1]
subject[2]
one[0]
one[1]
one[2]
${application_date}[0]
${application_date}[1]
${application_date}[2]
example.com,b#example.com[0]
example.com,b#example.com[1]
example.com,b#example.com[2]
JOB_ID_002[0]
JOB_ID_002[1]
JOB_ID_002[2]
This[0]
This[1]
This[2]
is[0]
is[1]
is[2]
a[0]
a[1]
a[2]
mail[0]
mail[1]
mail[2]
subject[0]
subject[1]
subject[2]
two[0]
two[1]
two[2]
${application_date}[0]
${application_date}[1]
${application_date}[2]
ple.com,b#example.com[0]
ple.com,b#example.com[1]
ple.com,b#example.com[2]
My desired output is:
JOB_ID_001
[PROD] This is a mail subject one ${application_date}
a#example.com,b#example.com
JOB_ID_002
[PROD] This is a mail subject two ${application_date}
c#example.com,b#example.com
So that I can continue with those strings.
My shell script is as follows:
email_configs=(`sqlplus -silent $DB_CONN <<-EOF
whenever sqlerror exit 1 oserror exit oscode
set heading off feedback off echo off verify off pagesize 0
$sql_subject_of_mail;
exit;
EOF`)
for i in "${!email_configs[#]}"
do
email_config=${email_configs[i]}
IFS='|' read -r -a email_config_array <<< "$email_config"
job_id=$email_config_array[0]
subject_of_mail=$email_config_array[1]
to_mail_id=$email_config_array[2]
echo $job_id
echo $subject_of_mail
echo $to_mail_id
done
I checked some alternate solutions from this page, but in the output ${application_date} part is missing or there is some other problem.
Can anyone have an idea about my mistake?
The $email_configs array is not being set correctly. It's using spaces as the array delimiters, not newlines.
Rather than set an array, read the output of sqlplus in a loop.
while IFS='|' read -r job_id subject_of_mail to_mail_id
do
echo "$job_id"
echo "$subject_of_mail"
echo "$to_mail_id"
done < <(sqlplus -silent $DB_CONN <<-EOF
whenever sqlerror exit 1 oserror exit oscode
set heading off feedback off echo off verify off pagesize 0
$sql_subject_of_mail;
exit;
EOF
)
I have this process to save a table to CSV file but is not perfect:
sqlplus / as sysdba
set heading off
SET TERMOUT OFF
spool MY_TABLE.csv
select column_id||','||column_name||','||data_type||','||nullable||','||data_length from user1.MY_TABLE;
spool off;
exit
Issue with that is that can not turn off printing to terminal (I have SET TERMOUT OFF but that does not suppress the output for some reason).
I would like to run a sqlplus with a query from a terminal (instead of opening sqlplus and running from there)..
How to do that?
Example 1 for linux create_csv.sh displays the table data on the monitor screen.
#!/bin/sh
sqlplus -s / as sysdba <<EOF
set heading off
set termout OFF
SET FEEDBACK OFF
SET TAB OFF
set pause off
set verify off
SET UNDERLINE OFF
set trimspool on
set timing off
set echo off
set linesize 1000
set pagesize 0
SET COLSEP ','
spool test2.csv
select code,name,code_rail from alexs.all_station;
spool off
exit;
EOF
Example 2 for linux createv2_csv.sh does not display anything.
#!/bin/sh
$ORACLE_HOME/bin/sqlplus -s / as sysdba #csv2.sql
csv2.sql removed spaces and tabs between fields in the output file test2.csv
set heading off
set termout OFF
SET FEEDBACK OFF
SET TAB OFF
set pause off
set verify off
SET UNDERLINE OFF
set trimspool on
set echo off
set linesize 1000
set pagesize 0
set wrap off
spool test2.csv
select code||','||name||','||code_rail from alexs.all_station;
spool off
exit;
Example for Windows test_csv.bat
sqlplus -s user/password#net_alias < csv.sql
csv.sql
set heading off
set termout OFF
SET FEEDBACK OFF
SET TAB OFF
set pause off
set verify off
SET UNDERLINE OFF
set trimspool on
set timing off
set echo off
set linesize 1000
set pagesize 0
SET COLSEP ','
spool test.csv
select code,name,code_rail from alexs.all_station;
spool off
exit;
Example for Windows powershell script create_csv.ps1
# login DBA user
$username_dba = "system"
# PASSWORD DBA user !!!!
$password_dba = "manager"
# Connect without alias
$tnsalias_db = "(DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = test.com)(PORT = 1521)))(CONNECT_DATA = (SID = test)))"
$sqlQuery =
#"
set heading off
set termout OFF
SET FEEDBACK OFF
SET TAB OFF
set pause off
set verify off
SET UNDERLINE OFF
set trimspool on
set timing off
set echo off
set linesize 1000
set pagesize 0
SET COLSEP ','
spool test.csv
select code,name,code_rail from alexs.all_station;
spool off
exit
"#
$sqlOutput = $sqlQuery | sqlplus -silent $username_dba/$password_dba#$tnsalias_db
Example output test.csv
966203,ГУРСКОЕ , 96
966307,УКТУР , 96
966400,КЕНАЙ , 96
966504,ВЫСОКОГОРНАЯ , 96
966608,КЕНАДА , 96
966805,ТУЛУЧИ , 96
966909,АКУР , 96
967009,ТУМНИН , 96
967102,ХУТУ , 96
967206,УСТЬ-ОРОЧИ , 96
967305,МОНГОХТО , 96
967403,ЛАНДЫШИ , 96
967526,ТОКИ , 96
967600,ВАНИНО , 96
967704,ВАНИНО-ПЕРЕВАЛКА , 96
967808,ВАНИНО-ЭКСПОРТ , 96
968001,ВИНЕВИТИНО , 96
Example output test2.csv
971907,ВЯЗЕМСКАЯ,96
972007,АВАН,96
972100,РОЗЕНГАРТОВКА,96
972204,БИКИН,96
972308,ЗВЕНЬЕВОЙ,96
972401,БУРЛИТ-ВОЛОЧАЕВСКИЙ,96
972505,ЛУЧЕГОРСК,96
972609,ЛАСТОЧКА,96
972702,ГУБЕРОВО,96
972721,ЭБЕРГАРД,96
972806,ДАЛЬНЕРЕЧЕНСК I,96
973207,ДАЛЬНЕРЕЧЕНСК II,96
973300,ЛАЗО,96
973404,ГРУШЕВОЕ,96
973508,ПРОХАСКО,96
973601,ФИЛАРЕТОВКА,96
973705,РУЖИНО,96
973809,ЛЕСОЗАВОДСК I,96
973813,1634 КМ,96
973902,ШМАКОВКА,96
974002,СУНГАЧ,96
974106,СВИЯГИНО,96
974229,ДРОЗДОВ,96
Below is the code to retrieve data from DB and output it in excel
sqlplus -s ${uidpwd} <<EOF
SET PAGESIZE 50000
SET FEEDBACK OFF
SET MARKUP HTML ON
SET NUM 24
SPOOL Extract.xls
whenever oserror exit 9
whenever sqlerror exit sql.sqlcode
#${SQL_SCRIPT}
SET SERVEROUTPUT ON
SET ECHO OFF
SET VERIFY OFF
SPOOL OFF
SET MARKUP HTML OFF
quit;
EOF
But what i see in excel report is all numbers in excel are with exponential like "5.30056E+15"
i tried below options, but none of them worked
SET NUMBERFORMAT '9999999999999999999999'
SET NUMWIDTH 20
in SQL i tried below
to_char(conf_num, '999999999999999999999999')
SELECT ' '||conf_num "Confirmation Number"
Also i have exec > ${LOGPATH}/log.``date +"%y%m%d"``.log 2>&1 in my script, Query out put is getting written in to this log file, how to avoid that? It should have only error logs.