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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T22:09:51+00:00 2026-06-10T22:09:51+00:00

I need to create a trigger that will add a record to a queue

  • 0

I need to create a trigger that will add a record to a queue table whenever a record is modified in the main table. The record added to the queue table must contain every field that was modified for that record.

I have this code so far but I don’t think it will work for multiple rows updated:

ALTER TRIGGER [dbo].[tr_EmpHistory]    
ON [dbo].[employeeData]  
FOR UPDATE 
AS 
BEGIN      
DECLARE @FieldsUpdated xml,  
@FieldsUpdated1 varchar(100)       
SELECT @Fieldsupdated1 = ' ' 
SELECT 
@FieldsUpdated1 = @FieldsUpdated1 + ' emp_bankaccountnumber' 
FROM inserted as a,  
deleted as b  
WHERE a.emp_id = b.emp_id             
AND a.emp_bankAccountNumber <> b.emp_bankAccountNumber  
SELECT      
@FieldsUpdated1 = @FieldsUpdated1 + 'emp_salary ' 
FROM inserted as a,        
deleted as b   
WHERE a.emp_id = b.emp_id       
AND a.emp_salary <> b.emp_salary 

SELECT    
@FieldsUpdated1 = @FieldsUpdated1 + 'emp_SSN ' 
FROM inserted as a,  
deleted as b  
WHERE a.emp_id = b.emp_id            
AND a.emp_SSN <> b.emp_SSN  
SELECT 
@FieldsUpdated1 = @FieldsUpdated1 + 'emp_lname ' 
FROM inserted as a,  
deleted as b  
WHERE a.emp_id = b.emp_id           
AND a.emp_lname <> b.emp_lname  
SELECT      
@FieldsUpdated1 = @FieldsUpdated1 + 'emp_fname ' 
FROM inserted as a,  
deleted as b  
WHERE a.emp_id = b.emp_id     
AND a.emp_fname <> b.emp_fname  
SELECT 
@FieldsUpdated1 = @FieldsUpdated1 + 'emp_manager ' 
FROM inserted as a,  
deleted as b  
WHERE a.emp_id = b.emp_id          
AND a.emp_manager <> b.emp_manager  
SELECT @Fieldsupdated =  ( 
SELECT COLUMN_NAME AS Name 
FROM INFORMATION_SCHEMA.COLUMNS  
WHERE TABLE_NAME = 'employeeData' 
AND CHARINDEX(COLUMN_NAME,(ltrim(rtrim(@fieldsupdated1)))) > 0 
FOR XML AUTO, ROOT('Fields') )

INSERT INTO auditEmployeeData( 
audit_emp_id, 
audit_emp_bankAccountNumber,
audit_emp_salary, 
audit_emp_SSN, 
audit_emp_lname, 
audit_emp_fname, 
audit_emp_manager, 
ColumnsUpdated ) 

SELECT emp_id, 
emp_bankAccountNumber, 
emp_salary,    
emp_SSN,   
emp_lname,  
emp_fname,     
emp_manager,       
@FieldsUpdated   
FROM INSERTED  
END
GO 

If I am understanding this correctly, if one record had last name updated and another record got the first name updated at the same time then both records would be logged as having both first and last named changed. Is that correct? If so, how could I make it work correctly without using a cursor?

The only way I can think to solve the problem is to use a cursor but I know that is not a good idea. Any help would be appreciated.

Thanks!

  • 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-10T22:09:53+00:00Added an answer on June 10, 2026 at 10:09 pm

    You can use CASE to build a value row-by-row showing the changed columns:

    select i.emp_id,
      case when i.foo <> d.foo then ',foo' else '' end +
      case when i.bar <> d.bar then ',bar' else '' end as changedcolumns
      from inserted as i inner join
        deleted as d on d.emp_id = i.emp_id
    

    Some additional fiddling about can eliminate the extra delimiter.

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

Sidebar

Related Questions

I need to write a trigger that will create a record in another table.
I am renewing a record. I need to create a trigger that will automatically
I need to create a Trigger that fires when a child record (Codes) is
I need to create a trigger that writes changes in a shadow table. I
I need to create a trigger, that will change Border background property, when MouseEnter
I am trying to create a Trigger, that will update the History Table with
I need to create a view or a table valued function that returns one
I need to create a field that will show the number of days between
I want to create a trigger that will update the column LastActivityDate to the
Ok. So I know how to create a trigger that makes changes to Table

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.