Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Multiple Selects to update tblItems – INNER JOIN required?

I need to update the buyersAccountID in tblITEMS with the accountID of a winner who’s boxNo in tblEntrants matches ‘winner’ in tblItems:

TBLENTRANTS
fileID
boxNo
accountID

TBLITEMS
fileID
buyersAccountID
winner

So to clarify: If ‘winner’ which is an integer in tblItems matches the integer boxNo in tblEntrants then I need to store tblEntrants.accountID in tblItems.buyersAccountID

fileID also needs to be matched in each table.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

UPDATE tblItems SET buyersAccountID = SELECT tblEntrants.accountID WHERE tblEntrants.boxNo = tblItems.winner) WHERE fileID=@fileID

>Solution :

With so little information, this may the script you need:

UPDATE I
SET I.buyersAccountID = E.accountID
FROM tblItems AS I
INNER JOIN tblEntrants AS E ON I.fileID = E.fileID
AND I.winner = E.boxNo;

You can add your where after this query (before the semicolon). For example

UPDATE I
SET I.buyersAccountID = E.accountID
FROM tblItems AS I
INNER JOIN tblEntrants AS E ON I.fileID = E.fileID AND I.winner = E.boxNo
WHERE I.fileID=@fileID;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading