'สำหรับ SQL Server และอื่นฯที่สนับสนุนคำสั่ง Merge
Public Shared Function SetInsertCommand(ByVal value As List(Of SEX_Model.AR_VAT)) As String
Dim s As New StringBuilder()
s.Append("MERGE INTO AR_VAT 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 Rem***** [,Space(1)]
s.Append(") AS S (BCode, BName) ") 'Source
s.Append("ON T.BCode = S.BCode ")
s.Append("WHEN MATCHED AND S.BName = 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.BName <> N'หอยไร้ขน' THEN ")
s.Append("INSERT (BCode, BName) VALUES (S.BCode, S.BName);")
Return s.ToString()
End Function