1

有没有人有关于如何在 PLSQL 的查询中使用 WITH 和 UPDATE 的示例?

我狂饮和谷歌搜索没有成功。

这是我想要做的

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
)

基本上这是一个 Type-II 维度更新。

4

2 回答 2

3

这是如何在 PLSQL 的 UPDATE 中使用 WITH。请注意,这仅适用于单列更新。

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
);
于 2010-11-18T20:21:07.073 回答
0

查看updateselect的语法图,我不确定子查询分解是否可以与 update 一起使用。您可以将这些查询完全替换回 UPDATE 语句的子查询部分,或者您可以为它们创建视图。我会同意意见。

于 2010-11-18T19:55:04.020 回答