Skip to content

Instantly share code, notes, and snippets.

@behitek
Last active December 24, 2016 10:32
Show Gist options
  • Save behitek/600f578e7a10c3890734d9a3af2548ed to your computer and use it in GitHub Desktop.
Save behitek/600f578e7a10c3890734d9a3af2548ed to your computer and use it in GitHub Desktop.

Cập nhật số lượng sách khi thay đổi trên tb_CTPN

1

ALTER TRIGGER [dbo].[tg_DeleteCTPN]
   ON  [dbo].[tb_CTPN]
   For delete
AS 
BEGIN
	Declare @soLuong int
	select @soLuong = deleted.soLuong from deleted
	declare @ma_Sach nvarchar(50)
	select @ma_Sach = deleted.maSach from deleted
	if @soLuong >0 and @soLuong < (select soLuongTon from tb_Sach where tb_Sach.maSach = @ma_Sach)
	Begin
		update tb_Sach
		set soLuongTon = soLuongTon - @soLuong
		where tb_Sach.maSach = @ma_Sach
	End
END

2

ALTER TRIGGER [dbo].[tg_InsertCTPN]
   ON  [dbo].[tb_CTPN]
   For Insert
AS 
BEGIN
	Declare @soLuong int
	select @soLuong = inserted.soLuong from inserted
	declare @ma_Sach nvarchar(50)
	select @ma_Sach = inserted.maSach from inserted
	if @soLuong > 0
	Begin
		update tb_Sach
		set soLuongTon = soLuongTon + @soLuong
		where tb_Sach.maSach = @ma_Sach
	End
END
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment