如何在PLSQL中使用UPDATE和WITH

发布于 2024-10-03 09:11:37 字数 1113 浏览 4 评论 0原文

有谁有如何在 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
)

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

彡翼 2024-10-10 09:11:38

这是如何在 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
);

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
);
叹倦 2024-10-10 09:11:38

查看 update 和 < 的语法图a href="http://download.oracle.com/docs/cd/B14117_01/server.101/b10759/statements_10002.htm#i2077142" rel="nofollow">选择,我不确定子查询分解可以与更新一起使用。您可以将这些查询完整地替换回 UPDATE 语句的子查询部分,或者您可以为它们创建视图。我会选择观点。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文