首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

求触发器,表的备份解决思路

2012-04-09 
求触发器,表的备份职员信息表:CREATETABLE[employee]([employee_id][numeric](18,0)IDENTITY(1,1)NOTNULL,

求触发器,表的备份
职员信息表:
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

热点排行