How to pass parameters to an ADOQuery object?

喜夏-厌秋 提交于 2019-11-28 08:14:40

问题


I am using ADOQuery in Delphi 7 and Oracle. I am getting error while passing parameters to ADOQuery. I have used following line. Please help me to identify error.

ADOQuery.Sql.text:= 'select * from temp_table '+
        'where column1 in (select column from table2 where id=:id) and id=:id';
ADOQuery.Parameters.ParamByValue('id').value= 'abc';
ADOQuery.open;

when I open the query i will get following error:

Parameter object is improperly defined. Inconsistent or incomplete information is provided.


回答1:


We have the same problem, we ended "masking" the class TParameters like this:

Declaration:

TMyParameter = class(TParameter)
private
  function GetAsValue: variant;
  Procedure SetAsValue(const Value: variant);
public
  property Value: variant read GetAsValue write SetAsValue;
end;

Implementation:

procedure TMyParameter.SetAsValue(const Value: variant);
var
  iPar: Integer;

begin
  for iPar:= 0 to Collection.Count - 1 do
    if (Name = TParameter(Collection.Items[iPar]).Name) then
      TParameter(Collection.Items[iPar]).Value:= Value;
end;

function TMyParameter.GetAsValue: variant;
begin
  Result:= inherited Value;
end;

And how to use:

TMyParameter(ADOQuery.Parameters.ParamByName('id')).AsValue:= 'abc';

I hope it helps.




回答2:


for i:=0 to ADOQuery.Parameters.Count-1 do
begin
  if ADOQuery.Parameters.Items[i].Name = 'id' then
    ADOQuery.Parameters.Items[i].Value := 'abc';
end;



回答3:


You need to distinguish between the two id;s:

ADOQuery.Sql.text:= 'select * from temp_table a where column1 in (select column from table2 b where b.id=:id) and a.id=:id'; 
ADOQuery.Parameters.ParamByValue('id').value= 'abc'; 
ADOQuery.open;


来源:https://stackoverflow.com/questions/7754345/how-to-pass-parameters-to-an-adoquery-object

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!