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?
Tony303Connect With a Mentor Commented:
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'
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 CroweConnect With a Mentor Database AdministratorCommented:
UPDATE tblRecords_appts
SET cancelled = 'Y'
FROM tblRecords AS R
INNER JOIN tblRecords_appts AS A
   ON R.rid = A.rid
Build your data science skills into a career

Are you ready to take your data science career to the next step, or break into data science? With Springboard’s Data Science Career Track, you’ll master data science topics, have personalized career guidance, weekly calls with a data science expert, and a job guarantee.

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!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.