Advertisement
Guest User

trg_Prevent_deletion_CreditCard

a guest
Jan 22nd, 2020
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.84 KB | None | 0 0
  1. USE [DataBaseExam2019]
  2. GO
  3. /****** Object: Trigger [dbo].[trg_Prevent_deletion_CreditCard] Script Date: 22/01/2020 12:34:32 ******/
  4. SET ANSI_NULLS ON
  5. GO
  6. SET QUOTED_IDENTIFIER ON
  7. GO
  8. ALTER TRIGGER [dbo].[trg_Prevent_deletion_CreditCard]
  9. ON [dbo].[TCreditCard]
  10. FOR DELETE
  11. AS
  12. BEGIN
  13. DECLARE @Totalpurchase money;
  14. Declare @UserId int = (Select nUserId from deleted)
  15. Declare @CreditCardCount int = (Select Count(*) from TCreditCard Where nUserId = @UserId)
  16. SELECT @Totalpurchase = nTotalPurchase FROM deleted;
  17.  
  18. Print(@UserId)
  19. Print(@CreditCardCount)
  20.  
  21. IF(@Totalpurchase != 0)
  22. Begin
  23. RAISERROR('Records can not be deleted, as user have purchased Item', 10,1)
  24. ROLLBACK TRAN
  25. End
  26.  
  27. IF(@CreditCardCount = 0)
  28. Begin
  29. RAISERROR('Records can not be deleted, as user only have 1 Credit Card', 10,1)
  30. ROLLBACK TRAN
  31. End
  32. END
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement