如何使用两个表运行BTEQ脚本,每个表应在单独的查询上加载

时间:2019-07-09 03:38:49

标签: bash unix teradata

我正在尝试创建一个 BTEQ脚本,该脚本应该以Table格式(如TableName,Curent_Date_Loaded和已加载的记录数)发送邮件。

我有两个表,我在寻找一个结果,例如在执行第一个BTEQ查询时不检索任何值(行),它应该运行到下一个查询,但主要是我只想要不检索任何值的表应在第二个查询上运行。 如果该表即使在第二次查询中也未检索到任何值(行),则必须使用“脚本发送邮件”来引发诸如“ 今天未加载表”的消息

tablecollist.txt 包含

ABC |加载日期

DEF | Load_Date

在我的情况下,两个表都在两个查询上加载。在哪里需要修复。

#! /bin/sh
set -vx

optfile="/home/scripts/opt.txt"
[[ -f $optfile ]] &&  rm $optfile

dbcred="******,******"

touch $optfile

for i in `cat tablecollist.txt`
do
tbl=`echo $i|awk -F '|' '{print $1}'`
col=`echo $i|awk -F '|' '{print $2}'`
done

/usr/bin/bteq << EOF

.LOGON abc${hostsuffix}.xyz.yzx.com/$dbcred;

.SET SEPARATOR '|'
.SET TITLEDASHES OFF;
.SET NULL AS ''

.EXPORT REPORT FILE=$optfile
.SET QUIET ON;

DATABASE par_yzx_db;

sel '$tbl' as tablename, cast($col as date) as Load_Date,count(*)
from $tbl where $col=cast(current_date as date format 'dd-mm-yyyy')
group by cast ($col as date);

.IF ACTIVITYCOUNT =0 THEN .GOTO DoQuery2;

.LABEL DoQuery2;

sel '$tbl' as tablename, cast($col as date) as load_date, count(*) from
$tbl where cast($col as date) = current_date-1 group by load_date;

GOTO End;
.LABEL End;

IF ERRORCODE > 0 THEN .QUIT 10;
.LOGOFF;
.EXIT
EOF
done

cdate=`date +'%F'`
sed -i -e '/tablename/d' $optfile


if [[ -s $optfile ]] ; then
SUBJECT=`echo "Important Tables yzx status: $cdate"`
/usr/bin/awk  -F"|" -v SUBJECT="Subject: ${SUBJECT}" 'BEGIN {
 print "From: abc@xyz.com"
 print "To: 123@xyz.com"
 print "MIME-Version: 1.0"
 print "Content-Type: text/html"
 print SUBJECT
 print "<html><body><table border=1 cellspacing=0 cellpadding=3 style=font-family:Calibri;>"
 print "<tr>"
 print "<td>Table_Name</td>";
 print "<td>Load Date</td>";
 print "<td>Count</td>";
 print "</tr>"
} {
 print "<tr>"
 print "<td>"$1"</td>";
 print "<td>"$2"</td>";
 print "<td>"$3"</td>";
 print "</tr>"
  } END {
 print " </table></body></html>"
} ' $optfile | /usr/sbin/sendmail -t
fi

预期结果:

**Table_Name  Load Date Count**    
ABC           19/07/08  46    
DEF           19/07/06  85

实际结果:

**Table_Name  Load Date Count**
ABC           19/07/08  46
ABC           19/07/08  46
DEF           19/07/06  85

1 个答案:

答案 0 :(得分:1)

您的IF逻辑错误,无论ACTIVITY_COUNT是多少,您都将执行第二个查询。

sel '$tbl' as tablename, cast($col as date) as Load_Date,count(*)
from $tbl where $col=cast(current_date as date format 'dd-mm-yyyy')
group by cast ($col as date);

.IF ACTIVITYCOUNT > 0 THEN .GOTO END;

sel '$tbl' as tablename, cast($col as date) as load_date, count(*) from
$tbl where cast($col as date) = current_date-1 group by load_date;

.LABEL End;