hi mohan,You can use MERGE statement to do insert and update at a time. See the following script:Just replace Database names ( study, ADIN)USE studyGOCREATE TABLE testStudy (c1 int, name varchar(10))insert into testStudy VALUES(1, 'chandu'), (2, 'sailu'), (3, 'manohar')GOUSE ADINGO CREATE TABLE testAdin (c1 int, name varchar(10))insert into testAdin VALUES(1, 'muni'), (4, 'chandana')GOSELECT * FROM study..teststudy -- target tableSELECT * FROM ADIN..testAdin -- source tableGOUSE studyGOMERGE INTO study.dbo.testStudy AS t1USING ADIN.dbo.testAdin t2ON t1.c1 = t2.c1WHEN MATCHED THEN UPDATE SET t1.name = t2.nameWHEN NOT MATCHED BY TARGET THEN INSERT(c1, name) VALUES(t2.c1, t2.name);GOSELECT * FROM study..teststudy -- target tableGODROP TABLE ADIN..testAdinDROP TABLE study..teststudyGO Output:c1 name1 muni2 sailu3 manohar4 chandana
Then apply this MERGE operation for INSERT as well as UPDATE--Chandu