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
|
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
|