我有两张桌子Employee
和Emp_Audit
.
在 table 上Employee
,我有一个AFTER INSERT
触发器,当我将行插入Employee
. 触发器的作用是将Emp_Audit
已经插入的行插入到表中Employee
。
当我为要插入的每条记录明确使用“插入值”时,触发器工作Employee
正常
INSERT INTO Employee_Test VALUES ('Anees',1000);
INSERT INTO Employee_Test VALUES ('Rick',1200);
INSERT INTO Employee_Test VALUES ('John',1100);
INSERT INTO Employee_Test VALUES ('Stephen',1300);
INSERT INTO Employee_Test VALUES ('Maria',1400);
触发器插入所有这些行 inti Emp_Audit
-------------GOOD
但是当我使用值构造函数作为
insert into dbo.Employee_Test
values ('Kritika', 25000),
('Ritu', 15000),
('Maduri', 7000),
('Dinkar', 7000);
只有第一行('Kritika', 25000)
被插入Emp_Audit
整个查询如下:
CREATE TABLE Employee_Test
(
Emp_ID INT Identity,
Emp_name Varchar(100),
Emp_Sal Decimal (10,2)
)
CREATE TABLE Employee_Test_Audit
(
Emp_ID int,
Emp_name varchar(100),
Emp_Sal decimal (10,2),
Audit_Action varchar(100),
Audit_Timestamp datetime
)
-----------------------Trigger------------------------------------
CREATE TRIGGER trgInsertAfter ON [dbo].[Employee_Test]
FOR INSERT
AS
declare @empid int;
declare @empname varchar(100);
declare @empsal decimal(10,2);
declare @audit_action nvarchar(200);
select @empid = inserted.Emp_ID
FROM inserted;
select @empname = inserted.Emp_name
from inserted;
select @empsal = inserted.Emp_Sal
from inserted;
set @audit_action = 'Record Inserted after Insert Trigger Fired';
INSERT INTO Employee_Test_Audit
VALUES(@empid, @empname, @empsal, @audit_action, GETDATE());
GO
print('Insert trigger FIRED')
insert into dbo.Employee_Test
values ('Kritika', 25000),
('Ritu', 15000),
('Maduri', 7000),
('Dinkar', 7000);