I am currently writing update statements to keep a query-able table constantly up to date. The schema is identical between both tables and the contents are not important:
STAGING
ID
NAME
COUNT
PRODUCTION
ID
NAME
COUNT
My update statement looks as follows:
update PRODUCTION
set name = (select stage.name from staging stage where stage.name=name and rownum <2),
count = (select stage.countfrom staging stage where stage.count=count and rownum <2);
The two things of note is that 1) There is no where clause at the end of my update (this may be the problem) and 2) all records after being updated have the same values. What I mean by this is the following:
BEFORE UPDATE:
1,"JOHN", 12;
2,"STEVE",15;
3,"BETTY",2;
AFTER UPDATE
1,"JOHN", 12;
2,"JOHN",12;
3,"JOHN",12;
My question is how do I fix this so that the table properly reflects "new" data from staging as a correct SQL update?
UPDATE
So my staging data could coincidentally mirror what is in PRODUCTION
and for the sake of discussion it will:
STAGING DATA TO MERGE:
1,"JOHN", 12;
2,"STEVE",15;
3,"BETTY",2;
UPDATE the second
The query that I would like to run would be this:
update PRODUCTION
set production.name = staging.name,
production.count = staging.count
where production.name = staging.name;
This however results in invalid identifier issues on "staging.name"
UPDATE Subquery Finally, you can use a subquery in an UPDATE statement for the table to be updated. In the previous examples, we have just used the product table. However, you can use a subquery instead of the product table, which will return a result set that can be updated.
UPDATE operations with subqueries that reference the same table object are supported only if all of the following conditions are true: The subquery either returns a single row, or else has no correlated column references. The subquery is in the UPDATE statement WHERE clause, using Condition with Subquery syntax.
Example - Using EXISTS Clause You may wish to update records in one table based on values in another table. Since you can't list more than one table in the Oracle UPDATE statement, you can use the Oracle EXISTS clause. For example: UPDATE suppliers SET supplier_name = (SELECT customers.
A subquery cannot contain an ORDER BY clause. A subquery in an UPDATE statement cannot retrieve data from the same table in which data is to be updated. A subquery in a DELETE statement cannot retrieve data from the same table in which data is to be deleted.
There are two ways to do what you are trying
One is a Multi-column Correlated Update
UPDATE PRODUCTION a
SET (name, count) = (
SELECT name, count
FROM STAGING b
WHERE a.ID = b.ID);
DEMO
You can use merge
MERGE INTO PRODUCTION a
USING ( select id, name, count
from STAGING ) b
ON ( a.id = b.id )
WHEN MATCHED THEN
UPDATE SET a.name = b.name,
a.count = b.count
DEMO
Try it ..
UPDATE PRODUCTION a
SET (name, count) = (
SELECT name, count
FROM STAGING b
WHERE a.ID = b.ID)
WHERE EXISTS (SELECT 1
FROM STAGING b
WHERE a.ID=b.ID
);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With