2015-04-18 110 views
0

我創建了一個SQL查詢W/C具有Delete聲明可惜Delete聲明似乎不執行。我的SQL語句有什麼問題。的SQL Server不執行DELETE語句

這是我的代碼:

BEGIN TRAN 
BEGIN TRY 
DECLARE @Mid VARCHAR(100) = '100303000' 
DECLARE @Cnt INT 
DECLARE @ScHeader TABLE 
(
    ScHeaderId UNIQUEIDENTIFIER 
) 
declare @SCHeaderId uniqueidentifier 
    -- get ids to be deleted 
    INSERT INTO @ScHeader(ScHeaderId) 
    SELECT scd.ServiceChargesId 
    FROM ServiceChargeDetails scd 
    INNER JOIN ServiceCharges sc ON scd.ServiceChargesId = sc.ServiceChargesId 
    INNER JOIN CreditCardMID cc ON cc.CreditCardMIDId = scd.CreditCardMIDId 
    WHERE cc.MID = @Mid; 
     /* 
      do some other task here Related to @ScHeaderId 
     */ 

    SELECT @Cnt = COUNT(*) FROM CreditCardMID WHERE MID = @Mid; 
    Print 'Count before delete ' + Cast(@Cnt as VARCHAR(10)) 
    PRINT 'Deleting Mid ' + @Mid; 
    DELETE FROM CreditCardMID WHERE MID = @Mid; -- not executed 
    PRINT 'Done deleting mid '; -- -- not executed 
    SELECT @Cnt = COUNT(*) FROM CreditCardMID WHERE MID = @Mid;-- not executed 
    Print 'Count after delete ' + Cast(@Cnt as VARCHAR(10)) -- not executed 
    COMMIT TRAN 
END TRY 
BEGIN CATCH 
    ROLLBACK TRAN 
    PRINT @@ERROR 
END CATCH 

下面是從消息窗口中的結果:

enter image description here

+2

也許嘗試打印'ERROR_MESSAGE()'或'ERROR_NUMBER()'在'CATCH'找出實際的錯誤:使用TRY ...在Transact-SQL CATCH(https://technet.microsoft.com/en-us/library/ms179296%28v= sql.105%29.aspx) – har07

+1

查找觸發器'CreditCardMID' –

+0

觸發器花費我一段時間。謝謝A –

回答

3

的 「0」 之後出現 「(受影響0行(或多個))」意味着刪除不起作用,並觸發了口號。你能不抓運行該代碼(內部的begin tran /回滾TRAN)對,看看是什麼錯誤(我相信它從CreditCardMID表觸發錯誤)

+0

我發現觸發器W/C限制行刪除基於一定的條件。感謝user2818323。 –

+0

Upvote!更多upvotes有用的答案! –