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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T10:15:03+00:00 2026-05-29T10:15:03+00:00

I need to compare two row with each other and then write the fields

  • 0

I need to compare two row with each other and then write the fields that changed into a table.

My table:

CREATE TABLE dbo.tUserChanges
(
cID int IDENTITY (1,1),
cChangeDate DateTime,
cValueChanged varchar(30),
cPreviousValue bit,
cCurrentValue bit
)

In my C# program the user table needs to be updated when changes are made to the users.

This is done with a stored procedure:

CREATE PROCEDURE [dbo].[UUser]       
( 
 @User as varchar(15),   
 @UCode  AS int,      
 @UName  AS varchar(30),      
 @UID   AS  varchar(10),      
 @UPassword  AS varchar(15),      
 @UPMaintenance as bit,      
 @UClient  as bit,      
 @UFinancial  as bit,      
 @UViewReceiptImage bit,
 @UViewPayrollData bit
) 
AS
BEGIN
UPDATE  tUsers      
 SET        
 UName    = @UName,      
 UID    = @UID,      
 UPassword   = @UPassword,  UPMaintenance   = @UPMaintenance,       
 UClient    = @UClient,      
 UFinancial   = @UFinancial,
 UViewReceiptImage = @UViewReceiptImage,
 UViewPayrollData = @UViewPayrollData     
 WHERE UCode = @UCode 
END 

There is more values in the tUser table but for the sake of keeping it sort I removed some of the values.

So what I need to do is create a temp table at the befor I update the tUsers table so that I can Compare the two rows after the update has been done and then right the changes that took place into the new table.

I have tried this but I know there is a better way and it also does not give the required results:

declare @i int
set @i = 0
declare @ColumnCount int
set @ColumnCount = (SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS WHERE  table_name = @UID)
declare @ColumnName varchar(30)
set @ColumnName = (select column_name from information_schema.columns  where table_name = 'tUsers' and ordinal_position = 14)

select UActivateLoan from tusers

while (@ColumnCount < @i)
Begin
    if((select @ColumnName from tUsers where UID = @UID) 
    <> (select @ColumnName from #myTemp where UID = @UID))
    Begin
        Insert into tUserChanges
        Values(GETDATE(),(select column_name from information_schema.columns  where table_name = 'tUsers' and ordinal_position = 14),
                (select(select column_name from information_schema.columns  where table_name = 'tUsers' and ordinal_position = 14) from #myTemp where UID = @UID),
                (select(select column_name from information_schema.columns  where table_name = 'tUsers' and ordinal_position = 14) from tUsers where UID = @UID))
    END
    set @i = @i + 1
End

I am not sure if I will need to use a cursor here or what I can do to get the result?
Any help would be appreciated.

  • 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-05-29T10:15:04+00:00Added an answer on May 29, 2026 at 10:15 am

    This would be better done using an FOR UPDATE trigger.

    CREATE TRIGGER dbo.tUser_AfterUpdate ON dbo.tUsers FOR UPDATE AS 
    BEGIN
      IF UPDATE(UName) THEN 
        INSERT INTO tUserChanges 
        SELECT GETDATE()
               , (SELECT UName FROM DELETED) -- Old Value
               , (SELECT UName FROM INSERTED) -- New Value
    
      ...
    END
    

    CREATE TRIGGER

    Creates a trigger, which is a special kind of stored procedure that
    executes automatically when a user attempts the specified
    data-modification statement on the specified table. Microsoft® SQL
    Server™ allows the creation of multiple triggers for any given INSERT,
    UPDATE, or DELETE statement.

    Edit

    Using Dynamic SQL, would be something like

      CREATE TRIGGER dbo.tUser_AfterUpdate ON dbo.tUsers FOR UPDATE AS 
      BEGIN
        DECLARE @Columns TABLE (name sysname)
        DECLARE @ColumnName sysname
        DECLARE @Statement VARCHAR(MAX)
    
        INSERT INTO @Columns
        SELECT  name
        FROM    sys.columns
        WHERE   OBJECT_NAME(OBJECT_ID) = 'tUsers'
    
        WHILE EXISTS (SELECT * FROM @Columns)
        BEGIN 
          SELECT TOP 1 @ColumnName = name FROM @Columns
          DELETE FROM @Columns WHERE name = @ColumnName
    
          SET @Statement = 
            'IF UPDATE(' + @ColumnName + ') THEN '
            + 'INSERT INTO tUserChanges '
            + 'SELECT GETDATE() '
            + '      , (SELECT ' + @ColumnName + 'FROM DELETED) -- Old Value'
            + '      , (SELECT ' + @ColumnName + 'FROM INSERTED) -- New Value'
    
          EXEC (@Statement)
        END
      END
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to check that two variables match fields in a row where the
I need to compare dozens of fields in two objects (instances of the same
i'm having to two tables, which i need to compare both the table. Let
I need to compare two lists where each list contains about 60,000 objects. what
I need to compare two columns in a sql table. The data in one
I need to compare two strings that are of varying length and as such
I need to compare values from two tables that are identical but contain some
I need to compare two Date s (e.g. date1 and date2 ) and come
When you need to compare two tables to see what the differences are, are
in bash I need to compare two float numbers, one which I define in

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.