2010-11-18 132 views
1

有沒有人有如何在PLSQL查詢中使用WITH和UPDATE的示例?如何在PLSQL中使用UPDATE和WITH

我狂歡並沒有成功Google搜索。

這裏就是我試圖做

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尺寸更新。

+0

@FrustratedWithFormsDesigner問題編輯添加代碼 – 2010-11-18 19:45:15

回答

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 
); 
0

看看updateselect的語法圖,我不確定子查詢因子分析可以用於更新。您可以將這些查詢全部替換回UPDATE語句的子查詢部分,或者可以爲它們創建視圖。我願意聽取意見。