You are over-complicating the situation by using two tables and you haven't helped by not giving us the table schema. However, perhaps this will help you...
Consider these two tables...
create table demoInput
(
CID int,
Mobile varchar(30),
FirstName varchar(30),
Lastname varchar(30)
)
create table demoTarget
(
CID int,
Mobile varchar(30),
FirstName varchar(30),
Lastname varchar(30),
UpdateIdentifier char(1),
Points int
)
How you create
demoInput
is up to you... it could be input parameters to a stored procedure or an actual table. I used this sample data on my first run
INSERT INTO demoInput (CID, Mobile, FirstName, LastName) values
(1, '12345','Joe','Bloggs'),
(2, '34567','Jane','Bloggs')
If the following code is in a stored procedure (say) <pre lang="SQL">
UPDATE T SET
Mobile = I.Mobile, UpdateIdentifier = 'U',
Points = CASE WHEN Points IS NULL THEN 10 ELSE Points END,
FirstName = CASE WHEN Points IS NULL THEN T.FirstName ELSE I.FirstName END,
Lastname = CASE WHEN Points IS NULL THEN T.Lastname ELSE I.Lastname END
FROM demoInput I
left outer join demoTarget T ON I.CID=T.CID
WHERE T.CID IS NOT NULL
INSERT INTO demoTarget
SELECT I.CID, I.Mobile, I.FirstName, I.Lastname, 'N' AS UpdateIdentifier, CAST(NULL as int) AS Points
FROM demoInput I
left outer join demoTarget T ON I.CID=T.CID
WHERE T.CID IS NULL
then I believe you will get the behaviour you want.
First run with data as above:
1 12345 Joe Bloggs N NULL
2 34567 Jane Bloggs N NULL
Second run with data of
TRUNCATE TABLE demoInput
INSERT INTO demoInput (CID, Mobile, FirstName, LastName) values
(1, '12345999','Joe','Bloggs'),
(2, '34567999','Jane','Bloggs'),
(3, '3435566', 'New','Person')
1 12345999 Joe Bloggs U 0
2 34567999 Jane Bloggs U 0
3 3435566 New Person N NULL
Then a third run of
TRUNCATE TABLE demoInput
INSERT INTO demoInput (CID, Mobile, FirstName, LastName) values
(1, '12345999','Joe2','Bloggs'),
(2, '34567998','Jane','Bloggs2'),
(3, '3435566', 'New','Person3')
gives
1 12345999 Joe2 Bloggs U 10
2 34567998 Jane Bloggs2 U 10
3 3435566 New Person U 10
There may be some wrinkles to iron out, and your two target tables need to be sorted, but this should give you an idea of how to go about it