0

我需要将表 Table_A 中已删除的行插入到 Table_B 中,这些行具有相同的结构
(类似于回收站),但我在 SQL SERVER 触发器方面没有太多经验

CREATE TRIGGER dbo.Table_A_Delete_Instead_Of_Trigger
ON dbo.Table_A
INSTEAD OF DELETE
AS
BEGIN    
     INSERT INTO Table_B 
     SELECT T 

     DELETE T                  
END

请饶恕我的经验之湖。谢谢。

4

2 回答 2

6

您不需要“INSTEAD OF”触发器,您需要“AFTER”触发器。

给定一个表 TableA...

CREATE TABLE [dbo].[TableA](
    [Col1] [varchar](50) NOT NULL,
    [Col2] [varchar](50) NOT NULL
) ON [PRIMARY]

和相应的TableB ...

CREATE TABLE [dbo].[TableB](
    [Col1] [varchar](50) NOT NULL,
    [Col2] [varchar](50) NOT NULL
) ON [PRIMARY]

然后下面的触发器会做你想做的事:

CREATE TRIGGER trg_TableA_Archive 
ON  TableA
AFTER DELETE
AS 

    INSERT INTO TableB
    SELECT *
    FROM deleted
GO
于 2012-10-16T14:17:41.627 回答
2

尝试这个:

CREATE TRIGGER dbo.Table_A_Delete_Instead_Of_Trigger
ON dbo.Table_A
AFTER DELETE
AS
BEGIN    
     INSERT INTO Table_B 
     SELECT * FROM 
     DELETED;          
END

我认为在这种情况下你不需要一个代替触发器。

于 2012-10-16T14:06:31.257 回答