这是我需要转换为过程的查询。
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 表,并且会进行适当的更新。
请指导我如何进行并完成此任务。