I created a SQL query w/c has a Delete statement but unfortunately the Delete statement seems not executing. What's wrong with my SQL statement.
Here is my code :
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
Here is the result from Message window :

ERROR_MESSAGE()orERROR_NUMBER()in yourCATCHto find out the actual error : Using TRY...CATCH in Transact-SQLCreditCardMID