You can implement it by using count() function:
<query connection-id="db">
select PRODUCT_ID as "product_id1", STORE_ID as "store_id1" from PRODUCT
<query connection-id="db">
select COUNT(*) as Price_Count from PRODUCT_PRICE where PRODUCT_ID=?product_id1 and STORE_ID=?store_id1
<script connection-id="db" if="Price_Count==0">
insert into PRODUCT_PRICE(REGULAR_PRICE, PRODUCT_ID, STORE_ID) values(?price, product_id1, store_id1)
</script>
<script connection-id="db" if="Price_Count gt 0">
update REGULAR_PRICE=?price where PRODUCT_ID=?product_id1 and STORE_ID=?store_id1
</script>
</query>
<query>
Although this solution should be working, you can leverage the power of SQL joins. The following solution needs only single query to accomplish this task. I will use left outer join. The idea is that you join 2 tables by product_id,store_id. When there is no match in product_price table, the corresponding price attribute will be null. Imagine you have the following input:
select * from product;
PRODUCT_ID STORE_ID
1 1
2 1
3 1
select * from product_price;
PRODUCT_ID STORE_ID PRICE
2 1 100
2 1 150
3 1 200
The left outer join of these 2 tables will look like:
select p.product_id, p.store_id , pp.price from product p LEFT OUTER JOIN product_price pp ON p.product_id =pp.product_id and p.store_id =pp.store_id;
PRODUCT_ID STORE_ID PRICE
1 1 null
2 1 100
2 1 150
3 1 200
Then you need to apply group by and count by price:
select p.product_id, p.store_id , count(pp.price) as Prices_Count from product p LEFT OUTER JOIN product_price pp ON p.product_id =pp.product_id and p.store_id =pp.store_id group by (p.product_id, p.store_id)
PRODUCT_ID STORE_ID Prices_Count
1 1 0
3 1 1
2 1 2
Then the XML becomes trivial and what is more important it should be much faster, because database joins are more efficient then ones made by any external client.
<query connection-id="db">
select p.product_id, p.store_id , count(pp.REGULAR_PRICE) as Prices_Count from product p LEFT OUTER JOIN product_price pp ON p.product_id =pp.product_id and p.store_id =pp.store_id group by (p.product_id, p.store_id)
<script connection-id="db" if="Prices_Count==0">
insert into PRODUCT_PRICE(REGULAR_PRICE, PRODUCT_ID, STORE_ID) values(?price, product_id, store_id)
</script>
<script connection-id="db" if="Prices_Count gt 0">
update REGULAR_PRICE=?price where PRODUCT_ID=?product_id and STORE_ID=?store_id
</script>
</query>
I think that the approach can be optimized even further by using MERGE INTO, so everything can be accomplished in a single Oracle statement, but I'll leave it for hungry minds.