如何在Update语句中循环?

时间:2018-05-24 16:16:52

标签: oracle plsql

我想在UPDATE语句中循环。有可能这样做吗?它在“rec”变量中给出了一个错误。如果没有,你能告诉我如何做到这一点。这是我的代码,我尝试执行SELECT以更新包含所有变量的所有行。

         BEGIN
            UPDATE DATMCCN0
                SET E_NOME = (
                FOR rec IN(SELECT IID FROM DATMCCN0)
                LOOP
                SELECT I_NOME FROM DAT_CCNCONFIG0 INNER 
JOIN DATMCCN0 ON DAT_CCNCONFIG0.I_NOME = DATMCCN0.CAPLIC 
    where DATMCCN0.IID = rec.IID) 
                END LOOP;
            END;

2 个答案:

答案 0 :(得分:1)

您可能正在尝试correlated update

UPDATE datmccn0 ccn 
SET    e_nome = (SELECT i_nome 
                 FROM   dat_ccnconfig0 conf 
                 WHERE  ccn.i_nome = conf.caplic 
                        AND ccn.iid = conf.iid); 

此声明应解决您的问题。

答案 1 :(得分:0)

使用MERGE声明:

MERGE INTO DATMCCN0 dst
USING DAT_CCNCONFIG0 src
ON (
      src.IID    = dst.IID
  AND src.I_NOME = dst.CAPLIC
)
WHEN MATCHED THEN
  UPDATE SET E_NOME = src.I_NOME;

或者,取决于你的逻辑:

MERGE INTO DATMCCN0 dst
USING (
  SELECT b.IID,
         /* MAX(I_NOME) AS */ I_NOME
  FROM   DAT_CCNCONFIG0 a
         INNER JOIN DATMCCN0 b
         ON a.I_NOME = b.CAPLIC
  -- GROUP BY b.IID
) src
ON ( src.IID = dst.IID)
WHEN MATCHED THEN
  UPDATE SET E_NOME = src.I_NOME;