Oracle Update Statement With Subquery

Oracle Update Statement With Subquery

A SELECT statement that provides a set of rows for processing. Its syntax is like that of selectintostatement without the INTO clause. See 'SELECT INTO Statement'. A table or view that must be accessible when you execute the UPDATE statement, and for which you must have UPDATE privileges. UPDATE employees@remote SET salary = salary.1.1 WHERE lastname = 'Baer'; The next example shows the following syntactic constructs of the UPDATE statement: Both forms of the updatesetclause together in a single statement. A correlated subquery. A whereclause to limit the updated rows. The aim is to update the rows in the DESTTAB table with the data from the SOURCETAB table. Subquery Method. The first option is to do an update of the DESTTAB table using a subquery to pull the correct data from the SOURCETAB table. Notice the EXISTS predicate to exclude rows from the DESTTAB table with no matching row in the SOURCETAB. This Oracle tutorial explains how to use Oracle subqueries with syntax and examples. A subquery is a query within a query. In Oracle, you can create subqueries within your SQL statements.

Active1 year, 2 months ago

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:

My update statement looks as follows:

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:

Oracle Update Statement With Subquery Answers

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:

UPDATE the second

Free college database software. Our cloud-based PK-12 school administrative software solution is trusted by thousands of schools worldwide. Save time and money with easy-to-use software for Attendance, Scheduling, Web Gradebook, Report Cards, Discipline, Online Forms, Billing, Accounting, Web Portals, Mobile Apps, Library, Admissions with Online Applications and Forms, Development and much more.

The query that I would like to run would be this:

This however results in invalid identifier issues on 'staging.name'

Woot4Moo
Woot4MooWoot4Moo
20.2k13 gold badges81 silver badges136 bronze badges

4 Answers

There are two ways to do what you are trying

One is a Multi-column Correlated Update

You can use merge

Conrad FrixConrad Frix
47k11 gold badges80 silver badges129 bronze badges
Community
Md. Obaidul Haque SarkerMd. Obaidul Haque Sarker

As you've noticed, you have no selectivity to your update statement so it is updating your entire table. If you want to update specific rows (ie where the IDs match) you probably want to do a coordinated subquery.

However, since you are using Oracle, it might be easier to create a materialized view for your query table and let Oracle's transaction mechanism handle the details. MVs work exactly like a table for querying semantics, are quite easy to set up, and allow you to specify the refresh interval.

khoxseykhoxsey

Oracle Update Statement Example

Without examples of the dataset of staging this is a shot in the dark, but have you tried something like this?

This would work assuming the id column matches on both tables.

Oracle Update Statement With Subquery Number

HermitHermit

Oracle Update Statement With Subquery

Not the answer you're looking for? Browse other questions tagged sqloracle or ask your own question.