首先我用这个脚本创建一个表:
CREATE TABLE [dbo].[Employee_Demo] (
[Emp_ID] [bigint] IDENTITY (1, 1) NOT NULL ,
[Emp_Name] [varchar] (55) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[Emp_Sal] [decimal](10, 2) NULL
) ON [PRIMARY]
然后我创建另一个表
CREATE TABLE [dbo].[Employee_Demo_Audit] (
[Emp_ID] [int] NULL ,
[Emp_Name] [varchar] (55) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[Emp_Sal] [decimal](10, 2) NULL ,
[Audit_Action] [varchar] (100) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[Audit_Timestamp] [datetime] NULL
) ON [PRIMARY]
之后,我INSTEAD OF UPDATE
在表上创建了一个触发器employee_demo
:
CREATE TRIGGER trgInsteadOfUpdate ON dbo.Employee_Demo
INSTEAD OF Update
AS
declare @emp_id int, @emp_name varchar(55), @emp_sal decimal(10,2), @audit_action varchar(100);
select @emp_id=i.Emp_ID from inserted i;
select @emp_name=i.Emp_Name from inserted i;
select @emp_sal=i.Emp_Sal from inserted i;
IF UPDATE (Emp_sal)
begin
BEGIN
BEGIN TRAN
if(@emp_sal>=1000)
begin
RAISERROR('Cannot Insert where salary fewer then 1000',16,1); ROLLBACK; end
else begin
insert into Employee_Demo_Audit(Emp_ID,Emp_Name,Emp_Sal,Audit_Action,Audit_Timestamp) values(@emp_id,@emp_name,@emp_sal,@audit_action,getdate());
COMMIT;
PRINT 'Record Updated -- Instead Of Update Trigger.'; END; end
end
我不知道为什么我employee_demo
用这个脚本更新表:
UPDATE employee_demo
SET emp_name = 'ZHEE'
WHERE emp_id = 1
名字emp_id = 1
不变...
为什么 ????