Popular Posts

Sep 3, 2013

update columns values with column of another table based on condition




Lets we have two tables.Name of table 1 is t_table1 and table 2 is t_table2.

Table1:
 
CREATE TABLE [dbo].[t_table1](

      [id] [int] IDENTITY(1,1) NOT NULL,

      [full_name] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
      [short_name] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
) ON [PRIMARY]
 


 Table2:

CREATE TABLE [dbo].[t_table2](

      [id] [int] IDENTITY(1,1) NOT NULL,

      [short_name_table1] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL) ON [PRIMARY]
Lets dump some data:
for table 1:


full_name short_name
Jubayer Ahmed J.Ahmed
Sera Rubaya S.Rubaya
Ahmed Khalid A. Khalid
Ahmed Sharid A. Sharid

for table 2:


full_name
Jubayer Ahmed
Sera Rubaya
Ahmed Khalid
Ahmed Sharid

We will update short_name_table1 columns of table 2 by the value of t t_table1.
Now run this query...

update t_table2
   set t_table2.short_name_table1 = t_table1.short_name
   FROM t_table1 INNER JOIN  t_table2 on t_table2.full_name = t_table2.short_name_table1



No comments:

Post a Comment