How do you update a joined table in SQL SERVER?

Hi all,

I been trying for days to figure this one out and can't seem to get the task done.

I have two tables in SQL SERVER 2005.  Table 1 is tblRecords and table 2 is tblRecords_appts and I need to join them to get be able to update the right records.

I need to update all records that have wedding dates that have passed and set canceled to yes.  

So I figured the correct update statement would be:
UPDATE tblRecords_appts INNER JOIN
                      tblRecords ON tblRecords_appts.rid = tblRecords.rid
SET tblRecords_appts.cancelled = 'Y'    
WHERE     (tblRecords.weddate < '10/1/2013') AND (tblRecords_appts.locationid = '17') AND (tblRecords_appts.cancelled = 'N')

Open in new window

but obviously that is not working :(

I'm pretty sure it has to be an additional select under where.  Any help would be appreciated!

Who is Participating?
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.

Christopher KileCommented:

This article contains an example of a joined update.  Note that only one table gets updated at a time (UPDATE tablename SET field1 = value1 FROM tablename INNER JOIN table2name ON tablename.key1 = table2name.fkey1 WHERE table2name.field2 = value2).  Need more?  I can help.  Also, here is a link to the Technet article on UPDATE:
Brian CroweDatabase AdministratorCommented:
UPDATE tblRecords_appts
SET cancelled = 'Y'
FROM tblRecords AS R
INNER JOIN tblRecords_appts AS A
   ON R.rid = A.rid
Try this in a test platform first....

UPdate   tblRecords_appts
SET tblRecords_appts.cancelled = 'Y'
FROM  tblRecords_appts ,  tblRecords
WHERE tblRecords_appts.rid = tblRecords.rid
AND tblRecords.weddate < '10/1/2013'
AND tblRecords_appts.locationid = '17'
AND tblRecords_appts.cancelled = 'N'

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
Newly released Acronis True Image 2019

In announcing the release of the 15th Anniversary Edition of Acronis True Image 2019, the company revealed that its artificial intelligence-based anti-ransomware technology – stopped more than 200,000 ransomware attacks on 150,000 customers last year.

mateogpAuthor Commented:
This was exactly what I needed to complete the task!  Thanks so much!
Brian CroweDatabase AdministratorCommented:
Too bad I didn't post exactly the same thing LoL
mateogpAuthor Commented:
Sorry BriCrowe.  I tried you're solution and added on the where first and it didn't work.  I just tried it again and it did work.  I'd give split credit but I don't think I can revise that now.
Brian CroweDatabase AdministratorCommented:
It's really not a big was a momentary annoyance and was rather petty on my long as mateogp got the answer need it's all good.

mateogpAuthor Commented:
I'm sorry about that either way.  I did request attention just haven't received any response yet.  Thanks for the help everyone!!
How is the point split going?
mateogpAuthor Commented:
Oops sorry Tony I thought this was all taken care of.  I did the split!
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
Microsoft SQL Server

From novice to tech pro — start learning today.