USE OF FROM CLAUSE IN UPDATE STATEMENT IN SQL SERVER 2005

We can use from clause with table alias to update a table.It would be much more clearer to update things.
Below example shows how we can achieve this.

–Check the table first
SELECT * FROM EMPLOYEE

Output
______________

2    Gaurav    80000
3    Saurabh    20000
5    Neelam    200000
6    Saurabh    24000

UPDATE p
SET Salary = 18000
FROM EMPLOYEE p
WHERE p.EmpName = ‘Neelam’

OUTPUT
______________

(1 row(s) affected)

–Check the table again to confirm
SELECT * FROM EMPLOYEE

OUTPUT
______________

2    Gaurav    80000
3    Saurabh    20000
5    Neelam    18000
6    Saurabh    24000

Notes:
1.UPDATE is a dangerous statement if used without proper where clause.
2.One has be 100% sure about what we are updating.First select the no of rows you are updating & then proceed with your update.

Pawan Kumar
Pawankkmr@hotmail.com

Advertisements