Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how the sql Update statement work , while using FROM Clause with multiple joins[also in this situation how the WHERE clause work] ?,

also is it necessary that the table which is getting updated has to be in FROM clause ?

kindly explain
Posted

Update works on the table you tell it to. For example:

SQL
UPDATE table1 
SET Field1 = ''


will update table1.

The from clause will limit what gets updated. You don't need a From clause unless you are trying to join in other tables.
 
Share this answer
 
Comments
anbujeremiah 3-Sep-13 20:18pm    
so FROM clause will act as WHERE clause ?
ZurdoDev 3-Sep-13 20:47pm    
No. You could do, for example

UPDATE table1
SET Field1 = ''
FROM table1 t1
INNER JOIN table2 t2 ON t1.CustomerID = t2.CustomerID
WHERE t2.City = ''

This will update Table1.Field1 to a blank where the corresponding record in Table2 has a blank City. You really only need from when you are joining in another table.

Or, you could do something like this:
UPDATE Table1
SET Field1 = ''
WHERE Field1 IS NULL

So, if Field1 is null it will get changed to a blank.
also is it necessary that the table which is getting updated has to be in FROM clause ?
 
Share this answer
 
Comments
Zerotimedev 15-Sep-13 22:58pm    
i think from the tests i tried it needs to be set as the 'from' table to update. If you needed to reference another table then you would inner join to that, otherwise if you need to update 2 tables you will probably just need a second update query.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900