在 MySQL DB 中運行的帶有 IF 的 SQL 命令顯示為語法錯誤。
if (exists(
SELECT * FROM tanss.leistungen
where
firmenid = Tanss
and date(FROM_UNIXTIME(datum)) >= DATE_ADD(DATE_ADD(MAKEDATE(YEAR(CURRENT_DATE), 1), INTERVAL MONTH(CURRENT_DATE)-6 MONTH), INTERVAL -0 DAY)
and date(FROM_UNIXTIME(datum)) < DATE_ADD(DATE_ADD(MAKEDATE(YEAR(CURRENT_DATE), 1), INTERVAL MONTH(CURRENT_DATE) MONTH), INTERVAL -0 DAY)
) =0 ,"ohne","vorhanden") as Leistung
或更簡單,例如
if(niederlassung.name is null,"Niederlassung Warendorf",niederlassung.name) as Niederlassung
cmd的錯誤輸出為:
Bareword在 C:\Entwicklung\LWEI_SampleCodes\sampleCode20.pl 第 123 行,靠近 ") =0 "ohne" (可能是從第 115 行開始的失控的多行 "" 字串)處找到操作員預期的位置(在 ohne之前缺少操作員? ) 字串在 C:\Entwicklung\LWEI_SampleCodes\sampleCode20.pl 第 123 行“ohne”附近的運算子預期位置找到,“ Bareword在 C:\Entwicklung\LWEI_SampleCodes\sampleCode20.pl 第 123 行“”附近找到運算子預期位置, “vorhanden”(在 vorhanden之前缺少運算子?) 在運算子預期的位置找到字串在 C:\Entwicklung\LWEI_SampleCodes\sampleCode20.pl 第 123 行,靠近“left join tanss.anfahrtpauschale_preise as anfahrtfirma on anfahrtfirma.linkID = vertrag.id 和 anfahrtfirma.zoneID = ""(上一行缺少分號?)
或者
Bareword在 C:\Entwicklung\LWEI_SampleCodes\sampleCode20.pl 第 115 行,“if(niederlassung.name is null,"Niederlassung”附近的操作員預期的位置找到了裸字(可能是從第 111 行開始的失控的多行“”字串)(做你需要預先宣告 if 嗎?)
我正在使用 DBD::ODBC
有什么方法可以讓 Perl 接受這樣的擴展 SQL 命令,而無需我在腳本中編輯它,就像我目前正在做的那樣?
更新:
- the SQL is a string and the full statement has quotation around it
- the SQL statement is 100% working
main part of code (but still not full code):
sub getLeadingMethode{
$SQL = "
SELECT
vertrag.ID as VertragsDBid,
vertrag.name as Vertragsart,
vertrag.datumvon as von,
vertrag.datumbis as bis,
firmen.displayID as Tanss,
firmen.name as Kundenname,
if (exists(
SELECT * FROM tanss.leistungen
where
firmenid = Tanss
and date(FROM_UNIXTIME(datum)) >= DATE_ADD(DATE_ADD(MAKEDATE(YEAR(CURRENT_DATE), 1), INTERVAL MONTH(CURRENT_DATE)-6 MONTH), INTERVAL -0 DAY)
and date(FROM_UNIXTIME(datum)) < DATE_ADD(DATE_ADD(MAKEDATE(YEAR(CURRENT_DATE), 1), INTERVAL MONTH(CURRENT_DATE) MONTH), INTERVAL -0 DAY)
) =0 ,"$4","$5") as Leistung
FROM tanss.vertrag
left join tanss.firmen on tanss.firmen.ID = vertrag.firmenID
left join tanss.anfahrtpauschale_preise as anfahrtfirma on anfahrtfirma.linkID = vertrag.id and anfahrtfirma.zoneID = "$6" and anfahrtfirma.linktypid = "$7"
left join (select * from tanss.firmen_fahrt as fahrt_1 where km_einfach =(select min(km_einfach) from tanss.firmen_fahrt where firmenID = fahrt_1.firmenid) ) as anfahrt on anfahrt.firmenID = firmen.id
left join tanss.firmen as niederlassung on niederlassung.id = anfahrt.mandantID
left join tanss.f_info_werte as vb on vb.firmenid = firmen.id and defID = "$8";";
Full error message:
C:\Entwicklung\LWEI_SampleCodes>sampleCode20.pl
Scalar found where operator expected at C:\Entwicklung\LWEI_SampleCodes\sampleCode20.pl line 115, near ") =0 ,"$4"
(Might be a runaway multi-line "" string starting on line 100)
(Missing operator before $4?)
String found where operator expected at C:\Entwicklung\LWEI_SampleCodes\sampleCode20.pl line 115, near "$4",""
(Missing operator before ","?)
Scalar found where operator expected at C:\Entwicklung\LWEI_SampleCodes\sampleCode20.pl line 115, near "","$5"
(Missing operator before $5?)
String found where operator expected at C:\Entwicklung\LWEI_SampleCodes\sampleCode20.pl line 115, near "left join tanss.anfahrtpauschale_preise as anfahrtfirma on anfahrtfirma.linkID = vertrag.id and anfahrtfirma.zoneID = ""
(Missing semicolon on previous line?)
Scalar found where operator expected at C:\Entwicklung\LWEI_SampleCodes\sampleCode20.pl line 119, near "left join tanss.anfahrtpauschale_preise as anfahrtfirma on anfahrtfirma.linkID = vertrag.id and anfahrtfirma.zoneID = "$6"
(Might be a runaway multi-line "" string starting on line 115)
(Do you need to predeclare left?)
String found where operator expected at C:\Entwicklung\LWEI_SampleCodes\sampleCode20.pl line 119, near "$6" and anfahrtfirma.linktypid = ""
(Missing operator before " and anfahrtfirma.linktypid = "?)
Scalar found where operator expected at C:\Entwicklung\LWEI_SampleCodes\sampleCode20.pl line 119, near "" and anfahrtfirma.linktypid = "$7"
(Missing operator before $7?)
String found where operator expected at C:\Entwicklung\LWEI_SampleCodes\sampleCode20.pl line 119, near "left join tanss.f_info_werte as vb on vb.firmenid = firmen.id and defID = ""
(Missing semicolon on previous line?)
Scalar found where operator expected at C:\Entwicklung\LWEI_SampleCodes\sampleCode20.pl line 122, near "left join tanss.f_info_werte as vb on vb.firmenid = firmen.id and defID = "$8"
(Might be a runaway multi-line "" string starting on line 119)
(Do you need to predeclare left?)
String found where operator expected at C:\Entwicklung\LWEI_SampleCodes\sampleCode20.pl line 122, near """
(Missing semicolon on previous line?)
syntax error at C:\Entwicklung\LWEI_SampleCodes\sampleCode20.pl line 115, near ") =0 ,"$4"
Execution of C:\Entwicklung\LWEI_SampleCodes\sampleCode20.pl aborted due to compilation errors.
uj5u.com熱心網友回復:
$SQL = "
.......
) =0 ,"$4","$5") as Leistung
雙引號字串中有未轉義的雙引號。這會破壞你的報價。您需要轉義引號,例如:
$SQL = "
.......
) =0 ,\"$4\",\"$5\") as Leistung
或者使用qq/q ...
$SQL = qq#
.......
) =0 ,"$4","$5") as Leistung
#
或使用heredoc ....
$SQL = <<"END_SQL"
.......
) =0 ,"$4","$5") as Leistung
END_SQL
或者,如果您使用DBI, 而不是在字串中插入變數,則應使用占位符
$SQL = "
.......
) =0 , ? , ?) as Leistung
稍后在您的執行陳述句中提供變數。
$dbi->prepare($SQL); # sample code, not for use
$dbi->execute($4, $5);
此外, variables$4等$5是正則運算式匹配捕獲的內置變數。使用它們與正則運算式捕獲分離可能很危險。我假設你正在做類似的事情。
if ($foo =~ /(...)(...)..../) { # capturing $1 .... $7 etc
getLeadingMethode(); # relying on global scope variables
但最好將值與子呼叫一起傳遞,如下所示:
if ($foo =~ /(...)(...)..../) { # capturing $1 .... $7 etc
getLeadingMethode($4, $5, $7); # passing values directly, encapsulated approach
就像我上面所說的,對于 DBI,您希望使用占位符并讓模塊處理參考。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/425556.html
下一篇:狗狗幣地址生成-地址無效
