在oracle中插入,更新和截断临时表

时间:2013-02-13 09:08:09

标签: oracle plsql temp-tables

我有一个脚本(它不能作为函数/存储过程创建)。当脚本运行时,我必须将表(从核心数据库)的结果插入临时表(在报告数据库中)。

插入结果后,我已更新另一个表中的现有行(在报告数据库中)。

需要从临时表中删除所有已更新的行,其余行必须插入主表(在报告数据库中)。

由于我对Oracle / Toad的语法不熟悉,我无法弄清楚如何做到这一点:

这是我的半脚本,希望这可以由专家在这里完成:

BEGIN

-- In REP DB
DECLARE
ExtractType         NUMBER(9);
RecordsExtracted    NUMBER(9);
CurStatus           NUMBER(9);
StartDate           date;
ErrorMessage        NVARCHAR2 (1000);
LastExtrctTimestamp DATE;

CREATE GLOBAL TABLE TEMP_AuditChanges
    (
    AuditRecordId           NUMBER(18),
    AuditChangeId           NUMBER(18),
    FieldName               NVARCHAR2(255),
    BeforeValue             NVARCHAR2(2000),
    AfterValue              NVARCHAR2(2000),
    AgreementName           NVARCHAR2(255),
    ActionName              NVARCHAR2(255),
    TrackedDataType         NVARCHAR2(255),
    ReferenceId             NUMBER(18),
    AmendedByName           NVARCHAR2(255),
    AmendedDate             DATE,
    ReferenceName           NVARCHAR2(255),
    ParentAuditRecordId     NUMBER(18),
    ParentReferenceName     NVARCHAR2(255),
    ParentReferenceId       NUMBER(18),
    ReviewedByName          NUMBER(18),
    ReviewedDate            DATE
    ) ON COMMIT DELETE ROWS;



    StartDate := sysdate;
    ExtractType := 66;

    SELECT * FROM TEMP_AuditChanges;

    -- Fetching the Last Extract Time Stamp 
    Select  max(ExtractTimestamp) INTO LastExtrctTimestamp from ExtractRecords where Status = 2 and  ExtractRecords.ExtractType= ExtractType;

    IF LastExtrctTimestamp IS NULL
    THEN LastExtrctTimestamp := To_Date('01/01/1901', 'MM/dd/yyyy');
    END IF;

    INSERT INTO TEMP_AuditChanges (AuditRecordId,AuditChangeId,FieldName,BeforeValue,AfterValue,AgreementName,ActionName,TrackedDataType,ReferenceId,AmendedByName,AmendedDate,ReferenceName,ParentAuditRecordId,ParentReferenceName,ParentReferenceId,ReviewedByName,ReviewedDate)
    -- From CORE DB 
    SELECT
            AR.AUDITRECORDID, NVL(AC.AUDITCHANGEID, 0) AUDITCHANGEID, AC.FIELDNAME, AC.BEFOREVALUE,
            AC.AFTERVALUE, AG.AGREEMENTNAME, ChangeAction.EnumText ActionName, 
            TrackedDataType.ENUMTEXT TrackedDataType, AR.REFERENCEID, AmendedBy.USERNAME AmendedByName,
            AR.AMENDEDDATE, AR.REFERENCENAME, AR.PARENTAUDITRECORDID, AR.PARENTREFERENCENAME, AR.PARENTREFERENCEID,
            ReviewdBy.USERNAME ReviewedByName, AR.REVIEWEDDATE
    FROM    AuditRecords@RPTCORE AR 
            LEFT OUTER JOIN AuditChanges@RPTCORE AC ON AR.AUDITRECORDID = AC.AUDITRECORDID
            LEFT OUTER JOIN Agreements@RPTCORE AG ON AC.AGREEMENTID = AG.AGREEMENTID
            LEFT OUTER JOIN EnumValues@RPTCORE ChangeAction ON AR.ACTION = ChangeAction.ENUMVALUE AND ChangeAction.ENUMTYPE = 'ChangeAction'
            LEFT OUTER JOIN EnumValues@RPTCORE TrackedDataType ON AR.DATATYPE = TrackedDataType.ENUMVALUE AND TrackedDataType.ENUMTYPE = 'TrackedDataType'
            LEFT OUTER JOIN Users@RPTCORE AmendedBy ON AR.AMENDEDBY = AmendedBy.USERID 
            LEFT OUTER JOIN Users@RPTCORE ReviewdBy ON AR.REVIEWEDBY = ReviewdBy.USERID
    WHERE   AmendedDate >= LastExtrctTimestamp
    ORDER BY AC.AUDITCHANGEID;
    RecordsExtracted := SQL%RowCount;

END;

注意:@RPTCORE是从CORE DB到REP DB获取数据的DB链接。

0 个答案:

没有答案