Update Query [message #596234] |
Thu, 19 September 2013 05:59 |
Vackar
Messages: 81 Registered: October 2007
|
Member |
|
|
Hi,
I have a table that I'm trying to update using data from legacy tables.
I can generate the updated using the following SQL:
SELECT
'UPDATE SAMPLE SET IDENTIFIER = '''
|| IDENTFIER
|| ''' WHERE ID='
|| SAMPLE.ID
FROM (
select
G.ID SAMPLE_GROUP_ID,
D.NAME IDENTFIER,
S.SAMPLE SAMPLE_NUMBER,
ROW_NUMBER() OVER (partition by L.ID order by S.SAMPLE) CALCULATED_SAMPLE_NUMBER
from
SAMPLE_GROUP G,
PROTMS_DATAFILE D,
PROTMS_SOFTWAREANALYSIS S,
PROTMS_LANE L
WHERE L.ID = S.LANE_ID
AND L.ID = G.LANE_ID
AND S.DATAFILE_ID = D.ID
ORDER BY L.ID, S.SAMPLE
) OLD_DATA, SAMPLE
WHERE
SAMPLE.SAMPLE_NUMBER = OLD_DATA.CALCULATED_SAMPLE_NUMBER
AND SAMPLE.SAMPLE_GROUP_ID = OLD_DATA.SAMPLE_GROUP_ID;
However I'd like to do the update in the DB in a one-er.
I've tried the following SQL but it's not running and I've been at this for hours.
UPDATE SAMPLE SET SAMPLE_IDENTIFIER = (
SELECT IDENTFIER FROM (
select
G.ID SAMPLE_GROUP_ID,
D.NAME IDENTFIER,
S.SAMPLE SAMPLE_NUMBER,
ROW_NUMBER() OVER (partition by L.ID order by S.SAMPLE) CALCULATED_SAMPLE_NUMBER
from
SAMPLE_GROUP G,
PROTMS_DATAFILE D,
PROTMS_SOFTWAREANALYSIS S,
PROTMS_LANE L
WHERE L.ID = S.LANE_ID
AND L.ID = G.LANE_ID
AND S.DATAFILE_ID = D.ID
ORDER BY L.ID, S.SAMPLE
) OLD_DATA
WHERE SAMPLE.SAMPLE_NUMBER = OLD_DATA.CALCULATED_SAMPLE_NUMBER
AND SAMPLE.SAMPLE_GROUP_ID = OLD_DATA.SAMPLE_GROUP_ID
);
Can anyone spot any obvious errors?
Best Regards,
Vackar
|
|
|
|
Re: Update Query [message #596236 is a reply to message #596235] |
Thu, 19 September 2013 06:07 |
Vackar
Messages: 81 Registered: October 2007
|
Member |
|
|
Hi Pablolee,
Apologies, I should clarify. It does run (it seems to be syntactically correct) however I have a not null constraint on the column that needs updating and when I run the update it fails with a constraint violation stating that it cannot insert null into this column. However when I run the first query with 'AND IDENTIFER IS NULL' I get zero rows.
Vackar
|
|
|
Re: Update Query [message #596238 is a reply to message #596236] |
Thu, 19 September 2013 06:31 |
Vackar
Messages: 81 Registered: October 2007
|
Member |
|
|
I've figured out what was causing this.
There was an assumption that all data from the legacy table would map to the new tables, however it turns out this was not the case.
Thanks Palolee for taking the time to look.
Vackar
|
|
|