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 study
GO
CREATE TABLE testStudy (c1 int, name varchar(10))
insert into testStudy VALUES(1, 'chandu'), (2, 'sailu'), (3, 'manohar')
GO
USE ADIN
GO
CREATE TABLE testAdin (c1 int, name varchar(10))
insert into testAdin VALUES(1, 'muni'), (4, 'chandana')
GO
SELECT * FROM study..teststudy -- target table
SELECT * FROM ADIN..testAdin -- source table
GO
USE study
GO
MERGE INTO study.dbo.testStudy AS t1
USING ADIN.dbo.testAdin t2
ON t1.c1 = t2.c1
WHEN MATCHED THEN
UPDATE SET t1.name = t2.name
WHEN NOT MATCHED BY TARGET THEN
INSERT(c1, name)
VALUES(t2.c1, t2.name);
GO
SELECT * FROM study..teststudy -- target table
GO
DROP TABLE ADIN..testAdin
DROP TABLE study..teststudy
GO
Output:
c1 name
1 muni
2 sailu
3 manohar
4 chandana
Then apply this MERGE operation for INSERT as well as UPDATE
--
Chandu