Compare the data in a row before the INSERT

Hi,

I would like to check if my data has changed since the last update and if so, another field to 'N/A'.  So I have forename, surname and married status. If a person's married status has changed.  I would like to set married status to 'Yes' otherwise do not update it.

Something like this?....

INSERT INTO TARGET
(FORENAME,
SURNAME,
MARRIED_STATUS)
SELECT
FORENAME,
SURNAME,
CASE WHEN TARGET.SURNAME <> SOURCE.SURNAME THEN 'YES' ELSE TARGET.MARRIED_STATUS
FROM SOURCE
Coco BeansDesignerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Pawan KumarDatabase ExpertCommented:
Please try like this -

You have to join the tables using some ID column... or primary key of the tables that uniquely identifies the row.
FROM SOURCE s INNER JOIN TARGET t ON t.SomeIDColumn = s.SomeIDColumn

INSERT INTO TARGET(FORENAME,SURNAME,MARRIED_STATUS)
SELECT s.FORENAME,s.SURNAME,CASE WHEN t.SURNAME <> s.SURNAME THEN 'YES' ELSE t.MARRIED_STATUS END as [MARRIED_STATUS]
FROM SOURCE s INNER JOIN TARGET t ON t.SomeIDColumn = s.SomeIDColumn

Open in new window

0
Coco BeansDesignerAuthor Commented:
Thanks.  I think this will just keep inserting more rows rather than update existing rows?  I would like to insert news if they don't exist in the target but they must update existing rows when they do.  I can't delete the table before the insert or I won't be able to do the compare?
0
Pawan KumarDatabase ExpertCommented:
In this case we need MERGE Statement. Please try like this-

--

MERGE INTO TARGET T
USING Source S ON S.SomeIDColumn = T.SomeIDColumn /*or Some Primary Key column*/
WHEN MATCHED THEN UPDATE 
	SET 
    T.FORENAME = S.FORENAME
   ,T.SURNAME = S.SURNAME
   ,T.MARRIED_STATUS = CASE WHEN t.SURNAME <> s.SURNAME THEN 'YES'  
ELSE S.MARRIED_STATUS END
WHEN NOT MATCHED THEN 
INSERT (T.FORENAME,T.SURNAME,T.MARRIED_STATUS)
VALUES (S.FORENAME,S.SURNAME,S.MARRIED_STATUS);

--

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ste5anSenior DeveloperCommented:
Just a comment:

What kind of database is this? Homework, hobby? What scenario do you want to cover?

FORENAME, SURNAME, MARRIED_STATUS are not attributes common for a relation.

Person ( PersonID, FirstName, LastName, etc)
Marriage ( PersonID, WithPersonID, ValidFrom, ValidTo)

would be the "natural" data model. Especially as "attributes" named status are normally not a attribute of a tuple, but a tuple of a relation.

p.s. attribute = column, tuple=row, relation = table.
1
Coco BeansDesignerAuthor Commented:
thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.