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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T08:22:27+00:00 2026-06-12T08:22:27+00:00

Say I have two tables, a live table and a history table. The live

  • 0

Say I have two tables, a “live” table and a history table. The live table looks like this:

CREATE TABLE dbo.LiveTable (
    LiveTableId INT IDENTITY(1,1) NOT NULL PRIMARY KEY,
    SomeVarChar VARCHAR(20) NOT NULL DEFAULT '',
    SomeForeignId INT NULL,
    OtherForeingId INT NULL,
    ChangeBy VARCHAR(128) NOT NULL,
    ChangeTime DATETIME NOT NULL DEFAULT GETDATE()
)

The history table looks the same, except it has its own primary key and an IsDelete column used elsewhere. When a user updates a row in LiveTable, I want to log the row’s prior values in the history table. This is simple enough with an UPDATE trigger, of course. But I’ve just determined that some of my updates aren’t firing the history table insert, and I believe this is due to fields that allow nulls. My trigger looks something like this:

CREATE TRIGGER dbo.trgLiveTableUpdate ON dbo.LiveTable FOR UPDATE
AS
INSERT INTO dbo.LiveTableHistory (
    LiveTableId,
    SomeVarChar,
    SomeForeignId,
    OtherForeignId,
    ChangeBy,
    ChangeTime
)
SELECT 
    d.LiveTableId,
    d.SomeVarChar,
    d.SomeForeignId,
    d.OtherForeignId,
    d.ChangeBy,
    d.ChangeTime
FROM DELETED d
JOIN INSERTED i ON d.LiveTableId = i.LiveTableId
WHERE d.SomeVarChar <> i.SomeVarChar
OR d.SomeForeignId <> i.SomeForeignId   //<--- I don't think this works
OR d.OtherForeignId <> i.OtherForeignId //<--- this either

Is it possible that my WHERE condition isn’t hitting on situations where the change is to SomeForeignId or OtherForeignId because those columns both allow nulls? If so, how would I write a condition that would take nulls into account when checking those columns for inequality?

  • 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-12T08:22:28+00:00Added an answer on June 12, 2026 at 8:22 am

    You could also find the EXCEPTions, which give you the keys to record.

    select 
        d.LiveTableId,
        d.SomeVarChar,
        d.SomeForeignId,
        d.OtherForeignId,
        d.ChangeBy,
        d.ChangeTime
    from DELETED d
    join
    (
    select LiveTableId, SomeVarChar, SomeForeignId, OtherForeignId from INSERTED
    EXCEPT 
    select LiveTableId, SomeVarChar, SomeForeignId, OtherForeignId from DELETED
    ) e on e.LiveTableId = d.LiveTableId
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables. Lets say they look like this Table Sports: Column 1:
Let's say I have two tables that look like this: TH TH TH TH
Lets say I have two tables like this: person: id, first_name, last_name, phone_id phone:
Say I have two tables: orderinfo and customerinfo They look like this: orderinfo: OrderID
Say I have two tables: create table parent ( id number not null, constraint
Say I have two tables: create table parent ( number not null, constraint parent_pk
Lets say I have two tables tblA ( tableAID INT IDENTITY(1,1), foo VARCHAR(100)) tblB
Say I have two tables, user and comment . They have table definitions that
Lets say I have two tables, one for transactions, and another table who's primary
Let's say I have two tables with several fields and in every table there

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.