Sqlplus script part showing "Badly placed ()'s." error - shell

I'm writing a script and have a problem when trying to get a date from the table. I'm having problem storing it into a variable. I always get the error:
Badly placed ()'s.
The variable TIMEFRAME is "D" and the shell actually gets into the IF, also the password is also not the problem as I use it to connect manually and check the query (no problems here).
This is the part of the script where I'm having problems.
#!/bin/csh -f
if ( $TIMEFRAME == "D" ) then
set TIMEBEG = `sqlplus -s $PASSWORD << EOF \
set head off; \
set feed off; \
select to_char(trunc(sysdate) - 1,'YYYYMMDD') from dual; \
exit; \
EOF`
set TIMEEND = $TIMEBEG"235959"
set TIMEBEG = $TIMEBEG"000000"
endif
I also tried to store it on a file, same problem
#!/bin/csh -f
set tmp_file=/tmp/tmp.$$
if ( $TIMEFRAME == "D" ) then
sqlplus -s $PASSWORD > $tmp_file << EOF \
set head off \
set feed off \
select to_char(trunc(sysdate) - 1,'YYYYMMDD') from dual; \
exit; \
EOF
set TIMEBEG=`cat $tmp_file`
set TIMEEND = $TIMEBEG"235959"
set TIMEBEG = $TIMEBEG"000000"
endif
I tried different solutions but with the same problem, tried without semi-colons, without the SETs, to put it on a single line, I'm out of Ideas.
When trying with -xvf this is what I get when I get into the part:
if ( $TIMEFRAME == "D" ) then
if ( D == D ) then
sqlplus -s $PASSWORD > $tmp_file << EOF set head off set feed off select to_char ( trunc ( sysdate ) - 1,'YYYYMMDD' ) from dual ; exit ; EOF
Badly placed ()'s.
Any ideas? This is running on Amazon Linux

I'm not sure you can use a here-doc inside backticks in C shell.
Write the sqlplus input to a file, rather than writing the whole sqlplus command.
#!/bin/csh -f
set tmp_file=/tmp/tmp.$$
if ( $TIMEFRAME == "D" ) then
cat > $tmp_file <<EOF
set head off
set feed off
select to_char(trunc(sysdate) - 1, 'YYYYMMDD') from dual;
exit;
EOF
set TIMEBEG = `sqlplus -s $PASSWORD < $tmp_file`
set TIMEEND = $TIMEBEG"235959"
set TIMEBEG = $TIMEBEG"000000"
endif

Related

batch file to store the psql query output into a variable

Hi I am creating a batch file to execute a query and write a log file for the output. So based on some condition the there two queries to be executed.
So I was trying to store a output value of a query in to a variable and use that variable value to check the condition.
But here I am facing a problem that how should I suppose to declare and store output value into a variable using psql command.
Please help!!
Here what I tried:
SET /A a = psql -t -U postgres -d rpd -c "select count(*) from rpd.rpm_rpd_count"
SET /A b = 1
if %a% == 3 goto is_stat
else goto is_start
REM to copy the log
:is_start
psql -U postgres -d rpd -c "SELECT
a.table_name , 'MATCH' status FROM rpd.rpm_rpd_count A WHERE
a.rpd_count = a.rpm_count UNION ALL SELECT a.table_name, 'NOT MATCH'
AS status FROM rpd.rpm_rpd_count A WHERE a.rpd_count <> a.rpm_count;" >> C:\Users\admin\Desktop\err.log
goto END
:is_stat
psql -U postgres -d rpd -c "SELECT a.table_name , 'MATCH'
status FROM rpd.rpm_rpd_count A WHERE a.rpd_count = a.rpm_count;" >>
C:\Users\admin\Desktop\err.log
goto END
:END
echo %b% >> C:\Users\admin\Desktop\err.log
Problem here is that nothing is getting saved in the variable a
You can use a for loop to parse the output
for /f %%i in ('psql -t -U postgres -d rpd -c "select count(*) from rpd.rpm_rpd_count"') do set A=%%i
(use a single % if you try it in the command line console, and double %% in a batch file)

Script runs in standalone but does not work in crontab

