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 8803269
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T01:17:49+00:00 2026-06-14T01:17:49+00:00

Dear all I´m having trouble with my trigger. Am I doing this at all

  • 0

Dear all I´m having trouble with my trigger.

Am I doing this at all right, right now it only works for Insert. I think I´m pretty close tho please help me if you have the time. I´m trying to store all the inserts, updates and deletes into the table customers_changelog via trigger. There is something wrong with the code I cant delete or update customers I can only insert new ones. Please help my I have been spending plenty of hours on this and just cant get this to work! 🙂

create table customers (
customerid int identity primary key,
name varchar(100) not null,
address varchar(100)
)
go

create table customers_changelog (
customerid int,
name varchar(100) not null,
address varchar(100),
change_user varchar(32),
change_time datetime,
change_action char(1) default 'I',
check (change_action = 'I' or change_action = 'D')
)
go


CREATE TRIGGER log_changes
ON customers
AFTER INSERT, UPDATE, DELETE
AS 
BEGIN
    SET NOCOUNT ON;

--
-- Check if this is an INSERT, UPDATE or DELETE Action.
-- 
DECLARE @customerid1 as int;
DEClARE @name1 as varchar(32);
DECLARE @address1 as varchar(100);
DECLARE @change_action1 as char(1);
DECLARE @change_time1 as datetime;
DECLARE @change_user1 as varchar(32);

select  @customerid1 = c.customerid, @name1 = c.name, @address1 = c.address
from customers c, inserted i
where c.customerid = i.customerid 

SET @change_time1 = CURRENT_TIMESTAMP;
SET @change_user1 = CURRENT_USER;
INSERT INTO customers_changelog(customerid,name,address,change_action,change_time,change_user)
    VALUES(@customerid1,@name1,@address1,'I',@change_time1,@change_user1)
IF EXISTS(SELECT * FROM DELETED)
BEGIN    
            IF EXISTS(SELECT * FROM INSERTED) 
            INSERT INTO customers_changelog VALUES(@customerid1,@name1,@address1,'U',@change_time1,@change_user1)
            ELSE 
            INSERT INTO customers_changelog VALUES(@customerid1,@name1,@address1,'D',@change_time1,@change_user1)

END
ELSE 
    IF NOT EXISTS(SELECT * FROM INSERTED) RETURN;
END
  • 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-14T01:17:51+00:00Added an answer on June 14, 2026 at 1:17 am

    Assuming MS-SQL from syntax – So couple issues here:
    1. Need to specify column lists in the “update” and “delete” inserts because the column order in the table doesn’t match your inserts.
    2. Can’t use “inserted” data for delete insert

    ALTER TRIGGER [dbo].[log_changes] ON [dbo].[customers] AFTER INSERT, UPDATE, DELETE AS 
    BEGIN
        SET NOCOUNT ON;
        DECLARE @customerid1 as int;
        DEClARE @name1 as varchar(32);
        DECLARE @address1 as varchar(100);
        DECLARE @change_action1 as char(1);
        DECLARE @change_time1 as datetime;
        DECLARE @change_user1 as varchar(32);
    
        select  @customerid1 = c.customerid, @name1 = c.name, @address1 = c.address
        from customers c, inserted i
        where c.customerid = i.customerid 
    
        SET @change_time1 = CURRENT_TIMESTAMP;
        SET @change_user1 = CURRENT_USER;
    
        IF EXISTS(SELECT * FROM DELETED)
        BEGIN    
                    IF EXISTS(SELECT * FROM INSERTED) 
                    INSERT INTO customers_changelog(customerid,name,address,change_action,change_time,change_user) 
                        VALUES(@customerid1,@name1,@address1,'U',@change_time1,@change_user1)
                    ELSE 
                    BEGIN
                        select  @customerid1 = d.customerid, @name1 = d.name, @address1 = d.address
                            from deleted d
                        INSERT INTO customers_changelog(customerid,name,address,change_action,change_time,change_user) 
                            VALUES(@customerid1,@name1,@address1,'D',@change_time1,@change_user1)
                    END
    
        END
        ELSE
        BEGIN
            IF NOT EXISTS(SELECT * FROM INSERTED) RETURN;
            INSERT INTO customers_changelog(customerid,name,address,change_action,change_time,change_user)
                VALUES(@customerid1,@name1,@address1,'I',@change_time1,@change_user1) 
        END
    END
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Dear all,Now i have this question in my java program,I think it should be
Dear all, I've been stuck with this problem now for a few days and
Dear all i am using this function to get the GMT time, i am
dear all..i have this code: <script> var str=KD-R435MUN2D; var matches=str.match(/(EE|[EJU]).*(D)/i); if (matches) { var
iphone http://sharingdream.com/phone.jpg Dear all, I'm having a strange problem with an application I'm building
Dear all, I now have a preliminary macro (defmacro key-if(test &key then else) `(cond
Dear all,I am now using a webtool http://fiddesktop.cs.northwestern.edu/mmp/scrape?url= to parse a webpage. For example,we
dear all.. i have a DB which built use Mysql. for this case, i
I've been having this problem for while now, but always seem to put off
Dear All, Now am developing offline examination kind of application, here it has different

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.