Thursday, July 18, 2013

Update query with different table in MSSQL?

How to use inner join in the update query using mssql?



Some time we need to update one table values to another table value in the same database.  For that we can use inner join to get the other table column value and update to the current or required table column. In the below example from  UserDetails table I have updated class name from ClassDetails tables class name column.


UPDATE    UserDetails
SET              ClassName =C.ClassName
FROM         UserDetails as U
INNER JOIN ClassDetails as C ON B.ClassID = M.ClassID
WHERE     B.TypeID = 'School'  AND U.ClassName = ''



The above SQL Query will help you to understand the JOIN or INNER JOIN in update sql query.

No comments: