Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9140131
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:28:34+00:00 2026-06-17T09:28:34+00:00

I’m using SQL Server 2008, and I have a trigger which I want to

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T09:28:35+00:00Added an answer on June 17, 2026 at 9:28 am

    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)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
I have an autohotkey script which looks up a word in a bilingual dictionary
I have an array which has BIG numbers and small numbers in it. I
I have a text area in my form which accepts all possible characters from
I have thousands of HTML files to process using Groovy/Java and I need to
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I am using JSon response to parse title,date content and thumbnail images and place

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.