DECLARE other_result_id int SET other_result_id SELECT SyncId FROM cur

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
DECLARE @other_result_id int
SET @other_result_id=(SELECT SyncId FROM #current_table# WHERE ValueId = #current_result_id#)
IF (@other_result_id IS NULL)
BEGIN
INSERT INTO sm_forms_Values (InstanceId) SELECT Id FROM sm_forms_Instances WHERE SeparateSQLTableName=''#other_table#''
SET @other_result_id = SCOPE_IDENTITY()
INSERT INTO #other_table# ([ValueId], [First Name], [Last Name], [Insurance Company], [Email Address], [SyncId])
SELECT @other_result_id, [First Name], [Last Name], [Insurance Company], [Email Address], #current_result_id#
FROM #current_table#
WHERE ValueId = #current_result_id#
UPDATE #current_table# SET SyncId = @other_result_id WHERE ValueId = #current_result_id#
END
ELSE
BEGIN
UPDATE OT SET
[First Name] = CT.[First Name],
[Last Name] = CT.[Last Name],
[Insurance Company] = CT.[Insurance Company],
[Email Address] = CT.[Email Address]
FROM #other_table# OT
INNER JOIN #current_table# CT ON (OT.ValueId=CT.SyncId)
WHERE CT.ValueId = #current_result_id#
END