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 in Others by avinash
• 1,840 points
43 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 by Avinash
• 240 points

Related Questions In Others

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 in Others by polo
• 1,500 points
88 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 in Others by nisha
• 2,180 points
27 views
0 votes
1 answer

How can you create an object in JavaScript?

JavaScript supports Object concept very well. You can create ...READ MORE

answered Mar 7, 2019 in Others by Frankie
• 9,830 points
195 views
0 votes
1 answer

How can you create an Array in JavaScript?

You can define arrays using the array literal as ...READ MORE

answered Mar 7, 2019 in Others by Frankie
• 9,830 points
249 views
0 votes
0 answers

Can we write AES enryption in Javascript and Decryption in Java using a static SALT and IV

I'm working on AES encryption I want to ...READ MORE

Jun 4, 2019 in Others by Eighteen
• 120 points
1,324 views
0 votes
0 answers

How to open a new tab or a new window while writing an exam in AI - Onlime Remote Proctored

How to open a new tab or ...READ MORE

Jul 20, 2020 in Others by vimalkamal
• 120 points
2,333 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 in Database by Vaani
• 7,020 points
75 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 in Database by Vaani
• 7,020 points
29 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 in Others by anisha
• 140 points
25 views
0 votes
1 answer

What is a stored procedure?

A stored procedure is a set of ...READ MORE

answered Feb 4 in Database by Neha
• 8,740 points
157 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP