2016-11-17 264 views
0

上述主題行的解決方案存在於多個鏈接中,並且遇到了所有鏈接,並且找不到我的問題的解決方案。INSERT語句與數據庫中發生的CHECK約束衝突發生衝突

這裏是我的查詢:

INSERT INTO PatientPatternElements (PatientPatternsID, ASEventID, LocationID, EffectiveDate, EffectiveDateEnd, WorkloadStartDate, WorkloadEndDate) 
       SELECT DISTINCT PatientPatterns.ID, 
         @hl7event_id, 
         @default_locationid, 
         @hl7event_effective_date, 
         @hl7event_effective_date, 
         @hl7event_effective_date, 
         @hl7event_effective_date 
       FROM PatientPatternElements 
       INNER JOIN PatientPatterns 
        ON PatientPatternElements.PatientPatternsID = PatientPatterns.ID 
       WHERE PatientPatterns.PatientEncounterID = @patient_encounter_id 
        AND PatientPatterns.Visit = @patient_encounter_visit 

       --------------------- 
       -- Error Checking 
       ---------------------- 
       SET @SysError = @@error 
       IF @SysError != 0 
       BEGIN 
        SET @iError = 23063 
        SET @strErrorMsg = 'Error Applying Manual Discharge Event, Error: ' + cast(@SysError AS varchar(256)) + ' encounter_id=' + cast(@patient_encounter_id as varchar(64)) 
        GOTO ERROR 
       END 

這只是我的我的存儲過程的代碼的一部分。

工作流程

這是發生在在客戶端測試環境的版本之一,客戶有一個更版本和流工作沒有任何問題。 當我們試圖運行記錄手動它是在我們的日誌表的一個拋出一個錯誤,並拋出以下消息:

THREAD 4: ApplyPatientPatternEvents: Recovering from SQL exception: The INSERT statement conflicted with the CHECK constraint "CK_PatientPatternElements_Location". 
The conflict occurred in database "db_name", table "dbo.PatPatternElements". 

Rolling back Transaction. spHL7ApplyPatientPatternEvents: Error Applying Manual Discharge Event, Error: 547 encounter_id=20 

我前面說這是發生在只有一個版本,存儲過程在兩個版本中都是相同的,這使得很難找出問題。

在事件查看器中沒有任何記錄,並且跟蹤sql profiler也沒有多大幫助,因爲SP spHL7ApplyPatientPatternEvents從未出現在profiler中。

任何人都可以給出一個想法究竟發生了什麼?

這是約束的查詢。

ALTER TABLE [dbo].[PatientPatternElements] WITH CHECK ADD CONSTRAINT [CK_PatientPatternElements_Location] CHECK (([LocationID] IS NULL AND [LevelOfCareGroupID] IS NOT NULL OR [LocationID] IS NOT NULL AND [LevelOfCareGroupID] IS NULL)) 
GO 

ALTER TABLE [dbo].[PatientPatternElements] CHECK CONSTRAINT  [CK_PatientPatternElements_Location] 
GO 
+0

您能否提供引發錯誤的參數值? –

+0

這些參數在許多地方,因此使得苦苦追尋的價值時,你有什麼更好的辦法是找到這些參數的值是多少? – Manjuboyz

+0

對不起,我想'當我們試圖手動運行一個記錄時,它會拋出一個錯誤',這意味着你重新創建了錯誤。一旦方法將插入語句包裝在[try ... catch塊](https://msdn.microsoft.com/en-gb/library/ms175976.aspx)中。使用catch塊轉移的參數值,[用戶](https://msdn.microsoft.com/en-us/library/ms187934.aspx)和[主機](https://msdn.microsoft.com/en- us/library/ms178598.aspx)名稱添加到您的日誌表中。 –

回答

0

Manjuboyz,

以下幾點可以幫助你瞭解什麼是在代碼中發生的事情。

  1. 正如你所提到的「有兩種環境,Insert語句在一個環境中工作正常,而不是在另一個環境中工作」。基於這個陳述,我能否建議你檢查兩個環境中是否存在相同的約束條件。
  2. 這裏的CHECK約束是檢查插入的值是否具有指定的值。如果失敗,那麼約束將失敗,並出現您提到的錯誤。因此,請使用TRY,CATCH將插入語句置於TRY下執行異常處理。因此,如果在CATCH塊中有任何錯誤,您可以捕獲錯誤,並且插入[LocationID]和[LevelOfCareGroupID]的值。

  3. ALTER TABLE [dbo].[PatientPatternElements] WITH CHECK ADD CONSTRAINT [CK_PatientPatternElements_Location] CHECK (([LocationID] IS NULL AND [LevelOfCareGroupID] IS NOT NULL OR [LocationID] IS NOT NULL AND [LevelOfCareGroupID] IS NULL)) GO

根據您所提供的,我們可以理解的是,插入語句將工作上面的CHECK約束,只有當「那些paramters中的任何一個」,即[LocationID][ LevelOfCareGroupID]爲NULL。但不是BOTH

即,如果[位置ID]是NULL,並且[LevelOfCareGroupID]是NULL。那麼INSERT將失敗

類似地,如果[LocationID]不是NULL,並且[LevelOfCareGroupID]不是NULL。那麼INSERT也會失敗

相關問題