日期:2014-05-18  浏览次数:20464 次

求触发器,表的备份
职员信息表:
CREATE   TABLE   [employee](
[employee_id]   [numeric](18,   0)   IDENTITY(1,1)   NOT   NULL,
[employee_name]   [nvarchar](50)   NOT   NULL,
[mailbox]   [nvarchar](50)     NOT   NULL,
[password]   [nvarchar](50)   NOT   NULL,
[sex]   [int]   not   null,
  CONSTRAINT   [PK_employee]   PRIMARY   KEY([employee_id]))

历史表:
CREATE   TABLE   [employee_history](
[employee_id]   [numeric](18,   0)   IDENTITY(1,1)   NOT   NULL,
[employee_name]   [nvarchar](50)   NOT   NULL,
[mailbox]   [nvarchar](50)     NOT   NULL,
[password]   [nvarchar](50)   NOT   NULL,
[sex]   [int]   not   null,
[changedate]   datetime,

每次职员表的修改操作将修改前的数据保存在历史表中,仅添加了一个修改时间列。
环境:Microsoft   SQL   Server   2005

------解决方案--------------------
触发器语句中使用了两种特殊的表:deleted 表和 inserted 表
Deleted 表用于存储 DELETE 和 UPDATE 语句所影响的行的复本。在执行 DELETE 或 UPDATE 语句时,行从触发器表中删除,并传输到 deleted 表中。Deleted 表和触发器表通常没有相同的行。
------解决方案--------------------
历史表employee_id也是自增的啊?
试试
create trigger tu_employee on employee
for update
as
insert into employee_history(employee_name,mailbox,password,sex,changedate)
select employee_name,mailbox,password,sex,getdate()
from deleted
------解决方案--------------------
CREATE TRIGGER tr1 ON employee FOR insert,update,delete AS
delete * from employee_history
insert into employee_history select * from employee