SQL 触发器在插入时将新记录添加到具有相同结构的 table
SQL trigger to add new records to a table with the same structure when an insertion is made
我正在尝试创建 SQL 触发器,它将新记录添加到通过网页进行插入的同一 table。我不确定如何实现它,但我尝试了以下查询
CREATE trigger [dbo].[trgI_DealsDoneInserRecord]
on [dbo].[Terms]
after insert
As
Insert into DealsDone
(Company,Grade,Term,Pipeline,[Index],Volume,Price,[Type],CounterParty,
TermID,GradeID,CPID,Locked,Product)
VALUES
(SELECT Company,Grade,Term,Pipeline,[Index],Volume,Price,[Type],CounterParty,
TermID,GradeID,CPID,Locked,Product FROM inserted)
END
上述查询在 VALUES
中的 SELECT
语句中抛出错误。
我可以知道实现这个的方法吗?
存在语法问题,而且您还缺少 BEGIN
基本语法是
INSERT INTO table2 (column_name(s))
SELECT column_name(s)
FROM table1;
所以试试这个
CREATE trigger [dbo].[trgI_DealsDoneInserRecord]
on [dbo].[Terms]
after insert
As
BEGIN
Insert into DealsDone
(Company,Grade,Term,Pipeline,[Index],Volume,Price,[Type],CounterParty,
TermID,GradeID,CPID,Locked,Product)
SELECT Company,Grade,Term,Pipeline,[Index],Volume,Price,[Type],CounterParty,
TermID,GradeID,CPID,Locked,Product
FROM inserted
END
参考:- http://technet.microsoft.com/en-us/library/ms188263(v=sql.105).aspx
试试这个:
CREATE trigger [dbo].[trgI_DealsDoneInserRecord]
ON [dbo].[Terms]
AFTER INSERT
As
BEGIN
INSERT INTO DealsDone
(Company,Grade,Term,Pipeline,[Index],Volume,Price,[Type],CounterParty,
TermID,GradeID,CPID,Locked,Product)
SELECT Company,Grade,Term,Pipeline,[Index],Volume,Price,[Type],CounterParty,
TermID,GradeID,CPID,Locked,Product FROM inserted
END
虽然我通常提倡反对使用SELECT *
,但在这种情况下似乎有好处:
通过不指定字段,您可以自动考虑表中的更改,而无需在添加或删除甚至重命名字段时更新此触发器。
如果其中一个表已更新但另一个表未更新且结构不同,这将帮助您捕获架构更新中的错误。如果发生这种情况,INSERT 操作将失败,您不必担心清理坏数据。
所以使用这个:
CREATE TRIGGER [dbo].[trgI_DealsDoneInserRecord]
ON [dbo].[Terms]
AFTER INSERT
AS
SET NOCOUNT ON;
INSERT INTO [DealsDone]
SELECT *
FROM inserted;
我正在尝试创建 SQL 触发器,它将新记录添加到通过网页进行插入的同一 table。我不确定如何实现它,但我尝试了以下查询
CREATE trigger [dbo].[trgI_DealsDoneInserRecord]
on [dbo].[Terms]
after insert
As
Insert into DealsDone
(Company,Grade,Term,Pipeline,[Index],Volume,Price,[Type],CounterParty,
TermID,GradeID,CPID,Locked,Product)
VALUES
(SELECT Company,Grade,Term,Pipeline,[Index],Volume,Price,[Type],CounterParty,
TermID,GradeID,CPID,Locked,Product FROM inserted)
END
上述查询在 VALUES
中的 SELECT
语句中抛出错误。
我可以知道实现这个的方法吗?
存在语法问题,而且您还缺少 BEGIN
基本语法是
INSERT INTO table2 (column_name(s))
SELECT column_name(s)
FROM table1;
所以试试这个
CREATE trigger [dbo].[trgI_DealsDoneInserRecord]
on [dbo].[Terms]
after insert
As
BEGIN
Insert into DealsDone
(Company,Grade,Term,Pipeline,[Index],Volume,Price,[Type],CounterParty,
TermID,GradeID,CPID,Locked,Product)
SELECT Company,Grade,Term,Pipeline,[Index],Volume,Price,[Type],CounterParty,
TermID,GradeID,CPID,Locked,Product
FROM inserted
END
参考:- http://technet.microsoft.com/en-us/library/ms188263(v=sql.105).aspx
试试这个:
CREATE trigger [dbo].[trgI_DealsDoneInserRecord]
ON [dbo].[Terms]
AFTER INSERT
As
BEGIN
INSERT INTO DealsDone
(Company,Grade,Term,Pipeline,[Index],Volume,Price,[Type],CounterParty,
TermID,GradeID,CPID,Locked,Product)
SELECT Company,Grade,Term,Pipeline,[Index],Volume,Price,[Type],CounterParty,
TermID,GradeID,CPID,Locked,Product FROM inserted
END
虽然我通常提倡反对使用SELECT *
,但在这种情况下似乎有好处:
通过不指定字段,您可以自动考虑表中的更改,而无需在添加或删除甚至重命名字段时更新此触发器。
如果其中一个表已更新但另一个表未更新且结构不同,这将帮助您捕获架构更新中的错误。如果发生这种情况,INSERT 操作将失败,您不必担心清理坏数据。
所以使用这个:
CREATE TRIGGER [dbo].[trgI_DealsDoneInserRecord]
ON [dbo].[Terms]
AFTER INSERT
AS
SET NOCOUNT ON;
INSERT INTO [DealsDone]
SELECT *
FROM inserted;