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

  • SEARCH
  • Home
  • 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 8233901
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T18:17:23+00:00 2026-06-07T18:17:23+00:00

I have a problem with the programming of an Update trigger. I want to

  • 0

I have a problem with the programming of an Update trigger. I want to create a trigger that copies the row I edited in a new row with the new data and a new ID. The old row should be the same with just a flag change from 0 to 1.

The table look like this:

ID      Artikelname  PREIS        UPDATE_DATE          FLAG
1       Tomatoe      3            14.06.2012 16:00     0
2       Apple        1,5          12.05.2012 14:45     0

When I change the price of the first row, the table should look like this:

ID      Artikelname  PREIS        UPDATE_DATE          FLAG
1       Tomatoe      3            14.06.2012 16:00     1
2       Apple        1,5          12.05.2012 14:45     0
1       Tomatoe      2            13.07.2012 10:45     0

Here is my trigger so far:

USE [TestDB]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER TRIGGER [dbo].[Produkt_Update]
ON [dbo].[Produkt]
AFTER UPDATE
AS
BEGIN
    SET NOCOUNT ON
    DECLARE @ID int
    DECLARE @Artikelname nvarchar(50)
    DECLARE @Preis numeric
    DECLARE @Flag numeric
    DECLARE @max_id int

    SET @ID = (SELECT ID FROM inserted)
    SET @Artikelname = (SELECT Artikelname FROM inserted)
    SET @Preis = (SELECT Preis FROM inserted)
    SET @Flag = (SELECT Flag FROM inserted)
    SET @max_id = (SELECT MAX(ID) from dbo.Produkt)


    SET IDENTITY_INSERT dbo.Produkt ON
    INSERT INTO dbo.Produkt
    (ID,Artikelname,Preis)
    values (@max_id+1,@Artikelname,@Preis)
    SET IDENTITY_INSERT dbo.Produkt OFF

    UPDATE dbo.Produkt
    SET Flag = 1
    WHERE ID=@ID
END

With my trigger I can create the new row but the price on the first row sill changes. I don’t know how to handle this. Could you help me please?

  • 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-07T18:17:25+00:00Added an answer on June 7, 2026 at 6:17 pm

    Please try this and let me know

    CREATE TRIGGER [dbo].[Produkt_Update]
       ON [dbo].[Produkt]
       AFTER  UPDATE
    AS
    BEGIN
     SET  NOCOUNT ON;
    
        DECLARE @MAX_ID INT;
        SELECT @MAX_ID=MAX(ID) FROM [Produkt];
    
        declare @tmp Table(ID  int,     Artikelname  varchar(200),
        PREIS varchar(200),UPDATE_DATE datetime,      FLAG bit)
    
        insert into @tmp
        select ID,Artikelname,PREIS,UPDATE_DATE,1 [flag] from deleted;
    
        delete T from [Produkt] T JOIN @tmp I
        ON T.ID=I.ID
    
    SET IDENTITY_INSERT   [Produkt] ON
    INSERT INTO [Produkt] (ID,Artikelname,PREIS,UPDATE_DATE,FLAG)
    SELECT @MAX_ID+ROW_NUMBER() OVER(ORDER BY ID) [ID],Artikelname,PREIS,GETDATE(),0 
    FROM INSERTED
    union all
    select * from @tmp
    
     SET IDENTITY_INSERT dbo.Produkt OFF
     SET  NOCOUNT OFF;
    END;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this programming problem, I'm using C#. I want to check if varA
I have this problem since I'm beginning in OOP programming I want to close
I have a problem: I am programming an app that is using a searchBar
I'm a beginner in Java programming language, and I have a problem that I
I'm new to python programming. I have this problem: I have a list of
I have one problem with android calendar programming, so If there are 2 months
I have a question about a programming problem from the book Cracking The Code
I'm a beginner concerning socket programming and I have a problem when I send
I have recently started programming in WPF and bumped into the following problem. I
PROBLEM I have a nested PHP array that I need to populate from flat

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.