> create view v1 (code,num) as
> select 'AAA',id from table1
> union
> select 'BBB',id from table2;
As your rows are, by definition, distinct between each subquery, you
should use UNION ALL instead of UNION to save postgres the trouble of
hunting non-existing duplicates. This will save you a few sorts.
> select * from v1 where code||num = 'AAA2005000001';
Why don't you use code='AAA' and num='2005000001' ?