仮定します PayDate
と定義されている date
/datetime
MSSQLでは、次のようにパラメーターを使用できます。
qryTemp.SQL.Text:=' select top 1 iif(ComeSumm>0,comesumm,0) from cash '
+' where idCashReason=1 and idCashClause=8 and cashNum='+IntToStr(i)
+' and PayDate<:D'
+' order by payDate desc';
qryTemp.Parameters.ParamByName('D').Value := Date;
qryTemp.Parameters.ParamByName('D').DataType := ftDateTime;
私も変わります cashNum
パラメーターへのIEへ:
...
+' where idCashReason=1 and idCashClause=8 and cashNum=:cashNum'+
...
qryTemp.Parameters.ParamByName('cashNum').Value := i;
文字列をフォーマットして使用するよりも、常にパラメーターを使用して互換性のあるデータ型を使用することを好みます。 SQLは、データ型を明示的に定義できれば推測する必要はありません。
ノート: IIF
SQL Server 2012で導入されました。 場合 表現。
古い非ユニコードDelphiバージョンでは、パラメーターがUnicodeに問題があります。
だから、もしあなたの場合 しないでください パラメーターを使用すると、次のものを使用できます。
function DateTimeToSqlDateTime(const DT: TDateTime): WideString;
begin
Result := FormatDateTime('yyyy-MM-dd', DT) + ' ' + FormatDateTime('hh:mm:ss', DT);
end;
function SqlDateTimeStr(const DT: TDateTime; const Is_MSSQL: Boolean): WideString;
var
S: WideString;
begin
S := DateTimeToSqlDateTime(DT);
if Is_MSSQL then
Result := Format('CONVERT(DATETIME, ''%s'', 102)', [S])
else
Result := Format('#%s#', [S]); // MS-ACCESS
end;
そして、あなたのクエリは次のように見えます:
...
+' and PayDate<' + SqlDateTimeStr(Date, True)
...