MSSQL Upsert statement
This commit is contained in:
parent
6bc6b1690c
commit
9bafa2f158
7 changed files with 89 additions and 41 deletions
queries
|
@ -283,6 +283,49 @@ func BuildUpsertQueryPostgres(dia Dialect, tableName string, updateOnConflict bo
|
|||
return buf.String()
|
||||
}
|
||||
|
||||
// BuildUpsertQueryMSSQL builds a SQL statement string using the upsertData provided.
|
||||
func BuildUpsertQueryMSSQL(dia Dialect, tableName string, primary, update, insert []string, output []string) string {
|
||||
insert = strmangle.IdentQuoteSlice(dia.LQ, dia.RQ, insert)
|
||||
|
||||
buf := strmangle.GetBuffer()
|
||||
defer strmangle.PutBuffer(buf)
|
||||
|
||||
startIndex := 1
|
||||
|
||||
fmt.Fprintf(buf, "MERGE INTO %s as [t]\n", tableName)
|
||||
fmt.Fprintf(buf, "USING (SELECT %s) as [s] ([%s])\n",
|
||||
strmangle.Placeholders(dia.IndexPlaceholders, len(primary), startIndex, 1),
|
||||
strings.Join(primary, string(dia.RQ)+","+string(dia.LQ)))
|
||||
fmt.Fprint(buf, "ON (")
|
||||
for i, v := range primary {
|
||||
if i != 0 {
|
||||
fmt.Fprint(buf, " AND ")
|
||||
}
|
||||
fmt.Fprintf(buf, "[s].[%s] = [t].[%s]", v, v)
|
||||
}
|
||||
fmt.Fprint(buf, ")\n")
|
||||
|
||||
startIndex = len(primary) + 1
|
||||
|
||||
fmt.Fprint(buf, "WHEN MATCHED THEN ")
|
||||
fmt.Fprintf(buf, "UPDATE SET %s\n", strmangle.SetParamNames(string(dia.LQ), string(dia.RQ), startIndex, update))
|
||||
|
||||
startIndex = len(primary) + 1 + len(update)
|
||||
|
||||
fmt.Fprint(buf, "WHEN NOT MATCHED THEN ")
|
||||
fmt.Fprintf(buf, "INSERT (%s) VALUES (%s)",
|
||||
strings.Join(insert, ", "),
|
||||
strmangle.Placeholders(dia.IndexPlaceholders, len(insert), startIndex, 1))
|
||||
|
||||
if len(output) > 0 {
|
||||
fmt.Fprintf(buf, "\nOUTPUT INSERTED.[%s];", strings.Join(output, "],INSERTED.["))
|
||||
} else {
|
||||
fmt.Fprint(buf, ";")
|
||||
}
|
||||
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func writeModifiers(q *Query, buf *bytes.Buffer, args *[]interface{}) {
|
||||
if len(q.groupBy) != 0 {
|
||||
fmt.Fprintf(buf, " GROUP BY %s", strings.Join(q.groupBy, ", "))
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue