Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use trigger how to copy just inserted row

I'm using SQL Server 2008, and I have a trigger which I want to copy any rows in the My_Table into a archive History_Table table.

How to copy the entire old content of the table into the archive each time someone inserts a new row?

My table structure is

    CREATE TABLE [dbo].[Stu_Table]
    (
    [Stu_Id] [int] NOT NULL,
    [Stu_Name] [varchar] (15) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [Stu_Class] [int] NULL
    ) ON [PRIMARY]
    GO

ALTER TABLE [dbo].[Stu_Table] ADD CONSTRAINT [PK_Stu_Table] PRIMARY KEY CLUSTERED  ([Stu_Id]) ON [PRIMARY]
GO

My archive table structure is

CREATE TABLE [dbo].[Stu_TableHistory]
(
[Stu_Id] [int] NOT NULL,
[Stu_Name] [varchar] (15) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[Stu_Class] [int] NULL
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[Stu_TableHistory] ADD CONSTRAINT [PK_Stu_TableHistory] PRIMARY KEY CLUSTERED  ([Stu_Id]) ON [PRIMARY]
GO

My trigger syntax is

Create TRIGGER [dbo].[HistoryKeep]
   ON  [dbo].[Stu_Table]
   INSTEAD OF  INSERT
AS 
BEGIN
    IF((SELECT COUNT(*) FROM Stu_Table WHERE Stu_Id = (SELECT Stu_Id FROM INSERTED)) >= 1)
    BEGIN
        INSERT INTO dbo.Stu_TableHistory( Stu_Id, Stu_Name, Stu_Class )
        SELECT Stu_Id, Stu_Name, Stu_Class FROM Stu_Table WHERE Stu_Id = (SELECT Stu_Id FROM INSERTED)

        UPDATE x
        SET x.Stu_Name = i.Stu_Name
        FROM dbo.Stu_Table AS x
        INNER JOIN inserted AS i ON i.Stu_Id = x.Stu_Id

    END
    ELSE
    BEGIN
        INSERT INTO dbo.Stu_Table( Stu_Id, Stu_Name, Stu_Class )
        SELECT Stu_Id, Stu_Name, Stu_Class FROM INSERTED
    END
END

In a word need help to transfer the old data from student table to archive table. My above trigger syntax can not satisfy me.

If have any query plz ask thanks in advance.

like image 371
shamim Avatar asked Dec 05 '25 06:12

shamim


1 Answers

Instead of your current trigger, you should have something like:

Create TRIGGER [dbo].[HistoryKeep]
   ON  [dbo].[Stu_Table]
   INSTEAD OF  INSERT
AS 
BEGIN
DECLARE @History table (
    Action sysname not null,
    STU_ID [int] NULL,
    [Stu_Name] [varchar] (15) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [Stu_Class] [int] NULL
)

;MERGE INTO Stu_Table t
USING INSERTED i ON t.STU_ID = i.STU_ID
WHEN MATCHED THEN UPDATE SET STU_Name = i.STU_Name
WHEN NOT MATCHED THEN INSERT (STU_ID,STU_NAME,STU_CLASS) VALUES (i.STU_ID,i.STU_NAME,i.STU_CLASS)
OUTPUT $Action,deleted.stu_id,deleted.stu_name,deleted.stu_class INTO @History;

INSERT INTO stu_TableHistory (stu_id,stu_name,stu_class)
select stu_id,stu_name,stu_class from @History where Action='UPDATE'
END

Note, also, that you'll need to drop your current PK constraint on STU_TableHistory, since as soon as a row is updated more than once, there'll be two entries containing the same STU_ID.


As per my comment, this treats INSERTED as a table containing multiple rows throughout. So if Stu_Table contains a row for STU_ID 1, the following insert:

INSERT INTO STU_Table (STU_ID,STU_Name,STU_Class) VALUES
(1,'abc',null),
(2,'def',null)

will update the row for STU_ID 1, insert a row for STU_ID 2, and insert one row into stu_tableHistory (for STU_ID 1)

like image 92
Damien_The_Unbeliever Avatar answered Dec 07 '25 22:12

Damien_The_Unbeliever



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!