Addwhere and Union

Discussion of open issues, suggestions and bugs regarding IBDAC (InterBase Data Access Components) for Delphi, C++Builder, Lazarus (and FPC)
Post Reply
Fabrice
Posts: 45
Joined: Tue 07 Sep 2010 09:44

Addwhere and Union

Post by Fabrice » Fri 07 Mar 2014 15:20

Hello,

Addwhere method (TibcQuery) is really usefull but it doesn't work with "Union" clause.
Any solution ?

Easy to reproduce with a query like :

Code: Select all

select *
from customer qc
union
select *
from customercopy qc2
If you do addwhere('(0=0)');

Result is :

Code: Select all

select *
from customer qc
where (0=0)
union
select *
from customercopy qc2
Of course same problem with deletewhere...

Best regards,
Fabrice

ZEuS
Devart Team
Posts: 240
Joined: Thu 05 Apr 2012 07:32

Re: Addwhere and Union

Post by ZEuS » Tue 11 Mar 2014 08:34

The AddWhere method inserts the WHERE statement into the first found SELECT clause. So, when you need to apply AddWhere to the whole UNION statement, you should rewrite your query like the following:

Code: Select all

select * from (
  select *
  from customer qc
  union
  select *
  from customercopy qc2
)

Fabrice
Posts: 45
Joined: Tue 07 Sep 2010 09:44

Re: Addwhere and Union

Post by Fabrice » Mon 17 Mar 2014 09:19

Hello,

The big problem it is that query like :

Code: Select all

select * from (
  select *
  ...
 union
 select *
)
Return read only result !

ZEuS
Devart Team
Posts: 240
Joined: Thu 05 Apr 2012 07:32

Re: Addwhere and Union

Post by ZEuS » Wed 26 Mar 2014 11:30

You can avoid this problem by providing your own SQL statements in the SQLInsert, SQLUpdate, SQLDelete, etc. properties. For example:

Code: Select all

IBCQuery1.SQL.Text := 'select * from (select * from customer qc union select * from customercopy qc2)';
IBCQuery1.SQLInsert.Text := 'insert into customer(ID, CNAME) values(:ID, :CNAME)';
IBCQuery1.SQLUpdate.Text := 'update customer set ID = :ID, CNAME = :CNAME where ID = :Old_ID';
For more information, please refer to the IBDAC documentation.

Post Reply