Here is the script which works well when executed in standalone but fails when scheduled as a cronjob,
#!/bin/bash
# TARGET TABLE COUNT FOR Server
#
CURRENTTARGETREF=$(sqlplus -s $DB_USER/$DB_PASS << END
set pagesize 0 feedback off verify off heading off echo off;
SELECT CURRENTTARGETREF FROM PARAMETER
exit;
END
)
TABLENAME="TARGET$CURRENTTARGETREF"
TARGETCOUNT=$(sqlplus -s $DB_USER/$DB_PASS << END
set pagesize 0 feedback off verify off heading off echo off;
SELECT COUNT(*) FROM $TABLENAME
exit;
END
)
echo "Current Target table count in server: $TARGETCOUNT"
#
# COUNTER TABLE COUNT FOR server
#
S1=`echo "SELECT COUNT(*) FROM COUNTER WHERE SERVERID=1;" | sqlplus $DB_USER/$DB_PASS | sed -n '/COUNT(\*)/{n;n;p}'`
S2=`echo "SELECT COUNT(*) FROM COUNTER WHERE SERVERID=2;" | sqlplus $DB_USER/$DB_PASS | sed -n '/COUNT(\*)/{n;n;p}'`
#
echo "Current Counter table count with SERVERID '1' in server:$S1"
echo "Current Counter table count with SERVERID '2' in server:$S2"
#
below is the cronjob for this script
31 01 * * * DS=$(date +\%Y-\%m-\%d); /path/Databasecount.sh >> /path/test.out.$DS.txt 2>&1
And i wanted to send the output of the script to mail and could you someone please help me on this.

How to make read -d return true

I am using the following construct to set a multiline string in bash shell. But this always returns false which does not work when I set set -e. How can I make this to return success?
#!/bin/bash
set -x
set -e
read -d '' QUERY <<EOF
UPDATE table_name SET
field1 = 'value',
field2 = 'value'
WHERE id = 1;
EOF
mysql table_name -e "$QUERY"
While not an answer to your original question, this does get your problem solved. Consider using substitution and e.g. cat
QUERY=$(cat <<EOM
test
test2
EOM
)
mysql table_name -e "$QUERY"
Please, be aware of safety issues if you're reading those values from unsanitized input.

pass a variable into a string ksh

how do you pass a global variable into a string inside a function?
I have the following code that works for the most part:
td_query () { bteq << EOF |grep '^>' |sed -e "s/^>/;/g"
$(cat $HOME/.tdlogon)
DATABASE $schemaName;
.set width 10000;
.set titledashes off;
$1
.LOGOFF;
.QUIT;
.EXIT
EOF
}
rqstID="1357"
echo $(td_query "select '>'||'UPDATE schema.SEGN_$rqstID_PRCSS_TBL SET POPN_LVL_EXCLN ='||a.CODE_ID||' WHERE ' || b.SQL_FILE_NM ||' AND POPN_LVL_EXCLN IS NULL'
FROM SE_POPN_EXCLSN a
INNER JOIN SE_CODE_LIB b
ON
a.CODE_ID = b.CODE_ID;")
but the results come back:
UPDATE schema.SEGN_ SET POPN_LVL_EXCLN = 1002 WHERE MR_IND = 'Y'
missing this:
$rqstID_PRCSS_TBL
it should be:
UPDATE schema.SEGN_1357_PRCSS_TBL SET POPN_LVL_EXCLN = 1002 WHERE MR_IND = 'Y'
_ is a legal character in a shell variable. The shell is trying to find a variable by the name of $rqstID_PRCSS_TBL and getting an empty string. (That's why _PRCSS_TBL is disappearing from your output.)
You need to tell the shell where the variable name ends: schema.SEGN_${rqstID}_PRCSS_TBL

Check if value within column exists, if not create the column

I need to check if one of the columns in my db contains specific value. If it doesn't I want to create that row with folowing values:
#!/bin/bash
#
MODEL=$1
if true (SELECT * FROM table.STATISTICS
WHERE MODEL = '$MODEL' )
do this (INSERT INTO table.STATISTICS('$MODEL',0,SYSDATE,0,SYSDATE,0); )
You could use a merge for this, run through SQL*Plus as a 'heredoc', so you don't have to do a separate count operation; the merge will do that for you effectively:
#!/bin/bash
MODEL=$1
sqlplus -s /nolog <<!EOF
connect user/pass
merge into statistics s
using (select '${MODEL}' as model, 0 as num1, sysdate as date1,
0 as num2, sysdate as date2 from dual) t
on (s.model = t.model)
when not matched then
insert (s.model, s.num1, s.date1, s.num2, s.date2)
values (t.model, t.num1, t.date1, t.num2, t.date2);
!EOF
But using your real column names, obviously. It's better to list them explicitly even for a plain insert.
get_count () {
sqlplus -s username/pass <<!
set heading off
set feedback off
set pages 0
select count(model) from statistics
where model='$MODEL';
!
}
count=$(get_count $1)
if [ "${count:-0}" -eq 0 ]; then
echo "its zero"
sqlplus -S username/pass << EOF
whenever sqlerror exit 1;
set echo on
set verify off
INSERT INTO table.STATISTICS VALUES('$MODEL',0,SYSDATE,0,SYSDATE,0);
exit;
EOF
fi

Resources