加入多个 table Sql 触发器
Joining multiple table Sql trigger
嗨,我是 SQL 触发器的新手。因为我在网上尝试和搜索,所以我没有找到任何明确的结果。
所以这是我的问题。
我有三个 tables:
表 1 :
ID NAME (columns )
1 prabhu
表 2 :
Id COUNTRY (columns )
1 India
如果 table2 中发生类似 insert/update 的事情,我希望将其发送到日志 table
SQL(DB2) 触发器必须执行以下操作,结果应该在日志 table 中,如下所示
LOGTABLE:
ID NAME COUNTRY
1 prabhu India
非常感谢您的帮助。
试试这个,
-- Create tables
create table table1(id int, empName varchar(20));
create table table2(id int, country varchar(20));
create table logtable(id int, empName varchar(20), country varchar(20));
-- Create trigger
CREATE TRIGGER logtableAfterInsert ON table2
after INSERT,DELETE,UPDATE
AS
BEGIN
declare @empid int;
declare @empname2 varchar(20);
declare @empcountry varchar(20);
select @empid=i.id from inserted i;
select @empcountry=i.country from inserted i;
select @empname2=tbl1.empName from table1 tbl1 where tbl1.id=@empid;
insert into logtable values(@empid,@empname2,@empcountry);
PRINT 'Inserted'
END
GO
之后插入值,
insert into table1 values(1, 'prabhu');
insert into table2 values (1, 'India');
查看结果,
select * from table1;
select * from table2;
select * from logtable;
希望这能解决...
顺便说一句,您需要添加外键约束。
CREATE OR REPLACE TRIGGER logtableAfterUpdate
AFTER UPDATE ON table2
REFERENCING NEW AS NAUDIT OLD AS OAUDIT
FOR EACH ROW MODE DB2SQL
--BEGIN --ATOMIC
insert into logtable
values(
(select id from table2 tbl2 where tbl2.id =OAUDIT.id),
(select empName from table1 tbl1 where tbl1.id=(select id from table2 tbl2 where tbl2.id =OAUDIT.id)),
(select country from table2 tbl2 where tbl2.id =OAUDIT.id)
);
--END;
嗨,我是 SQL 触发器的新手。因为我在网上尝试和搜索,所以我没有找到任何明确的结果。 所以这是我的问题。 我有三个 tables:
表 1 :
ID NAME (columns )
1 prabhu
表 2 :
Id COUNTRY (columns )
1 India
如果 table2 中发生类似 insert/update 的事情,我希望将其发送到日志 table SQL(DB2) 触发器必须执行以下操作,结果应该在日志 table 中,如下所示
LOGTABLE:
ID NAME COUNTRY
1 prabhu India
非常感谢您的帮助。
试试这个,
-- Create tables
create table table1(id int, empName varchar(20));
create table table2(id int, country varchar(20));
create table logtable(id int, empName varchar(20), country varchar(20));
-- Create trigger
CREATE TRIGGER logtableAfterInsert ON table2
after INSERT,DELETE,UPDATE
AS
BEGIN
declare @empid int;
declare @empname2 varchar(20);
declare @empcountry varchar(20);
select @empid=i.id from inserted i;
select @empcountry=i.country from inserted i;
select @empname2=tbl1.empName from table1 tbl1 where tbl1.id=@empid;
insert into logtable values(@empid,@empname2,@empcountry);
PRINT 'Inserted'
END
GO
之后插入值,
insert into table1 values(1, 'prabhu');
insert into table2 values (1, 'India');
查看结果,
select * from table1;
select * from table2;
select * from logtable;
希望这能解决...
顺便说一句,您需要添加外键约束。
CREATE OR REPLACE TRIGGER logtableAfterUpdate
AFTER UPDATE ON table2
REFERENCING NEW AS NAUDIT OLD AS OAUDIT
FOR EACH ROW MODE DB2SQL
--BEGIN --ATOMIC
insert into logtable
values(
(select id from table2 tbl2 where tbl2.id =OAUDIT.id),
(select empName from table1 tbl1 where tbl1.id=(select id from table2 tbl2 where tbl2.id =OAUDIT.id)),
(select country from table2 tbl2 where tbl2.id =OAUDIT.id)
);
--END;