更新Oracle

时间:2016-06-16 08:30:58

标签: sql oracle sql-update dml

我想更新表EFORMDYNAMICFIELDINSTANCE的EDFI_id字段。该字段具有NULL值。所以我想采取ROWID并更新这个字段(EDFI_id)。

我尝试使用CTE在Oracle中更新上表,但是我收到了以下错误。

ORA-00928: missing SELECT keyword 00928. 00000 - "missing SELECT keyword" *Cause:
*Action: Error at Line: 21 Column: 1

以下是我的更新声明。

WITH tmp(row_id , ENTERPRISEID,OWNERTYPE,OWNERID,ITEMTYPE,ITEMID) AS (
    SELECT Row_number() over (order by ENTERPRISEID, OWNERTYPE, OWNERID, ITEMTYPE, ITEMID) as row_id , 
           ENTERPRISEID,
           OWNERTYPE,
           OWNERID,
           ITEMTYPE,
           ITEMID 
      FROM EFORMDYNAMICFIELDINSTANCE 
     ORDER BY ITEMID,ITEMTYPE )
UPDATE EFORMDYNAMICFIELDINSTANCE EDFI 
  SET EDFI.EDFI_id = tmp.row_id + 700000 
INNER JOIN tmp ON EDFI.ENTERPRISEID = tmp.ENTERPRISEID 
       AND EDFI.OWNERTYPE = tmp.OWNERTYPE 
       and EDFI.OWNERID = tmp.OWNERID 
       AND EDFI.ITEMTYPE = tmp.ITEMTYPE 
       AND EDFI.ITEMID = tmp.ITEMID;

我想在此提及我能够使用CTE选择数据但无法更新。以下是选择标准。

WITH tmp(row_id , ENTERPRISEID,OWNERTYPE,OWNERID,ITEMTYPE,ITEMID) AS (
    SELECT Row_number() over (order by ENTERPRISEID, OWNERTYPE, OWNERID, ITEMTYPE, ITEMID) as row_id , 
           ENTERPRISEID,
           OWNERTYPE,
           OWNERID,
           ITEMTYPE,
           ITEMID 
      FROM EFORMDYNAMICFIELDINSTANCE 
     ORDER BY ITEMID,ITEMTYPE )
select * 
 from EFORMDYNAMICFIELDINSTANCE EDFI 
INNER JOIN tmp ON EDFI.ENTERPRISEID = tmp.ENTERPRISEID 
  AND EDFI.OWNERTYPE = tmp.OWNERTYPE 
  and EDFI.OWNERID = tmp.OWNERID 
  AND EDFI.ITEMTYPE = tmp.ITEMTYPE 
  AND EDFI.ITEMID = tmp.ITEMID;

我尝试使用以下查询更新表,但运行时间超过2小时,因此我取消了查询。

UPDATE EFORMDYNAMICFIELDINSTANCE EDFI
   SET EDFI.EDFI_ID = (SELECT rn.row_id + 7000000 FROM
(Select Row_Number() Over (Order By Enterpriseid,Ownertype,Ownerid,Itemtype,Itemid)As Row_Id , Enterpriseid,Ownertype,Ownerid,Itemtype,Itemid From Eformdynamicfieldinstance) Rn
                  WHERE EDFI.ITEMTYPE=rn.ITEMTYPE and EDFI.ITEMID=rn.ITEMID and EDFI.Enterpriseid=rn.Enterpriseid and edfi.Ownertype=rn.Ownertype and edfi.Ownerid=rn.Ownerid  )
WHERE EXISTS( SELECT 1
                 FROM (select Row_number() over (order by Enterpriseid,Ownertype,Ownerid,Itemtype,Itemid)as row_id , Enterpriseid,Ownertype,Ownerid,Itemtype,Itemid from EFORMDYNAMICFIELDINSTANCE) rn
                WHERE EDFI.ITEMTYPE=rn.ITEMTYPE and EDFI.ITEMID=rn.ITEMID and EDFI.Enterpriseid=rn.Enterpriseid and edfi.Ownertype=rn.Ownertype and edfi.Ownerid=rn.Ownerid) and EDFI.EDFI_ID is null
/

这个查询的解释计划非常高,同时更新记录但是如果选择解释计划是相当不错的

注意: - 上面的表EFORMDYNAMICFIELDINSTANCE有超过200,000条记录 请提供最佳解决方案/查询以更新此表。

此表格(EFORMDYNAMICFIELDINSTANCE)在ENTERPRISEIDOWNERTYPEOWNERIDITEMTYPEITEMID列上有复合主键。

0 个答案:

没有答案