我在Ostendo(ERP)中有一个脚本,它在firebird数据库上运行。 该脚本是用Pascal编写的。它返回错误" Firebird SQL:错误代码-104,令牌未知,' 13/04/2018'在TMPQuery2.ExecQuery行。我删除了参考文本DATEWORKED = {ds'' 13/04 2018'}并且它工作正常。我已经注释掉了我尝试解决它的所有其他方面 - 其中没有一条有效。
关于我哪里出错的任何想法?我觉得它与日期格式有关。我试过dd-mm-yyyy,yyyy-mm-dd(这是从OBDC到excel的工作原理),dd / mm / yyyy(这就是它在ostendo中的显示方式)。
function DuplicateTimesheetDays(ExcelFileName: String; RowCount: Integer; EmployeeName: String;): Boolean;
var
TSDateWorked, SQLStr: String;
TSBatchNo: String;
TMPQuery2 : TpFIBQuery;
ErrorMessage : String;
DuplicateDay : String;
DisplayErrorMessage : Boolean;
x: Integer;
begin
DisplayErrorMessage := False;
ErrorMessage := 'The following Days have previously been added: ' + #13#10;
LoadSpreadSheet(ExcelFileName);
for x := 1 to RowCount -1 do
begin
//
//TSDateWorked := SSGetCellText(0,x); //convert to YYYY-MM-DD
TSDateWorked := FormatDateTime('YYYY-MM-DD',strtodate(SSGetCellText(0,x)));
//SQLStr := 'SELECT TIMESHEETBATCHNO FROM TIMESHEETLINES WHERE DATEWORKED = {d ''' + TSDateWorked + '''} AND EMPLOYEENAME = ''' + EmployeeName + '''';
//SQLStr := 'SELECT TIMESHEETBATCHNO FROM TIMESHEETLINES WHERE DATEWORKED = {ds ''2018-04-13''} AND EMPLOYEENAME = ''DE BEER''';
SQLStr := 'SELECT TIMESHEETBATCHNO FROM TIMESHEETLINES WHERE DATEWORKED = {ds ''13/04/2018''} AND EMPLOYEENAME = ''DE BEER''';
//SQLStr := 'SELECT TIMESHEETBATCHNO FROM TIMESHEETLINES WHERE EMPLOYEENAME = ''DE BEER''';
Showmessage(SQLStr);
try
TMPQuery2 := TpFIBQuery.Create(nil);
TMPQuery2.Database := OstendoDB;
TMPQuery2.Options := qoStartTransaction + qoAutoCommit;
TMPQuery2.SQL.clear;
//TMPQuery2.SQL.Add('SELECT TIMESHEETBATCHNO FROM TIMESHEETLINES WHERE DATEWORKED = {d ''' + TSDateWorked + '''} AND EMPLOYEENAME = ''' + EmployeeName + '''');
TMPQuery2.SQL.Add(SQLStr);
Showmessage('About to execute query');
TMPQuery2.ExecQuery;
if not TMPQuery2.EOF then
begin
TSBatchNo := TMPQuery2.FN('TIMESHEETBATCHNO').value;
//Showmessage('Position of Batch Number in error message is: ' + pos(TSBatchNo, ErrorMessage));
// If pos(TSBatchNo, ErrorMessage) = 0 then
ErrorMessage := ErrorMessage + 'Duplicate Timesheet already entered on Batch No ' + TSBatchNo + ' for day ' + TSDateWorked + '.' + #13#10;
DisplayErrorMessage := True;
Result := True
end;
finally
TMPQuery2.close;
end;
end; // For loop
//Display error messages if duplicates exist.
If DisplayErrorMessage then MessageDlg(ErrorMessage,mtinformation,mbok,0);
end;
答案 0 :(得分:3)
从哪里可以看出Firebird中的日期文字以{ds '2018-04-13'}
的格式表示?将字符串数据类型转换为DATE的合法格式列在" Literal Formats" " Conversion of Data Types"中的部分章。
基本上yyyy-mm-dd
格式是正确的,但是当连接pascal中的字符串时,它必须是一个简单的字符串,即DATEWORKED = ''' + TSDateWorked + '''
。
但不是将查询构建为字符串,而应使用参数化查询。 SQL字符串中的参数通常由name表示,前面有一个冒号,即:DateWorked
。所以你的查询看起来像
SQLStr := 'SELECT TIMESHEETBATCHNO FROM TIMESHEETLINES WHERE DATEWORKED = :DateWorked AND EMPLOYEENAME = :employee';
...
TMPQuery2.Params[0].AsDateTime := strtodate(SSGetCellText(0,x));
TMPQuery2.Params[1].AsString := 'DE BEER';
TMPQuery2.ExecQuery;
查询组件通常还具有ParamByName
方法,因此您可以使用名称来指定值,而不是参数位置。
我不知道TpFIBQuery
组件,因此属性/方法名称可能不同,请参阅组件的帮助。