1

Need a bit of help on a query for Oracle. Basically I would like to put together a query that updates a number of columns to replace actual data with test data. To do this we need to do things like change project codes in two tables, but the references must match, so for example I'm looking to do...

UPDATE table1 t1, table2 t2 
SET t1.project_ref = concat('projectRef-',rownum), 
    t2.project_ref = t1.project_ref 
WHERE t1.project_ref = t2.project_ref and t1.project_client = XYZ

This would change t1.project_ref and t2.project_ref to something like 'projectRef-1' for client XYZ.

Any ideas?

Regards

KS

2
  • 2
    :You need two different update statements for that Commented May 13, 2013 at 12:41
  • Hmmmm, going to be awkward if I have 1,279 projects reference numbers, I update in one table, then have to go through and do it in the second. How will I know which relates to which. Kind of hoping Oracle could do something clever here (it costs enough, I'd like it to earn it's fees :-D ) Commented May 13, 2013 at 12:43

2 Answers 2

4

Generally updates on joins are possible in Oracle.

This is the syntax: update (select T1.* from T1, T2 where .. ) set ...

They require a join condition between T1 and T2, and a unique index on the join column in T2 to assure that the select returns at most one column per row in T1. Unfortunately for you there is another limitation that says, that only columns in one of the tables can be updated (T1 here).

So you need an intermediate table where you insert the changeset of project_ref to new-project_ref and then use this table to update T1 and T2 separately.

Sign up to request clarification or add additional context in comments.

Comments

2

I'm afraid you're going to have to build a translation table. Something like:

project_test_data
-----------------
real_project_ref varchar2(30)
test_project_ref varchar2(30)

Then write update statements substituting the test version for the real version in the relevant tables.

Note that if you have defined foreign key relationships on these columns you will need to disable them, or at least make them deferrable and deferred. This is one reason to avoid using meaningful data as primary keys.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.