Sunday, December 7, 2014

Update from Another table or select query in SQL

In case when we need to Update a table from another table or select query we can use this syntax.
 
 
For update from another Table. 
Syntax:
 
UPDATE
    Table
SET
    Table.col1 = other_table.col1,
    Table.col2 = other_table.col2
FROM
    Table
INNER JOIN
    other_table
ON
    Table.id = other_table.id
 
 
Update From Select Query. 
example:
 
update 
    tbl_JournalMaster 
set 
    Remarks=dbj.pname 
from 
    tbl_JournalMaster 
inner join 
(
select 
    distinct DJ.vno,
            (select 
                 top(1) pname 
             from 
                 DBASEJournal 
             where 
                 vno=dj.vno) as pname 
             from 
                 DBASEJournal DJ
 ) dbj 
 
on 
  dbj.VNO=tbl_JournalMaster.JVNo  ;
 
 
Feel Free To Contact For any Comments
 
Gautam Kumar Gupta
 
Go To Home Page