How can we UPDATE from a SELECT in an SQL Server

0 votes

In an SQL Server, we can insert rows into a table with the help of INSERT.. SELECT command. The statement is shown below :

INSERT INTO Table (col1, col2, col3)
SELECT col1, col2, col3 
FROM other_table 
WHERE sql = 'cool'

Can we also update a table with SELECT? Having a temporary table containing the values and also would like to update another table using those values. Perhaps something like this:

UPDATE Table SET col1, col2
SELECT col1, col2 
FROM other_table 
WHERE sql = 'cool'
WHERE Table.id = other_table.id
May 27, 2022 in Others by avinash
• 1,840 points
4,727 views

1 answer to this question.

0 votes

We can firstly use SELECT statement to fetch the reference column and target column values.

SELECT e.City,A.City,e.PostCode,A.PostCode
FROM Employee e
INNER JOIN
[Address] add
ON e.EmpID = A.EmpID

Then, you will perform slight changes in your query, and it will prepare an UPDATE statement as shown below.

  1. Replace the select keyword with update.
  2. Specify the table name or alias name that needs to be updated.
  3. Use a set keyword and equals symbol (=) between referencing and target columns. 
UPDATE u set
u.City=A.City,
e.PostalCode=A.PostalCode
FROM Employee e
INNER JOIN [Address] a
ON e.EmpID = A.EmpID
answered May 27, 2022 by Avinash
• 240 points

Related Questions In Others

0 votes
1 answer
0 votes
1 answer

How can I open a URL in Android's web browser from my application?

ry this: Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri ...READ MORE

answered Jun 14, 2022 in Others by polo
• 1,480 points
4,798 views
0 votes
1 answer

How Can I use "Date" Datatype in sql server?

Your dates are interpreted as MM-DD-YYYY. This ...READ MORE

answered Jun 21, 2022 in Others by nisha
• 2,210 points
591 views
0 votes
1 answer

how to select a statistical range in an excel

The lowest 5% and top 5% will ...READ MORE

answered Nov 4, 2022 in Others by narikkadan
• 63,600 points
715 views
0 votes
1 answer

How can I store the data of an open excel workbook in a collection using BluePrism?

To do what you want is like ...READ MORE

answered Nov 24, 2022 in Others by narikkadan
• 63,600 points
1,362 views
0 votes
1 answer

How can I scrape a excel file from a website and divide it in different parts?

Use Scrapy or beautifulsoup4 parsing data it's more convenient ...READ MORE

answered Jan 13, 2023 in Others by narikkadan
• 63,600 points
659 views
0 votes
1 answer

How do I UPDATE from a SELECT in SQL Server?

MERGE INTO YourTable T USING ...READ MORE

answered Feb 3, 2022 in Database by Vaani
• 7,070 points
819 views
0 votes
0 answers

How do I UPDATE from a SELECT in SQL Server?

INSERT INTO Table (col1, col2, col3) SELECT col1, ...READ MORE

Feb 4, 2022 in Database by Vaani
• 7,070 points
545 views
0 votes
1 answer

How do I UPDATE from a SELECT in SQL server?

In SQL Server, it is possible to insert ...READ MORE

answered May 30, 2022 in Others by anisha
• 140 points
588 views
0 votes
0 answers

SQL Update from One Table to Another Based on a ID Match

Account and credit card numbers are stored ...READ MORE

Aug 22, 2022 in Database by Kithuzzz
• 38,000 points
1,209 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP