SQL code
1 2 3 4 5 6 7 8 9 10 11 12 13 | CREATE TRIGGER [checkid] ON [dbo].[计0261] INSTEAD OF insert AS IF NOT EXISTS(SELECT * FROM 计026 WHERE 学号=(SELECT 学号 FROM INSERTED)) BEGIN ROLLBACK TRANSACTION PRINT '要处理记录的学号不存在!' END ELSE BEGIN INSERT INTO 计0261 select * from inserted PRINT '已经成功处理记录!' END |
SQL code
1 2 3 4 5 6 7 8 9 10 11 12 13 | CREATE TRIGGER [orderinsert] ON [dbo].[订货表] AFTER INSERT AS DECLARE @bookid int, @ordernum int, @num int SELECT @bookid = 书籍编号, @ordernum = 数量 FROM INSERTED SELECT @num = count(书籍编号) FROM 订货统计表 WHERE 书籍编号=@bookid IF @num = 0 --未找到该书,插入记录 INSERT INTO 订货统计表 VALUES(@bookid, @ordernum) ELSE --找到该书,更新记录 UPDATE 订货统计表 SET 总订货量 = 总订货量 + @ordernum WHERE 书籍编号 = @bookid |