Oracle 与存储过程合并

问题描述 投票:0回答:1

数据未插入,这是我使用 Oracle 21c Express 所拥有的。

create or replace PROCEDURE UPSERTREADINGS 
(
   STATION_ID NVARCHAR2
 , READING_DATE TIMESTAMP 
 , READING_TYPE NVARCHAR2 
 , RESULT_VALUE NUMBER 
 , RESULT_TEXT NVARCHAR2 
 , RESULT_UNIT NVARCHAR2 
 , READING_STATUS NVARCHAR2 
 , ACTIVITY_TYPE NVARCHAR2 
 , COMMENTS NVARCHAR2 
 , SOURCE_FILE_ID NUMBER
) AS
BEGIN
    MERGE INTO READINGS r
        USING (SELECT STATION_ID, READING_DATE, READING_TYPE, RESULT_VALUE, RESULT_TEXT, RESULT_UNIT, 
        READING_STATUS, ACTIVITY_TYPE, COMMENTS, SOURCE_FILE_ID FROM READINGS) e 
        ON ( r.STATION_ID = e.STATION_ID AND r.READING_DATE = e.READING_DATE 
            AND r.READING_TYPE = e.READING_TYPE )
    WHEN MATCHED THEN 
        UPDATE SET 
            r.RESULT_VALUE = UPSERTREADINGS.RESULT_VALUE,
            r.RESULT_TEXT = UPSERTREADINGS.RESULT_TEXT,
            r.RESULT_UNIT = UPSERTREADINGS.RESULT_UNIT,
            r.READING_STATUS = UPSERTREADINGS.READING_STATUS,
            r.ACTIVITY_TYPE = UPSERTREADINGS.ACTIVITY_TYPE,
            r.COMMENTS = UPSERTREADINGS.COMMENTS,
            r.SOURCE_FILE_ID = UPSERTREADINGS.SOURCE_FILE_ID
    WHEN NOT MATCHED THEN 
        INSERT (STATION_ID, READING_DATE, READING_TYPE, RESULT_VALUE, RESULT_TEXT, RESULT_UNIT, READING_STATUS, ACTIVITY_TYPE, COMMENTS, SOURCE_FILE_ID) 
        VALUES (UPSERTREADINGS.STATION_ID, UPSERTREADINGS.READING_DATE, UPSERTREADINGS.READING_TYPE, UPSERTREADINGS.RESULT_VALUE, UPSERTREADINGS.RESULT_TEXT, 
            UPSERTREADINGS.RESULT_UNIT, UPSERTREADINGS.READING_STATUS, UPSERTREADINGS.ACTIVITY_TYPE, UPSERTREADINGS.COMMENTS, UPSERTREADINGS.SOURCE_FILE_ID);
    COMMIT;
END UPSERTREADINGS;

我尝试过使用 e. 而不是 UPSERTREADINGS。 但不清楚在哪里使用 e 或

谢谢

oracle plsql
1个回答
0
投票

这解决了这个问题,感谢@Boneist的帮助

create or replace PROCEDURE UPSERTREADINGS 
(
  STATION_ID NVARCHAR2
, READING_DATE TIMESTAMP 
, READING_TYPE NVARCHAR2 
, RESULT_VALUE NUMBER 
, RESULT_TEXT NVARCHAR2 
, RESULT_UNIT NVARCHAR2 
, READING_STATUS NVARCHAR2 
, ACTIVITY_TYPE NVARCHAR2 
, COMMENTS NVARCHAR2 
, SOURCE_FILE_ID NUMBER
) AS
BEGIN
    MERGE INTO READINGS r
        USING (SELECT UPSERTREADINGS.STATION_ID As StationID, UPSERTREADINGS.READING_DATE As ReadingDate, UPSERTREADINGS.READING_TYPE As ReadingType, UPSERTREADINGS.RESULT_VALUE As ResultValue, 
        UPSERTREADINGS.RESULT_TEXT As ResultText,     UPSERTREADINGS.RESULT_UNIT As ResultUnit, UPSERTREADINGS.READING_STATUS As     ReadingStatus, UPSERTREADINGS.ACTIVITY_TYPE As ActivityType, 
            UPSERTREADINGS.COMMENTS As Comments, UPSERTREADINGS.SOURCE_FILE_ID As SourceFileID FROM dual) e 
        ON ( r.STATION_ID = UPSERTREADINGS.STATION_ID AND r.READING_DATE = UPSERTREADINGS.READING_DATE AND r.READING_TYPE = UPSERTREADINGS.READING_TYPE )
    WHEN MATCHED THEN 
        UPDATE SET 
            r.RESULT_VALUE = e.ResultValue,
            r.RESULT_TEXT = e.ResultText,
            r.RESULT_UNIT = e.ResultUnit,
            r.READING_STATUS = e.ReadingStatus,
            r.ACTIVITY_TYPE = e.ActivityType,
            r.COMMENTS = e.Comments,
            r.SOURCE_FILE_ID = e.SourceFileID
    WHEN NOT MATCHED THEN 
        INSERT (STATION_ID, READING_DATE, READING_TYPE, RESULT_VALUE, RESULT_TEXT, RESULT_UNIT, READING_STATUS, ACTIVITY_TYPE, COMMENTS, SOURCE_FILE_ID) 
        VALUES (e.StationID, e.ReadingDate, e.ReadingType, e.ResultValue, e.ResultText, e.ResultUnit, e.ReadingStatus, e.ActivityType, e.Comments, e.SourceFileID);
    COMMIT;
END UPSERTREADINGS;
© www.soinside.com 2019 - 2024. All rights reserved.