가정합니다 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
매개 변수 : 즉 :
...
+' where idCashReason=1 and idCashClause=8 and cashNum=:cashNum'+
...
qryTemp.Parameters.ParamByName('cashNum').Value := i;
문자열을 형식화하고 사용하지 않고 매개 변수와 함께 호환되는 데이터 유형을 항상 선호합니다. SQL은 데이터 유형을 명시 적으로 정의 할 수 있다면 데이터 유형을 추측 할 필요가 없습니다.
메모: IIF
SQL Server 2012에서 소개되었습니다. 이전 버전 사용 사례 표현.
구형 비 유니 코드 델파이 버전에서는 매개 변수가 유니 코드에 문제가 있습니다.
그래서, 당신이 ~하지 않다 다음을 사용할 수있는 매개 변수를 사용하십시오.
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)
...