2012-09-26 118 views
0

我在SQL Server 2008中有一個存儲過程,需要刪除幾行數據。但是當我運行它時,它會返回一個失敗並且值爲-6。SQL Server存儲過程返回'失敗'

ALTER procedure [dbo].[p_CaseFiles_Exhibits_DeleteExhibits] 
    @ExhibitID int 
, @Message nvarchar(50) output 
as 
declare @FileID int 
set @FileID = (select FileID from CaseFileExhibits where ExhibitID = @ExhibitID) 
begin transaction 
    begin try 
    delete from CaseFileExhibitMovementTracking where ExhibitID = @ExhibitID 
    delete from CaseFileExhibitAttachments where CaseFileExhibitID = @ExhibitID 
    delete from CaseFileExhibits where ExhibitID = @ExhibitID 
    delete from CaseFileExhibitPropertyLink where ExhibitID = @ExhibitID 
    update CaseFileQuickStats set ExhibitCount = ExhibitCount -1 where CaseFileID = @FileID  
    commit transaction 
    end try 
    begin catch 
    set @Message='Fail' 
    rollback transaction 
    end catch 

我似乎無法找到問題所在。

+0

對不起。更改了它 – user1084319

+0

您是否嘗試單獨運行每個查詢的存儲過程以查看哪個查詢可能失敗?或者甚至在存儲過程之外單獨運行每一個? – Taryn

+1

您應該使用** TAGS **這種「meta」信息 - 我現在爲您更新了 –

回答

3

你能查出來消息自己,將它添加到您的CATCH塊:

SELECT 
ERROR_NUMBER() AS ErrorNumber 
     ,ERROR_SEVERITY() AS ErrorSeverity 
     ,ERROR_STATE() AS ErrorState 
     ,ERROR_PROCEDURE() AS ErrorProcedure 
     ,ERROR_LINE() AS ErrorLine 
     ,ERROR_MESSAGE() AS ErrorMessage; 

你可能想改變這種狀況選擇打印,然後你就可以看到結果在SSMS內運行SP時,在'消息'選項卡中。

我懷疑這是一個外鍵問題或可能的觸發器。

+0

是的非常感謝。我知道CaseFileExhibitLinkProperty有一個外鍵約束,並且因爲它在衝突後有它,它不起作用。我只是把「從CaseFileExhibitPropertyLink刪除,其中ExhibitID = @ExhibitID」作爲第一個刪除,然後它不違反約束,它的工作原理。 – user1084319