Monday, March 3, 2014

How to write update with select in SQL Server


UPDATE Query with SELECT in WHERE clause  in SQL Server :


Here i want to UPDATE employee details of  empdt table using the details of  empdt2 table where the team of both tables are same.

so, for that i SELECT Salary,Commission  values of empdt2 table,then i UPDATE the empdt table by using that SELECT result. 

Here we use INNER JOIN  for getting the values from the RESULT table of SELECT query.

Here is the sample code:

UPDATE  Table1
SET Table1.empsal=Table2.empsal,
Table1.empcomm=Table2.empcomm FROM empdt Table1
INNER JOIN
(SELECT empsal,empcomm,empteam FROM empdt2)  Table2

ON Table1.empteam=Table2.empteam

No comments:

Post a Comment

amazon

Sukanya Samriddhi Account - SBI

SUKANYA SAMRIDDHI Account information by SBI SUKANYA SAMRIDDHI ACCOUNT : FACILITY AVAILABLE AT ALL BRANCHES OF SBI Sukanya Samriddhi ...