SQL Server不执行Delete语句

时间:2015-04-18 07:27:55

标签: sql sql-server

我创建了一个SQL查询w / c有一个Delete语句,但不幸的是Delete语句似乎没有执行。我的SQL语句出了什么问题。

这是我的代码:

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

1 个答案:

答案 0 :(得分:3)

" 0"出现在"(0行(s)受影响)"意味着删除没有奏效,并触发了口号。你可以在没有catch(在开始tran / rollback tran内)对的情况下运行这段代码来查看错误是什么(我认为它来自CreditCardMID表上的触发器的错误)