help with sql query | SQL Server Performance Forums

SQL Server Performance Forum – Threads Archive

help with sql query

TableA
empid cardno
111
123
233 TableB empid cardno
111 89
123 11
233 33
444 66
555 44
666 33 I need to update TableA’s cardno with the corresponding cardno data from TableB
How can I achieve this? Update TableA set TableA.cardno=TableB.cardno where TableA.empid = TableB.empId
This doesnt work .I need a Join or Select or something Thx
Nearly there … add a FROM clause like this: UPDATE TableA
SET TableA.CardNo = TableB.CardNo
FROM TableA
INNER JOIN TableB
ON TableA.EmpId = TableB.EmpId Yes, you get to mention TableA twice, but it’s clear to SQL Server what you mean – also, you can only ever update columns in one table in an UPDATE statement, so that’s what the UPDATE clause is actually for. You can even use a table alias if you want to avoid repeating the full name of your table: UPDATE TA
SET TA.CardNo = TB.CardNo
FROM TableA TA
INNER JOIN TableB TB
ON TA.EmpId = TB.EmpId


Thx. Worked.
]]>