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

Discussion of open issues, suggestions and bugs regarding MyDAC (Data Access Components for MySQL) for Delphi, C++Builder, Lazarus (and FPC)
Post Reply
jzs
Posts: 2
Joined: Thu 07 May 2015 07:31

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

Post by jzs » Thu 07 May 2015 09:27

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,

ViktorV
Devart Team
Posts: 3168
Joined: Wed 30 Jul 2014 07:16

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

Post by ViktorV » Thu 07 May 2015 11:04

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.

jzs
Posts: 2
Joined: Thu 07 May 2015 07:31

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

Post by jzs » Thu 07 May 2015 11:36

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';

ViktorV
Devart Team
Posts: 3168
Joined: Wed 30 Jul 2014 07:16

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

Post by ViktorV » Thu 07 May 2015 12:25

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.

Post Reply