使用2个不同数据库中的相关表触发更新

时间:2016-01-20 17:03:27

标签: sql sql-server-2008 triggers

我正在尝试编写一个触发器,将db1中的表格更新为db2

中的表格

这样的关系;

ALTER TRIGGER [dbo].[TR_PERSONAS] 
ON [dbo].[Personas]
AFTER UPDATE
AS
BEGIN
     UPDATE [AXData].[dbo].[TEmployee]
     SET
        [EmployeeCode] = [Numero_Documento],
        [EmployeeName] = [Nombres],
        [EnglishName] = [Apellidos],
        [CardNo] = [Codigo],
        [pin] = 1234,
        [EmpEnable] = [Estado],
        [Sex] = 'false',
        [Birthday] = CAST(0x0000890400000000 AS DATETIME),
        [Deleted] = 0,
        [Leave] = 0,
        [BeKQ] = 1,
        [MapVisible] = 1,
        [ShowCardNo] = 1,
        [RegDate] = [Fecha_Hora],
        [EndDate] = [FechaVence],
        [Status] = [Estado],
        [DeptID] = [Tipo],
        [JobID] = [Id_Centro_Costos],
        [TimeStamp] = GETDATE()
     FROM 
        [AXData].[dbo].[TEmployee]
     INNER JOIN 
        [ControlSuite].[dbo].[Personas] ON [ControlSuite].[dbo].[Personas].[Id_Persona] = [AXData].[dbo].[TEmployee].[EmployeeID]
     WHERE 
        [AXData].[dbo].[TEmployee].[EmployeeID] = updated.[Personas].[Id_Persona];
 END;

但是我收到了这个错误:

  

无法绑定多部分标识符“updated.Personas.Id_Persona”

我做错了什么?

2 个答案:

答案 0 :(得分:1)

没有UPDATED表只有INSERTED和DELETED。

试试这个:

ALTER TRIGGER [dbo].[TR_PERSONAS] ON [dbo].[Personas]
AFTER UPDATE
AS
 BEGIN

     UPDATE [AXData].[dbo].[TEmployee]
       SET
           [EmployeeCode] = [Numero_Documento],
           [EmployeeName] = [Nombres],
           [EnglishName] = [Apellidos],
           [CardNo] = [Codigo],
           [pin] = 1234,
           [EmpEnable] = [Estado],
           [Sex] = 'false',
           [Birthday] = CAST(0x0000890400000000 AS DATETIME),
           [Deleted] = 0,
           [Leave] = 0,
           [BeKQ] = 1,
           [MapVisible] = 1,
           [ShowCardNo] = 1,
           [RegDate] = [Fecha_Hora],
           [EndDate] = [FechaVence],
        [Status]=[Estado],
        [DeptID]=[Tipo],
        [JobID]=[Id_Centro_Costos],
           [TimeStamp] = GETDATE()
     FROM [AXData].[dbo].[TEmployee]
          INNER JOIN [ControlSuite].[dbo].[Personas] ON [ControlSuite].[dbo].[Personas].[Id_Persona] = [AXData].[dbo].[TEmployee].[EmployeeID]
     WHERE [AXData].[dbo].[TEmployee].[EmployeeID] IN ( SELECT i.[Id_Persona] FROM  inserted AS i INNER JOIN deleted AS d ON i.[Id_Persona] = d.[Id_Persona]) ;
 END;

答案 1 :(得分:0)

SQL Server触发器上下文中的伪表称为

  • Deleted:包含已删除的行(DELETE触发器中)或UPDATE触发器中行的旧值

  • Inserted:包含已插入的行(INSERT触发器中)或UPDATE触发器中行的新值

没有updated伪表