How to Update From Select Statement in SQL Server ?

Published on December 11, 2013 by abundantcode

Filed under SQL Server

Last modified December 11, 2013

Print this page

rate 1 star rate 2 star rate 3 star rate 4 star rate 5 star
Your rating: none, Average: 0 (0 votes)

This article have been viewed 3207 times

Below is a sample query that demonstrates how to update a table by getting the data from the select query in SQL Server. Assume that there are 2 tables AbundantCodeEmployee and AbundantCodeDepartment which contains the id . The id(in department) in this example refers to the department head . The columns “DepartmentName” is updated in the query based on the value retreived  .

How to Update From Select Statement in SQL Server ?

UPDATE
AbundantCodeEmployee
SET
  AbundantCodeEmployee.DepartmentName = AbundantCodeDepartment.DepartmentName
FROM
  AbundantCodeEmployee
INNER JOIN
  AbundantCodeDepartment
ON
  AbundantCodeEmployee.id = AbundantCodeDepartment.id
If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.

Leave a Comment


x

You might be Interested in these Posts

How to update the value in a Dictionary in C# ?

Below is a sample code snippet demonstrating how to update the value that is stored within the dicti...

How to Perform Inner Join using LINQ in C# ?

Are you looking for the syntax on how to perform inner join using LINQ in C# ? . Below is a sample c...

Equivalent of IF Statement in SQL Server

There are times when you want to perform conditional checks within a SQL Query in SQL Server .Be...