Anh Hoàng

42 bundles
1 file3 months ago
1

1.

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
1. CREATE TRIGGER TG_CAU01 ON CTPNHAP FOR INSERT, UPDATE AS IF(SELECT SUM(CTPNHAP.SLNhap) FROM CTPNHAP JOIN INSERTED ON CTPNHAP.MAVTu = INSERTED.MaVTu) > (SELECT SUM(SLDat) FROM CTDONDH JOIN INSERTED ON CTDONDH.MaVTu = INSERTED.MaVTu) BEGIN PRINT(N'K duoc lon hon') ROLLBACK TRAN END 3. CREATE TRIGGER TG_CAU03 ON TONKHO FOR INSERT, UPDATE AS UPDATE TONKHO SET SLCUOI = (SELECT TONKHO.SLDAU + TONKHO.TongSLN - TONKHO.TongSLX) FROM TONKHO JOIN INSERTED ON TONKHO.MaVTu = INSERTED.MaVTu AND TONKHO.NAMTHANG = INSERTED.NAMTHANG) BEGIN END 2. CREATE TRIGGER TG_XoaCTDH ON CTHD FOR DELETE AS UPDATE MATHANG SET SoTon = SoTon + SL FROM DELETED WHERE MATHANG.MAMH = DELETED.MAMH CREATE TRIGGER TG_SuatCTHD ON CTHD FOR UPDATE AS DECLARE @D INT SELECT @D = COUNT(*) FROM MATHANG a, DELETED b, INSERTED c WHERE a.MaMH = b.MaMH AND a.MaMH = c.MaMH AND SoTon + b.SL + c.SL < 0 IF(@D > 0) BEGIN PRINT N'Không đủ hàng để bán' ROLLBACK TRAN RETURN END ----------- UPDATE MatHang SET SoTon = SoTon + b.SL - c.SL FROM MatHang a, DELETED b, INSERTED c WHERE a.MaMH = b.MaMH AND a.MaMH = c.MaMH