Page 1 of 1

Using the parameter's value in the same query multiple times

Posted: Thu 07 May 2015 09:27
by jzs
Hi there,
Is it possible to execute/open a query which needs one parameter that gets used multiple times?

e.g:

Code: Select all

select * from Table
where ColA = :Par1
and ColB = :Par1
If I open this query, only the first parameter gets the value when I do:
MyQuery.ParamByName('Par1').AsString:= 'Value';
The second parameter is Null or not provided.

I know I can work around by making them two different params which hold the exact same value (:Par1&:Par2), or using MySQL variables.

But I need to know if it's possible to reuse that only one parameter, multiple times just by passing one line value and all occurrence of the parameter in the query gets evaluated to that value.

i.e:

Code: Select all

select * from Table
where ColA = :Par1
and ColB = :Par1
MyQuery.ParamByName('Par1').AsString:= 'Value';

To make the query like:

Code: Select all

select * from Table
where ColA = 'Value'
and ColB = 'Value'
Regards,

Re: Using the parameter's value in the same query multiple times

Posted: Thu 07 May 2015 11:04
by ViktorV
You can use several parameters with similar names in a SQL query. In this case, after setting the parameter value using the TMyQuery.ParamByName method, in the resulting SQL query the specified value will be assigned for each parameter with the specified name.

Re: Using the parameter's value in the same query multiple times

Posted: Thu 07 May 2015 11:36
by jzs
Thanks for replying ViktorV.

I am sure, it does not work like that when I tried. Only the first parameter occurrence gets the assigned value while the second occurrence gets NULL.

For instance when I try the following query I get no result:

Code: Select all

select * from Table
where ColA = :Par1
and ColB = :Par1
MyQuery.ParamByName('Par1').AsString:= 'Value';

But, this one gets me the result I want:

Code: Select all

select * from Table
where ColA = :Par1
and ColB = :Par2
MyQuery.ParamByName('Par1').AsString:= 'Value';
MyQuery.ParamByName('Par2').AsString:= 'Value';

Re: Using the parameter's value in the same query multiple times

Posted: Thu 07 May 2015 12:25
by ViktorV
Unfortunately, we couldn't reproduce the issue according to the described steps: we got the same result in both cases. Please update MyDAC to the latest version (8.5.14). If this doesn't solve the issue, send us a small sample demonstrating the issue, including the script for creating and filling in the database objects.