문제

Does anyone have a sample of how to use WITH and UPDATE in a query in PLSQL?

I binged and googled without success.

Here is what I am trying to do

WITH
OldRecords AS
(
  SELECT 'New Records' as RecordLabel, rowid, OrDev.* 
  FROM CDR.MSRS_OR_ORDEV OrDev
  WHERE 1=1
    AND OrDev.ASOFENDDATETIME IS NULL
    AND OrDev.REPORTSTARTDATE = to_date ('7/1/2010', 'mm/dd/yyyy')
    AND OrDev.REPORTENDDATE = to_date ('7/31/2010', 'mm/dd/yyyy')
    AND OrDev.downloadrequestid <> 3
), 
NewRecords AS
(
  SELECT 'Old Records' as RecordLabelrowid, OrDev.* 
  FROM CDR.MSRS_OR_ORDEV OrDev
  WHERE 1=1
    AND OrDev.ASOFENDDATETIME IS NULL
    AND OrDev.REPORTSTARTDATE = to_date ('7/1/2010', 'mm/dd/yyyy')
    AND OrDev.REPORTENDDATE = to_date ('7/31/2010', 'mm/dd/yyyy')
    AND OrDev.downloadrequestid = 3
)
UPDATE CDR.MSRS_OR_ORDEV SET ASOFENDDATETIME = GETDATE()
WHERE RowID IN
(
  SELECT OldRecords.RowId
  FROM OldRecords
    INNER JOIN NewRecords
      ON  OldRecords.Customer_Id = NewRecords.Customer_Id
      AND OldRecords.BusinesKey1 = NewRecords.BusinesKey1
      AND OldRecords.BusinesKey2 = NewRecords.BusinesKey2
)

Basically this is a Type-II dimension update.

도움이 되었습니까?

해결책

This is how to use WITH in an UPDATE in PLSQL. Note that this only works for a single column update.

UPDATE CDR.MSRS_OR_ORDEV 
SET ASOFENDDATETIME = sysdate
WHERE RowID IN
(
  WITH
  OldRecords AS
  (
    SELECT rowid, OldRecords.* 
    FROM CDR.MSRS_OR_ORDEV OldRecords
    WHERE 1=1
      AND OldRecords.ASOFENDDATETIME IS NULL
      AND OldRecords.REPORTSTARTDATE = to_date ('7/1/2010', 'mm/dd/yyyy')
      AND OldRecords.REPORTENDDATE = to_date ('7/31/2010', 'mm/dd/yyyy')
      AND OldRecords.downloadrequestid <> 3
  ), 
  NewRecords AS
  (
    SELECT rowid, NewRecords.* 
    FROM CDR.MSRS_OR_ORDEV NewRecords
    WHERE 1=1
      AND NewRecords.ASOFENDDATETIME IS NULL
      AND NewRecords.REPORTSTARTDATE = to_date ('7/1/2010', 'mm/dd/yyyy')
      AND NewRecords.REPORTENDDATE = to_date ('7/31/2010', 'mm/dd/yyyy')
      AND NewRecords.downloadrequestid = 3
  )
  SELECT OldRecords.RowId
  FROM OldRecords
    INNER JOIN NewRecords
      ON  OldRecords.BusinesKey1 = NewRecords.BusinesKey1
      AND OldRecords.BusinesKey2 = NewRecords.BusinesKey2
);

다른 팁

Looking at the syntax diagrams for update and select, I'm not sure the subquery factoring can be used with update. You could either substitute those queries in full back into the subquery part of your UPDATE statement, OR you could create views for them. I'd go with the views.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top