Multiple
ways we can do the update statements.
In MSSQL Server update statement
we can write the different types.
See the below example.
Created below the following tables.
--
Creating two tables.
Create Table
dbo.Table1(ID Int,Name VarChar(50));
GO
Create Table dbo.Table2(ID Int,Name VarChar(50));
GO
Insert Into dbo.Table1
Values(1,'A'),(2,'B'),(3,'C')
GO
Insert Into dbo.Table2
(ID,Name)
Values(1,'D'),(2,'E'),(3,'F')
GO
--
WITH Single Table
Update
Table1 SET Name=(Select Name From Table2 T2 Where T2.ID=Table1.ID)
--
With Join Condition.
UPDATE
T1 SET T1.Name=T2.Name
FROM dbo.Table1 T1 Inner JOin dbo.Table2 T2 On T1.ID=T2.ID
--Join
Statement
Update
Table1 SET Name=T2.Name
From dbo.Table1 T1 Inner Join dbo.Table2 T2 On T1.ID=T2.ID
--Using Sub Query
Update
Table1 SET Name=T2.Name
From
(Select ID,Name
From Table2 ) T2 Where T2.ID=Table1.ID
--Clean
up Tables
Drop Table dbo.Table1,dbo.Table2
No comments:
Post a Comment
It’s all about friendly conversation here at small review :) I’d love to be hear your thoughts!
Be sure to check back again because I do make every effort to reply to your comments here.