2017-03-06 46 views
1

爲什麼這個SP返回TRUE,即使我提到如果某些條件滿足,那麼它應該提高錯誤,但它仍然返回true。我已經檢查了條件,它正在開會,但仍然如此,爲什麼?爲什麼我的存儲過程返回true,即使它引發錯誤?

SP:

ALTER PROC [dbo].[usp_UpdateWithdrawal] 
(
    @NITNo int, 
    @WorkNo int, 
    @ContractorID int, 
    @IsWithDrawn bit 
) 

AS 
BEGIN 
    Begin Try 

    Begin Transaction 

     Declare @ExpiryDate date 
     Declare @ExpiryTime time(7) 

     Set @ExpiryDate= (Select convert(varchar(11), ExpiryDate, 106) from Works where NIT_No= @NITNo and WorkNo= @WorkNo) 
     Set @ExpiryTime= (Select CAST(ExpiryTime as TIME(0)) from Works where NIT_No= @NITNo and WorkNo= @WorkNo) 

     IF(CONVERT(varchar(11), @ExpiryDate, 106)+' '+Convert(varchar(8),CAST(@ExpiryTime as TIME(0))) < CONVERT(varchar(11), Getdate(), 106)+' '+Convert(varchar(8),CAST(Getdate() as TIME(0))))  
     Begin 
      RAISERROR('Sorry, Expiry Date and Time has passed', 16, 10); 
      return; 
     End 

     Update AppliedWorks 
     Set Withdrawn= @IsWithDrawn 
     Where NIT_No= @NITNo and Work_No= @WorkNo and Contractor_ID= @ContractorID and 
     CONVERT(varchar(11), @ExpiryDate, 106)+' '+Convert(varchar(8),CAST(@ExpiryTime as TIME(0))) < CONVERT(varchar(11), Getdate(), 106)+' '+Convert(varchar(8),CAST(Getdate() as TIME(0))) 

     --Update Hisotry 

     Declare @AppliedWorkID int 
     Set @AppliedWorkID= (Select AppliedWorkID from AppliedWorks 
          Where NIT_No= @NITNo and Work_No= @WorkNo and Contractor_ID= @ContractorID) 


       Insert into AppliedWorkHistory 
       (
        AppliedWork_ID, 
        FinancialMoney, 
        AboveBelow, 
        EntryDateTime, 

        TwoPercentAmount, 
        TwoPercentBankName, 
        TwoPercentDemandDaftNo, 
        TwoPercentDemandDaftValue, 
        TwoPercentRemarks, 

        EightPercentAmount, 
        EightPercentBankName, 
        EightPercentDemandDaftNo, 
        EightPercentDemandDaftValue, 
        EightPercentRemarks, 
        IsFinalized, 
        Withdrawn 
       ) 
       Select @AppliedWorkID, aw.FinancialMoney, aw.AboveBelow, Getdate(), aw.TwoPercentAmount, aw.TwoPercentBankName, aw.EightPercentDemandDaftNo, 
         aw.TwoPercentDemandDaftValue, aw.TwoPercentRemarks, aw.EightPercentAmount, aw.EightPercentBankName, 
         aw.EightPercentDemandDaftNo, aw.EightPercentDemandDaftValue, aw.EightPercentRemarks, aw.IsFinalized, @IsWithDrawn 
       from AppliedWorks aw where aw.AppliedWorkID= @AppliedWorkID 



    Commit Transaction 

    End Try 
    Begin Catch 
       Rollback transaction 
    End Catch 


END; 

C#:

公共靜態布爾UpdateWithdrawal(INT NitNo,INT WorkNo,INT ContractorID,布爾IsWithDrawn) { 布爾結果;

ClsDatabaseManager dbManager = ClsDatabaseManager.InitializeDbManager(Constants.Databases.TendersConnection); 
try 
{ 
    dbManager.CreateParameters(4); 
    dbManager.AddParameters(0, "@NITNo", NitNo); 
    dbManager.AddParameters(1, "@WorkNo", WorkNo); 
    dbManager.AddParameters(2, "@ContractorID", ContractorID); 
    dbManager.AddParameters(3, "@IsWithDrawn", IsWithDrawn); 
    dbManager.Open(); 
    Result = dbManager.ExecuteNonQuery("usp_UpdateWithdrawal", CommandType.StoredProcedure).ToBool(); 
} 
catch (Exception ex) 
{ 
    throw ex; 
} 
finally 
{ 
    dbManager.Close(); 
    dbManager.Dispose(); 
} 

return Result; 

}

+3

您在'TRY..CATCH'中引發錯誤,並且沒有在'CATCH'中處理它,您需要將錯誤拋入您的'CATCH'部分以及 – Stephen

+0

。在答案框中寫下你的留言,這樣我就可以標記它 – Covert

回答

2

正如發表評論。

如果您在TRY..CATCH內發生錯誤,並且在CATCH中未處理該錯誤,則需要將錯誤也列入您的CATCH部分。

1

取出放置在RAISERROR語句後return聲明。
因爲如果使用tryCatch塊,Error會拋出catch塊。
所以在raiseError之後,在您的存儲過程中刪除return語句。

+0

是的,但不能正常工作 – Covert

相關問題