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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:49:45+00:00 2026-05-25T10:49:45+00:00

My Goal: Is to move data from one table to another if the row

  • 0

My Goal:
Is to move data from one table to another if the row has been updated or a new row has been Inserted.

I have a table I needs certain fields from. I need to know if the row has been updated or inserted.
The Source table does not have any Timestamp fields. I’m using MSSQL2008. The data is coming from a client and they are controlling the tables and replication.

I thought I had it figure out using the new Merge function for MSSQL 2008 but it updates all rows whether or not anything has changed. This is normally not a big problem but I have to add a timestamp fields. My modified time fields will be update whether or not the row has been updated.

So I need a way to complete my above goal. I’m not a great SQL expert so as you can see I’m struggling any help would be great.

USE NaylorAequor
DECLARE CurretDate GetDate();

MERGE Aequor_SLA_Ads AS Target

USING (select AWA.AdOrderID,emp.FirstName, emp.LastName,AWA.VendorID,AO.OrderDate,AO.SaleStatusID,A.AdColorId,AO.PublicationID,AWA.DateAssigned,AWA.DateAdCompleted 
from AdWorkAssignMent as AWA, Employee as emp, AdOrder AS AO,Ad as A
WHERE VendorId = 'Aequor' AND emp.EmployeeID = AWA.EmployeeID AND AWA.AdOrderId = AO.AdOrderID AND AO.AdId = A.AdId) AS Source

ON (Target.AdOrderID = Source.AdOrderID) 

WHEN MATCHED THEN
         UPDATE SET 
            Target.AdOrderID =Source.AdOrderID,
            Target.FirstName = Source.FirstName,
            Target.LastName =Source.LastName,
            Target.VendorID =Source.VendorID,
            Target.OrderDate =Source.OrderDate,
            Target.SaleStatusID =Source.SaleStatusID,
            Target.AdColorId =Source.AdColorId,
            Target.PublicationID =Source.PublicationID,
            Target.DateAssigned =Source.DateAssigned,
            Target.DateAdCompleted =Source.DateAdCompleted,
            Target.AequorModifiedDateTime = GetDate()


WHEN NOT MATCHED BY TARGET THEN
      INSERT (AdOrderID,FirstName,LastName,VendorID,OrderDate,SaleStatusID,AdColorId,PublicationID,DateAssigned,DateAdCompleted,AequorDateTime,AequorModifiedDateTime)

     VALUES (Source.AdOrderID, Source.FirstName,Source.LastName,Source.VendorID, Source.OrderDate,Source.SaleStatusID,Source.AdColorId,
                Source.PublicationID,Source.DateAssigned,Source.DateAdCompleted,GetDate(),GetDate())

OUTPUT $action, Inserted.*, Deleted.*;
  • 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-25T10:49:46+00:00Added an answer on May 25, 2026 at 10:49 am

    You need to add the restriction that you only want to update the data if some of the values are different. So you have to change that part of the query for something like this:

    WHEN MATCHED AND (  Target.FirstName != Source.FirstName OR 
                        Target.LastName != Source.LastName OR
                        Target.VendorID != Source.VendorID OR
                        Target.OrderDate != Source.OrderDate OR
                        Target.SaleStatusID != Source.SaleStatusID OR
                        Target.AdColorId !=Source.AdColorId OR
                        Target.PublicationID !=Source.PublicationID OR
                        Target.DateAssigned !=Source.DateAssigned OR
                        Target.DateAdCompleted !=Source.DateAdCompleted)
    THEN
             UPDATE SET 
                Target.AdOrderID =Source.AdOrderID,
                Target.FirstName = Source.FirstName,
                Target.LastName =Source.LastName,
                Target.VendorID =Source.VendorID,
                Target.OrderDate =Source.OrderDate,
                Target.SaleStatusID =Source.SaleStatusID,
                Target.AdColorId =Source.AdColorId,
                Target.PublicationID =Source.PublicationID,
                Target.DateAssigned =Source.DateAssigned,
                Target.DateAdCompleted =Source.DateAdCompleted,
                Target.AequorModifiedDateTime = GetDate()
    

    In this case, the comparison is assuming that every field is not nullable, if that’s not the case, you need to add that to the logic as well (an ISNULL or something like that)

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

Sidebar

Related Questions

I have two tables and my goal is to move specific data from the
My goal is to move away from an ISAPI filter that was set up
Each time, the figure should move 1/16 of its current distance from the goal
I have data coming in from datastage that is being put in our SQL
goal: I have the string 1234432144 I want to only replace the first 2
Goal Pass images generated by one process efficiently and at very high speed to
My Goal I would like to have a main processing thread (non GUI), and
My goal is to maintain a web file server separately from my main ASP.NET
The goal: To create a .NET dll i can reference from inside SQL Server
I'm basically trying to get all touch event data from something like a system

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.