Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I insert a record when comparing two tables?

Status
Not open for further replies.

john9

Programmer
May 31, 2002
16
0
0
US
How can I insert a record into a table when comparing two other tables? In the code shown below I am comparing two different tables. When a 'not exist' condition occurs I want to insert a new record into a third table.

=============================================
select system_id, cycle_date
from commdev.comm_agent_balances_hdr_stg1 h

where not exists
(select system_id, cycle_date from commdev.comm_hdr_trl c
where h.system_id = c.system_id
and h.cycle_date = c.cycle_date);

insert into commdev.comm_agent_balance_status
(status_system_id ,
status_cycle_date ,
status_message)
select
h.system_id ,
h.cycle_date ,
'ERROR'
from commdev.comm_agent_balances_hdr_stg1 h;
 
I can not recognize your problem. Why don't you use

insert into <your table>
select system_id, cycle_date
from commdev.comm_agent_balances_hdr_stg1 h
where not exists
(select system_id, cycle_date from commdev.comm_hdr_trl c
where h.system_id = c.system_id
and h.cycle_date = c.cycle_date);

in an ordinary way?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top