0

这是我需要转换为过程的查询。

MERGE INTO table1 SAI
 USING
 <IF> :SITE_PROJECTS_ID: != null &&:SITE_PROJECTS_ID: != 0 <THEN>(SELECT * from table2)AX
 ON (SAI.SITE_INFO_ID=AX.SITE_INFO_ID)
 WHEN MATCHED THEN UPDATE SET
 SAI.column1 = AX.column1,
 SAI.column2 = AX.column2,
 SAI.LAST_MODIFIED_BY = AX.LAST_MODIFIED_BY,
 SAI.LAST_MODIFIED_DATE = SYSDATE
 <ELSE>
 DUAL ON (SAI.SITE_INFO_ID=:SITE_INFO_ID:)
 WHEN MATCHED THEN UPDATE SET
 <IF> :value1: != null && :value2: != '' <THEN> SAI.SITE_TRAKER_SITE_ID = :value3:, <ENDIF>
  SAI.LAST_MODIFIED_DATE = sysdate
 <ENDIF>"

上面的合并语句需要转换成下面的 PL/SQL 格式:

DECLARE v_rowcount NUMBER DEFAULT 0;
BEGIN 
FOR ax IN ( SELECT * FROM table2 ) 
LOOP 
UPDATE table1 SET status = ax.status 
WHERE project_id = ax.site_projects_id;
v_rowcount := SQL%rowcount; 
IF ( v_rowcount = 0 ) 
THEN INSERT INTO table1 ( fuze_project_id, status ) 
VALUES ( ax.site_projects_id, ax.status ); v_rowcount := 0; 
END IF; 
END LOOP;
END;/

我对更新两个表感到困惑,因为当满足条件时我需要一个不同的表,否则我会得到 DUAL 表,并且会进行适当的更新。

请指导我如何进行并完成此任务。

4

1 回答 1

1

只需使用一个IF-ELSE语句并将MERGE语句分成两部分:

BEGIN
  IF :SITE_PROJECTS_ID != 0 THEN
    MERGE INTO table1 SAI
    USING table2 AX
    ON (SAI.SITE_INFO_ID=AX.SITE_INFO_ID)
    WHEN MATCHED THEN
      UPDATE
      SET SAI.column1 = AX.column1,
          SAI.column2 = AX.column2,
          SAI.LAST_MODIFIED_BY = AX.LAST_MODIFIED_BY,
          SAI.LAST_MODIFIED_DATE = SYSDATE;
  ELSIF :value1 IS NOT NULL AND :value2 IS NOT NULL THEN
    UPDATE table1
    SET SITE_TRAKER_SITE_ID = :value3,
        LAST_MODIFIED_DATE  = sysdate;
  END IF;
END;
/

注意:!= NULL永远不会是真的;你想要IS NOT NULL。此外,在 Oracle 中,''NULL.

于 2021-09-21T08:37:14.690 回答