我正在嘗試使用 FireDAC DBMS 識別符號來生成特定于資料庫的查詢。我目前正在連接到 MySQL 服務器(DriverID = MySQL)。我想用limit/查詢從 mysql 或 mssqltop查詢。我目前的陳述是這樣的:
SELECT
{IF MSSQL} TOP(1) {fi} `tr`.`TaxRate_Primkey`
FROM
`tbl_taxrates` AS `tr`
WHERE
`tr`.`TaxRate_TaxCodeId` = `tc`.`TaxCode_Primkey`
AND
`tr`.`TaxRate_ValidSince` <= :DATE
ORDER BY `tr`.`TaxRate_ValidSince` DESC
{IF MySQL} LIMIT 1 {fi}
當然我知道,轉義對于 mssql 是不正確的,但那是另一回事。當我檢查 FireDAC 監視器時,預處理的查詢如下所示:
SELECT
TOP(1) `tr`.`TaxRate_Primkey`
FROM
`tbl_taxrates` AS `tr`
WHERE
`tr`.`TaxRate_TaxCodeId` = `tc`.`TaxCode_Primkey`
AND
`tr`.`TaxRate_ValidSince` <= ?
ORDER BY
`tr`.`TaxRate_ValidSince` DESC
LIMIT 1
當然這會導致錯誤
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '.`TaxRate_Primkey` FROM `tbl_taxrates` AS `tr` WHERE `tr`.`TaxRate_TaxCodeId` ' at line 1 [errno=1064, sqlstate="42000"]
因為它應該只添加LIMIT 1和省略TOP(1).
因為我只有 Delphi Community Edition 10.4,所以我無法訪問 MSSQL-Driver。我想,這可能會導致這個錯誤,我和其他人一起嘗試過。但也與{if FIREBIRD}它{if ADS}是一樣的。
建構式如下所示:
constructor TFireDACTenantRepository.Create(
ADBName: string;
ADBServer: string;
APort: Integer;
AUserName: string;
APassword: string;
ALogger: TLogger
);
var
oParams: TStrings;
begin
inherited Create;
FLogger := ALogger;
Self.MonitorLink := nil;
Self.MonitorBy := mbRemote;
Self.Tracing := True;
FConnection := TFDConnection.Create(nil);
oParams := TStringList.Create;
try
oParams.Add('Server=' ADBServer);
oParams.Add('Port=' IntToStr(APort));
oParams.Add('Database=' ADBName);
oParams.Add('User_Name=' AUserName);
oParams.Add('Password=' APassword);
oParams.Add('OSAuthent=No');
FDManager.AddConnectionDef('MySQLConnectionTenant', 'MySQL', oParams);
FConnection.Params.MonitorBy := Self.MonitorBy;
FConnection.ConnectionDefName := 'MySQLConnectionTenant';
FConnection.ResourceOptions.ParamCreate := True;
FConnection.ResourceOptions.MacroCreate := True;
FConnection.ResourceOptions.ParamExpand := True;
FConnection.ResourceOptions.MacroExpand := True;
FConnection.ResourceOptions.PreprocessCmdText := True;
FConnection.ResourceOptions.EscapeExpand := True;
finally
oParams.Free;
end;
FConnection.AfterConnect := DoAfterConnect;
FConnection.AfterDisconnect := DoAfterDisconnect;
end;
我的問題在這里看起來與這個問題有點相關 如何使用 FireDAC DBMS 識別符號有條件地更改 SQL 文本
謝謝
uj5u.com熱心網友回復:
我找到了解決方案。如果您想使用條件轉義序列,您還必須為所有涉及的資料庫使用use該單元。FireDAC.Phys.XXX由于我想使用 MSSQL,所以我必須添加FireDAC.Phys.MSSQL-use子句。
此處列出了這些單位: https://docwiki.embarcadero.com/RADStudio/Sydney/en/Databases_(FireDAC)
也許這個答案對其他人有幫助。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/503903.html
上一篇:DLLC 匯入函式到Delphi
