我創建了一個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
下面是從消息窗口中的結果:
也許嘗試打印'ERROR_MESSAGE()'或'ERROR_NUMBER()'在'CATCH'找出實際的錯誤:使用TRY ...在Transact-SQL CATCH(https://technet.microsoft.com/en-us/library/ms179296%28v= sql.105%29.aspx) – har07
查找觸發器'CreditCardMID' –
觸發器花費我一段時間。謝謝A –