Dim s As New StringBuilder()
s.Append("MERGE INTO tblDetails AS T ") 'Target
s.Append("USING ( VALUES ")
For Each r In value
s.Append("(")
s.Append(r.Tran_Type & ", ")
s.Append(r.Tran_Code & ", ")
s.Append(r.Tran_Desc & ", ")
s.Append(r.ACCount_NO & ", ")
s.Append(r.Project)
s.Append("), ")
Next
s.Remove(s.Length - 2, 2) 'Remove End of , " " [, Space(1)]
s.Append(") AS S (BCode, BName) ") 'Source
s.Append("ON T.BCode = S.BCode ")
s.Append("WHEN MATCHED AND S.BCode = N'สิ่งที่ไม่ต้องการ' THEN ")
s.Append("DELETE ")
s.Append("WHEN MATCHED THEN ")
s.Append("UPDATE SET BName = S.BName ")
s.Append("WHEN NOT MATCHED BY TARGET AND S.BCode <> N'สิ่งที่ต้องการ' THEN ")
s.Append("INSERT (BCode, BName) VALUES (S.BCode, S.BName);")
Return s.ToString()