Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 从多个表复制记录的触发器_Sql_Sql Server_Database_Tsql_Triggers - Fatal编程技术网

Sql 从多个表复制记录的触发器

Sql 从多个表复制记录的触发器,sql,sql-server,database,tsql,triggers,Sql,Sql Server,Database,Tsql,Triggers,我有以下表格: TableStaff UserId, Username, Firstname, Lastname 1001 CLWA Clara Wallace 1002 RITA Ricky Tate 1003 PAEV Pat Evans 表格功能 UserId, Feature, Notes, LogDate 1001 901 Note1 2018-06-15 1002 905 Note111 2018-06-17 1003 903 Note222 2018-06-18 CREATE

我有以下表格:

TableStaff

UserId, Username, Firstname, Lastname
1001 CLWA Clara Wallace
1002 RITA Ricky Tate
1003 PAEV Pat Evans
表格功能

UserId, Feature, Notes, LogDate
1001 901 Note1 2018-06-15
1002 905 Note111 2018-06-17
1003 903 Note222 2018-06-18
CREATE TRIGGER trig_TableFeatures ON TableFeatures
FOR INSERT 
AS
INSERT INTO TableExport (UserId, Feature, Notes)
SELECT UserId, Feature, Notes from Inserted
GO
表格导出

UserId, Username, Firstname, Lastname, Feature, Notes
1001 CLWA Clara Wallace 901 Note1    
1002 RITA Ricky Tate 905 Note111
1003 PAEV Pat Evans 903 Note222
我们有一个前端应用程序,可以从中选择用户和功能,然后将其插入到表格功能中。 每次插入新记录时,我都希望将该新记录插入到表格导出中

每天晚上10:00,计划作业将数据从表格导出到CSV,然后表格被截断

我不知道如何创建一个触发器来复制用户名、名字、姓氏以及用户ID、功能、注释

UserId, Feature, Notes, LogDate
1001 901 Note1 2018-06-15
1002 905 Note111 2018-06-17
1003 903 Note222 2018-06-18
CREATE TRIGGER trig_TableFeatures ON TableFeatures
FOR INSERT 
AS
INSERT INTO TableExport (UserId, Feature, Notes)
SELECT UserId, Feature, Notes from Inserted
GO
我有以下触发器,它只从表功能中复制用户ID、功能、注释

UserId, Feature, Notes, LogDate
1001 901 Note1 2018-06-15
1002 905 Note111 2018-06-17
1003 903 Note222 2018-06-18
CREATE TRIGGER trig_TableFeatures ON TableFeatures
FOR INSERT 
AS
INSERT INTO TableExport (UserId, Feature, Notes)
SELECT UserId, Feature, Notes from Inserted
GO

非常感谢您在这方面提供的任何帮助。

如果我理解正确,您只需加入

CREATE TRIGGER trig_TableFeatures ON TableFeatures
FOR INSERT 
AS
BEGIN
    INSERT INTO TableExport (UserId, Username, Firstname, Lastname, Feature, Notes)
        SELECT i.UserId, s.Username, s.Firstname, s.Lastname, i.Feature, i.Notes 
        FROM Inserted i JOIN
             TableStaff s
             ON i.UserId = s.UserId
END;

Gordon的答案是正确的,你只需要一个连接,因为你提到了错误,我自己在sql Server 2016上尝试过,它对我来说很好。这是代码

   insert into TabelExport
   (userid,username,firstname,
   lastname,feature,notes)
   select  i.userid,ts.username,
   ts.firstname,
   ts.lastname,i.features,
   i.notes 
   from inserted i 
   inner join TableStaff ts 
   on (i.userid=ts.userid);

我建议你不要制造触发器。将数据插入TableFeatures时,也将数据插入TableExport。您拥有所有相关的列,这样可以更容易地插入,更好地进行设计。而不是依靠触发器为您执行插入。我看到的第一件事是,我认为您不需要
TableExport
中的所有列,这是我的说明,对于
触发器
,您可以在
TableFeatures
上创建一个触发器,只需简单的
SELECT
并与
TableStaff
表连接。它抱怨:列名“UserId”不明确。我尝试了TableFeatures.UserId或TableStaff.UserId,这会引发另一个错误“多部分标识符”TableFeatures.UserId“无法绑定”