what kind of database are u using? For oracle, I know you can do that use "merge" statement in oracle 9i: example
MERGE INTO sales s
USING new_sales n
ON (s.sales_transaction_id = n.sales_transaction_id)
WHEN MATCHED THEN
UPDATE s_quantity = s_quantity + n_quantity, s_dollar = s_dollar + n_dollar
WHEN NOT MATCHED THEN
INSERT (sales_quantity_sold, sales_dollar_amount)
VALUES (n.sales_quantity_sold, n.sales_dollar_amount);
for oracle version prior to 9i, u have to use two sql statement like:
UPDATE
(SELECT
s.sales_quantity_sold AS s_quantity,
s.sales_dollar_amount AS s_dollar,
n.sales_quantity_sold AS n_quantity,
n.sales_dollar_amount AS n_dollar
FROM sales s, new_sales n
WHERE s.sales_transaction_id = n.sales_transaction_id) sales_view
SET s_quantity = s_quantity + n_quantity, s_dollar = s_dollar + n_dollar;
INSERT INTO sales
SELECT * FROM new_sales s
WHERE NOT EXISTS
(SELECT 'x' FROM FROM sales t
WHERE s.sales_transaction_id = t.sales_transaction_id);
MERGE INTO sales s
USING new_sales n
ON (s.sales_transaction_id = n.sales_transaction_id)
WHEN MATCHED THEN
UPDATE s_quantity = s_quantity + n_quantity, s_dollar = s_dollar + n_dollar
WHEN NOT MATCHED THEN
INSERT (sales_quantity_sold, sales_dollar_amount)
VALUES (n.sales_quantity_sold, n.sales_dollar_amount);
for oracle version prior to 9i, u have to use two sql statement like:
UPDATE
(SELECT
s.sales_quantity_sold AS s_quantity,
s.sales_dollar_amount AS s_dollar,
n.sales_quantity_sold AS n_quantity,
n.sales_dollar_amount AS n_dollar
FROM sales s, new_sales n
WHERE s.sales_transaction_id = n.sales_transaction_id) sales_view
SET s_quantity = s_quantity + n_quantity, s_dollar = s_dollar + n_dollar;
INSERT INTO sales
SELECT * FROM new_sales s
WHERE NOT EXISTS
(SELECT 'x' FROM FROM sales t
WHERE s.sales_transaction_id = t.sales_transaction_id);