まず、このスクリプトでテーブルを作成します:
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
変わりません...
どうして ????