hazelsisson
Programmer
Hi guys,
I have a union query in my stored procedure as follows, which inserts the results into a database over a database link.
My problem is the occurrence number that's inserted "zcmi_occ_no", which is always "01" for rows from the first part of the query, but could vary for the second part (in the query it's just set to "02" at the moment).
Is there any way I can change the value of this based on whether there are any results from the first part of the query? I would like it to be "01" if there are no rows returned from the first half of the query and "02" if there are 1 or more rows.
Many thanks,
Hazel
I have a union query in my stored procedure as follows, which inserts the results into a database over a database link.
Code:
insert into zcmintranet@hrtest( zcmi_con_no, zcmi_car_reg, zcmi_occ_no, zcmi_car_sch, zcmi_car_cc, zcmi_mls_ntx, zcmi_mls_tax )
--Part one of the query:
select u.emp_num, j.reg_num, '01', j.scheme_id, j.cc, (sum(j.total_miles) - sum(j.h2o_miles)) nt, 0 tx
from cm_users u, cm_claims c, cm_journeys j
where u.emp_num=c.emp_num
and c.claim_num=j.claim_num
and trunc(j.approval_date) between trunc(sdate) and trunc(edate)
and (j.scheme_id = 'DC' or j.scheme_id = 'DE')
and (j.total_miles - j.h2o_miles) > 0
group by u.emp_num, j.scheme_id , j.reg_num, j.cc
union
--Part two of the query:
select u.emp_num, j.reg_num, '02', j.scheme_id, j.cc, 0 nt, sum(j.taxable_miles) tx
from cm_users u, cm_claims c, cm_journeys j
where u.emp_num=c.emp_num
and c.claim_num=j.claim_num
and trunc(j.approval_date) between trunc(sdate) and trunc(edate)
and (j.scheme_id = 'DC' or j.scheme_id = 'DE')
and j.taxable_miles > 0
group by u.emp_num, j.scheme_id , j.reg_num, j.cc;
commit;
My problem is the occurrence number that's inserted "zcmi_occ_no", which is always "01" for rows from the first part of the query, but could vary for the second part (in the query it's just set to "02" at the moment).
Is there any way I can change the value of this based on whether there are any results from the first part of the query? I would like it to be "01" if there are no rows returned from the first half of the query and "02" if there are 1 or more rows.
Many thanks,
Hazel