我是Java开发人员。我在德尔菲有一些老程序。在旧版本中,它们使用mdb。我为与Server的连接修复了它。所有的SQL查询都是用TAdoQuery实现的。
qryTemp.SQL.Text:='select sum(iif(ComeSumm>0,comesumm,0)),sum(iif(lostSumm>0,lostsumm,0)) from cash '+
'where (IdCashClause is null or idcashclause<>8) '+
' and cashNum='+IntToStr(i)+
' and CashType=0'+
' and format(PayDate,"dd/mm/yyyy")=format('''+DateToStr(Date)+''',"dd/mm/yyyy") ';程序抛出一个异常:
无效列名'dd/mm/yyyy‘。
为了比较,我修正了其他查询:
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:=DateTimeToStr(Date);我能否快速修复所有用于使用Server的查询,而无需重写整个项目?
发布于 2013-11-08 09:35:16
假设PayDate在MSSQL中被定义为date/datetime,您可以使用如下参数:
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是在Server 2012中引入的。对于旧版本,请使用案例表达式。
在旧的非Unicode 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)
...发布于 2013-11-08 09:41:38
很可能PayDate列在cash表中被声明为DATE。考虑到这一点,您的参数应该是TDateTime而不是string,如下所示:
qryTemp.SQL.Text:=' select top 1 iif(ComeSumm>0,comesumm,0) from cash '
+' where idCashReason=:cashReason and idCashClause=8 and cashNum='+IntToStr(i)
+' and PayDate<:D'
+' order by payDate desc';
qryTemp.Parameters.ParamByName('D').Value := Date;我只替换了其中一个参数,但您应该考虑替换所有参数,因为这将通过启用其语句缓存来提高服务器性能。
回到您最初的问题,我想重构所有应用程序的唯一方法是有一个重构程序,它可以解析您的代码,找到这些情况,并遵循一种模式将一段代码替换为另一段代码。
我现在不知道有什么工具可以做到这一点。
也许使用支持正则表达式的查找/替换可以帮助您,但它肯定不会一次性修复这些情况。您必须运行一系列替换阶段,以便将代码从原来的代码转换为您想要的代码。
发布于 2013-11-08 07:42:58
DateToStr使用全局变量中包含的本地化信息来格式化日期字符串。也许这就是问题所在。
您可以尝试FormatDateTime:
qryTemp.SQL.Text:='select sum(iif(ComeSumm>0,comesumm,0)),sum(iif(lostSumm>0,lostsumm,0)) from cash '+
'where (IdCashClause is null or idcashclause<>8) '+
' and cashNum='+IntToStr(i)+
' and CashType=0'+
' and format(PayDate,"dd/MM/yyyy")='''+FormatDateTime('dd/mm/yyyy',Date)+'''';https://stackoverflow.com/questions/19852841
复制相似问题