Using ParseSQL Command for ADO Parameters Cause Invalid Parameter DataType

1.4k views Asked by At

I have some SQL Command that contains a parameter such:(Note that myID has "int" type in SQL)

vSqlString :='Select * From myTable Where myID= :paramID';

and Use ParseSQL Command to execute this command:

myADOQuery.Parameters.ParseSQL(vSqlString , True);

Now myADOQuery.Parameters.ParamByName('paramID').DataType is smallint Type and it can't accept negative integer values.

I can exactly show to compiler that my Parameter[0].DataType is ftIneteger and it works properly, but what is a good solution for this problem?

2

There are 2 answers

2
Mohammad Gohari On BEST ANSWER

My question is asked by Mr. imanShadabi Here: Using TAdoQuery.ParseSql and has resolved by user1008646

1
pariasdev On

Hope this will help.

If you want to create in runtime parameters, you can use something like this:

ADOQuery1.Close;
ADOQuery1.SQL.Text := vSqlString;
ADOQuery1.Parameters.Clear;
ADOQuery1.Parameters.CreateParameter('paramID', ftInteger, pdInput, 10, vIntegerValue);
ADOQuery1.Open;

Or you can concatenate values to the query. For example:

//For Integer values:
vSqlString: = 'Select * From myTable Where myID =' + IntToStr (vIntegerValue); 

//For String values:
vSqlString: = 'Select * From myTable Where myID =' + QuotedStr (vStringValue); 

//For float values: 
//Be careful with this, usually in a query, the comma is separator values, 
//so make sure that the decimal separator is '.'
vDS := DecimalSeparator; //I keep the value it had 
DecimalSeparator := '.'; 
try
  ADOQuery1.close;
  ADOQuery1.SQL.Text := 'Select * From myTable Where myID='+FloatToStr(vFloatValue);
  ADOQuery1.Open;
finally
  DecimalSeparator := vDS; //Restore the value that had
end;

The third option is to set the parameters at design time. But I think this is not what you want.