If the record is already not present in the table then insert the record.If the record is already present then update the fields Name, LST_UPD_BY and LST_UPD_DT.
Check the following conditions before transferring the data to User_Info table Remove duplicate records based on EMAIL field in the source.Check if the Email Address from source is already added in the table. If the record is already not present in the table then insert the record. If the record is already present then update the fields Name, LST_UPD_BY and LST_UPD_DT.
July 30th, 2012 5:00pm

Please try this MERGE syntax for SQL Server 2005 and later. Full syntax at - http://msdn.microsoft.com/en-us/library/bb510625(SQL.105).aspx declare @User_Info table (email varchar(20), LST_UPD_BY varchar(10), LST_UPD_DT datetime) declare @User_Info_Source table (email varchar(20)) declare @Lst_Upd_By varchar(10) select @Lst_Upd_By = 'Merge' insert into @User_Info (email) values ('abc@a.com') insert into @User_Info (email) values ('def@a.com') insert into @User_Info (email) values ('ghi@a.com') insert into @User_Info (email) values ('jkl@a.com') insert into @User_Info (email) values ('mno@a.com') insert into @User_Info (email) values ('pqr@a.com') insert into @User_Info_Source(email) values ('abc@a.com') insert into @User_Info_Source(email) values ('def@a.com') insert into @User_Info_Source(email) values ('uvw@a.com') insert into @User_Info_Source(email) values ('xyz@a.com') select * from @User_Info; select * from @User_Info_Source; merge into @User_Info as ui using @User_Info_Source as uis on ui.email = uis.email when matched then update set ui.LST_UPD_BY = @Lst_Upd_By, LST_UPD_DT = getdate() when not matched by target then insert (email) values (uis.email) ; -- Verifyselect * from @User_Info; select * from @User_Info_Source; Please mark this reply as the answer or vote as helpful, as appropriate, to make it useful for other readers. Thanks! Aalam | Blog (http://aalamrangi.wordpress.com)
Free Windows Admin Tool Kit Click here and download it now
July 30th, 2012 5:21pm

if both source data and destination are in sql server and same server, then Merge command works much faster and better as Aalam suggested. but if you have different source then you can do that with lookup component like this: http://www.rad.pasfu.com/index.php?/archives/46-SSIS-Upsert-With-Lookup-Transform.htmlhttp://www.rad.pasfu.com
July 30th, 2012 6:00pm

if both source data and destination are in sql server and same server, then Merge command works much faster and better as Aalam suggested. but if you have different source then you can do that with lookup component like this: http://www.rad.pasfu.com/index.php?/archives/46-SSIS-Upsert-With-Lookup-Transform.htmlhttp://www.rad.pasfu.com
Free Windows Admin Tool Kit Click here and download it now
July 30th, 2012 6:02pm

Thanks a lot for all, some new learnings in T-SQL, But srinivas if you want to do it using SSIS , go for lookup as reza suggested...
July 30th, 2012 11:33pm

thanq
Free Windows Admin Tool Kit Click here and download it now
July 31st, 2012 7:21am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics