JOIN two tables question

I need to UPDATE a field in a table.  tbl2015 will have data that is NOT in tbl2014.  Those rows that are NOT IN tbl2014 I want to SET ComparePriorYr = '2'.  I am having trouble with my query.  Here is what I have so far.

UPDATE tblStockCertInfo_2015
SET ComparePriorYr = '2'

FROM tblStockCertInfo_2015 tbl2015
 LEFT JOIN tblStockCertInfo_2014  tbl2014 ON tbl2014.InfoUniqueKey = tbl2015.InfoUniqueKey

Open in new window


What type of JOIN do I use to get only the data that is in tbl2015 and NOT IN tbl2014?
huerita37Asked:
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.

Brendt HessSenior DBACommented:
Your LEFT JOIN is correct. What you need is a WHERE clause that filters on the tbl2014.InfoUniqueKey field, like this one:

WHERE tbl2014.InfoUniqueKey IS NULL

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
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
Use the alias in UPDATE, if defined:
UPDATE tbl2015
SET ComparePriorYr = '2'
FROM tblStockCertInfo_2015 tbl2015
LEFT JOIN tblStockCertInfo_2014 tbl2014
ON bl2014.InfoUniqueKey = tbl2015.InfoUniqueKey
WHERE bl2014.InfoUniqueKey IS NULL

Open in new window

huerita37Author Commented:
I gave most of the points to bhess1 since he was the first and so quick.  I did want to award some points to Qlemo because he responded with a good solution as well.
Jim HornMicrosoft SQL Server Data DudeCommented:
>tbl2015 will have data that is NOT in tbl2014.
In addition to the above correct comments, to better understand this concept do an images.google.com search for 'sql joins', and you'll see some lovely pictures that illustrate this.  Since they're all copyrighted we can't just post them here.
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.