我想基於這個月如的某一天來執行SQL語句「如果今天是那麼25日執行SQL,否則說:‘今天是不是一個有效的rundate’。執行SQL僅在每月的某一天
基本上我用DBI
連接並建立一些HTML之前運行一些SQL被自動發送出去
但是,我得到的語法錯誤,如下列:在
語法錯誤XXXXX,靠近「,否則」 (可能是一個失控的多行{ }從XX行開始的字符串) 在行結束時在XXXXX處缺少右捲曲或方括號。
我讀過,你不能直接在表達式中嵌入指令(我相信我可能會這樣做)。該腳本工作正常,沒有day元素和else
聲明。
# Get the current day of the month (along with other date parts we can ignore)
my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime(time);
## Filter based on day of week
my $sql;
if ($mday == 25) {
$sql = qq{<MY SQL STATEMENT HERE>
}
else {
say "Today is not a valid rundate";
}
## Prepare and execute the SQL
my $sth = $dbh->prepare($sql) or die "Failed to prepare SQL: " . $dbh->errstr;
$sth->execute() or die "Failed to execute SQL: " . $dbh->errstr;
## Start building the HTML output
my $html = qq{
<table style="width:100%"><thead>
<tr>
<th>#</th>
<th>col1</th>
<th>col2</th>
<th>col3</th>
</tr>
</thead>
};
## Loop round each row in the result set, and add to the HTML output for each one.
my $count = 0;
while (my $row = $sth->fetchrow_hashref) {
#
# process rows
$count++;
#say Dumper $row;
#say "$row->{col1}, $row->{col2}, $row->{col3}";
$html .= qq|
<tr>
<td align="right">$count.</td>
<td>$row->{col1}</td>
<td>$row->{col2}<td>
<td>$row->{col3}</td>
</tr>
|;
}
## Close the HTML
$html .= "</table>";
say "$count rows processed.";
$html .= "<h2>Report SQL</h2>The report was produced with the following SQL<p><pre>$sql</pre>";
## Send the mail
my $sender = new Mail::Sender();
$sender->Open({
from => 'email1',
to => $maildest,
subject => $mailsubj,
ctype => "text/html",
encoding => "quoted-printable",
smtp => "localhost"
}) or die $Mail::Sender::Error, "\n";
$sender->SendEnc($html);
$sender->Close();
$dbh->disconnect;
這不是[mcve]所以這可能只是一個轉錄錯誤,但是您從不在'$ sql = qq {'語句中關閉'qq'。 –
ThisSuitIsBlackNot