Home » SQL & PL/SQL » SQL & PL/SQL » Update Query (Oracle 10g, Centos 5)
Update Query [message #596234] Thu, 19 September 2013 05:59 Go to next message
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 #596235 is a reply to message #596234] Thu, 19 September 2013 06:03 Go to previous messageGo to next message
pablolee
Messages: 2882
Registered: May 2007
Location: Scotland
Senior Member
When you say "is not running" exactly what do you mean?
Re: Update Query [message #596236 is a reply to message #596235] Thu, 19 September 2013 06:07 Go to previous messageGo to next message
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 Go to previous message
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
Previous Topic: Limitations for With data clause
Next Topic: Architecture
Goto Forum:
  


Current Time: Wed Apr 24 11:54:20 CDT 2024