2012-05-03 37 views
0

在我的應用程序中,我有一個名爲「每日例外報告」的報告,用於顯示ISA的例外情況。在這個表格中,我看到重複的條目(報告中恰好有兩個相同的行)。在將結果集插入表中時複製

技術細節。

例外情況報告將顯示在table中的可用數據中,該數據稱爲ExceptionFirstReported,其中有重複的條目。 Insert Statement用於將insert排成ExceptionFirstReportedstored procedureusp_Refresh_Daily_Exception_Report

INSERT INTO ExceptionFirstReported(
     InvestorReference, ExceptionReason, First_Reported_Date, RelativeRef) 
    SELECT InvestorReference, ExceptionReason, Dateupdated, RelatedInvRef FROM (
     SELECT * FROM DailyExceptionReport As DER 
       WHERE NOT EXISTS 
       (SELECT ExcepRptd.InvestorReference 
        FROM ExceptionFirstReported AS ExcepRptd 
        WHERE DER.InvestorReference = ExcepRptd.InvestorReference 
        AND DER.ExceptionReason = ExcepRptd.ExceptionReason 
        AND DER.RelatedInvRef = ExcepRptd.RelativeRef)) 
    AS CI 
    JOIN currentISAs AN CI.InvestorReference = Status_Inv_Ref 

我們認爲可以在兩種不同的情況下,沒有重複的條目這個說法,因爲不會有行中具有相同的InvestorReferenceExceptionReason和`RelatedInvRef。

但是我們有重複輸入。這是用於將行插入table的唯一insert聲明。 表DailyExceptionReportcurrentISAs以前不包含重複項。

上述插入查詢已在存儲過程「usp_refresh_dailyreport」中調用,並且SP僅在一個週期中執行過一次。

@returnvalue = Exec的usp_refresh_dailyreport

如果@返回值= 1

然後

InvestorReference ExceptionReason First_reported_dt
Recent_reported_dt relativeref report_gen_date

442643169642無餘額2012-04-11 9時54分:00
2012-05-04 23:58:00 NULL

442643169642>一ISA 2012-04-21六點30分00秒2012-04-23二十三點58分00秒452750423823 NULL

442643169642>一ISA 2012-04-21六點30分00秒
2012-04-23 23:58:00 452750423823 NULL

上述'ExceptionFirstReported'表中的數據的最後兩行是相同的,並且包含相同的relativeref(非空)。道歉傢伙'relativeref'不是空字段,但我可能會像第一行一樣保留空白數據。

回答

1

您是否在多個線程中同時運行此插入?兩個線程都可以運行存在檢查,找不到任何內容,併爲相同的數據執行插入操作。

我建議您添加一個唯一索引,以100%確定您沒有插入重複記錄。最好有一個(可操作的)異常而不是損壞的數據。

您可以通過使我們的語句在事務隔離級別可序列化下運行來解決此問題。

+0

thnk好友。問題是我無法向表中添加索引,因爲在向表中添加主索引時,如果插入查詢嘗試插入副本,則過程將停止發生錯誤。我正在爲銀行直播系統工作。這份報告必須每天生成。 – user1373129

+0

@Angelo Neuschitzer thnk u ..讓我檢查ABT的線程.. – user1373129