比较简单的一条嵌入查询,在查询分析器里能查出正常记录,而在delphi却不能,
详情如下:
1、查询分析器里:
select * from gps_carinfor where gps_carcorp=(select id from gps_carteam where gps_carteamname='一车队');
查询结果正常
2、delphi里
carteam :='一车队';
sql.Add('select * from gps_carinfor where gps_carcorp=(select id from gps_carteam where gps_carteamname=:tname)');
parameters.ParamByName('tname').Value := carteam;
查不出记录来而把sql语句改为
sql.Add('select id from gps_carteam where gps_carteamname=:tname');
或'select * from gps_carinfor where gps_carcorp=1'
都能查出正常的结果。什么原因,期待解答
详情如下:
1、查询分析器里:
select * from gps_carinfor where gps_carcorp=(select id from gps_carteam where gps_carteamname='一车队');
查询结果正常
2、delphi里
carteam :='一车队';
sql.Add('select * from gps_carinfor where gps_carcorp=(select id from gps_carteam where gps_carteamname=:tname)');
parameters.ParamByName('tname').Value := carteam;
查不出记录来而把sql语句改为
sql.Add('select id from gps_carteam where gps_carteamname=:tname');
或'select * from gps_carinfor where gps_carcorp=1'
都能查出正常的结果。什么原因,期待解答
carteam :='一车队';
sql.Add('select * from gps_carinfor where gps_carcorp=(select id from gps_carteam where gps_carteamname='''+carteam+''')');
这样也正常
而在sql语句里要查询出结果,carteam的值应为 '一车队'
多加几个引号吧
因为
sql.Add('select id from gps_carteam where gps_carteamname=:tname');
parameters.ParamByName('tname').Value := carteam;结果是正常的
sql.add('select * from table where id='''+edit1.text+'''');
你的問題:sql.add('select *from table where .....''1車隊''');就可以了
那不是字符串的参数,都不能这样传了?为什么把sql语句分开写就没问题?
多加引号也能解决问题
//sql.Add('select * from gps_carinfor where gps_carcorp=(select id from gps_carteam where gps_carteamname=:tname)');//结果不正常
sql.Add('select * from gps_carteam where gps_carteamname=:tname');//结果正常
parameters.ParamByName('tname').Value := carteam;
carteam初始赋值为 '一车队'经过赋值操作
parameters.ParamByName('tname').Value := carteam;后传入sql语句中的不是'一车队'而是别的,可能被截断了。怎么看赋完值要执行的sql语句的字符串?
跟踪结果:select * from gps_carinfor where gps_carcorp=(select id from gps_carteam where gps_carteamname='一'果然是传参数时的bug那就只能用别的方式查询了吗?
//adodb.pas
function VarDataSize(const Value: OleVariant): Integer;
begin
if VarIsNull(Value) then
Result := -1
else if VarIsArray(Value) then
Result := VarArrayHighBound(Value, 1) + 1
else if TVarData(Value).VType = varOleStr then
begin
//Result := Length(PWideString(@TVarData(Value).VOleStr)^);
Result := Length(PAnsiString(@TVarData(Value).VOleStr)^);
if Result = 0 then
Result := -1;
end
else
Result := SizeOf(OleVariant);
end;
另外,还有一点,如果传的参数是英文,就不会出错。
应该是bug 吧。
我后来也只是把语句改成
carteam :='一车队';
sql.Add('select * from gps_carinfor where gps_carcorp=(select id from gps_carteam where gps_carteamname='''+carteam+''')');
这样的形式。
http://www.nssoft.net/showdoc.asp?did=1200
跟踪结果:select * from gps_carinfor where gps_carcorp=(select id from gps_carteam where gps_carteamname='一'所以结果不正确