34,576
社区成员
发帖
与我相关
我的任务
分享
tableA --数据表
tableB --监视表,用于监视tableA中数据的插入、修改操作
create trigger trig_A_Upd
ON tableA
FOR INSERT
INSERT INTO tableB something
create trigger tri_tableA_insert_update
on tableA
for insert,update
as
begin
insert into tableB select * from inserted
end
SELECT @Price = dj FROM Inserted
--Select @PrePrice = dj FROM Deleted 注释掉就可以了0
CREATE TABLE TABLEA(ID INT PRIMARY KEY,NAME VARCHAR(10))
CREATE TABLE TABLEB(ID INT IDENTITY(1,1) PRIMARY KEY,NAME VARCHAR(10))
create trigger trig_A_Upd
ON tableA
FOR INSERT,UPDATE
AS
insert into tableB(NAME)Values(1)
--执行1
INSERT TABLEA VALUES(1,'1')
SELECT * FROM TABLEB
/*ID NAME
----------- ----------
1 1
(所影响的行数为 1 行)
*/
--执行2
INSERT TABLEA VALUES(2,'2')
SELECT * FROM TABLEB
/*
ID NAME
----------- ----------
1 1
2 1
(所影响的行数为 2 行)
*/
--执行3
UPDATE TABLEA SET NAME='1'
SELECT * FROM TABLEA
/*ID NAME
----------- ----------
1 1
2 1
(所影响的行数为 2 行)
*/
SELECT * FROM TABLEB
/*ID NAME
----------- ----------
1 1
2 1
3 1
(所影响的行数为 3 行)
*/
--我认为可能需要这样,首先对表tableb建里一个自增字段,以区别重复问题,然后需要设置一个标记字段,以记录是删除、插入、更改三种中的一种。
--大致为:
create trigger my_trig on tableA for insert ,update ,delete
as
if not exists(select 1 from inserted)
insert into tableB(除了自增列的其他字段) select * , '删除' from deleted
else if not exists(select 1 from deleted)
insert into tableB(除了自增列的其他字段) select * , '插入' from inserted
else
insert into tableB(除了自增列的其他字段) select * , '更改' from inserted
go