mssql: use SCOPE_IDENTITY() if OUTPUT not possible

This commit is contained in:
tnclong 2019-12-06 20:17:44 +08:00
parent 26c261fb13
commit ae79e179fb
2 changed files with 6 additions and 2 deletions

View File

@ -100,8 +100,11 @@ func createCallback(scope *Scope) {
returningColumn = scope.Quote(primaryField.DBName) returningColumn = scope.Quote(primaryField.DBName)
} }
lastInsertIDReturningSuffix := scope.Dialect().LastInsertIDReturningSuffix(quotedTableName, returningColumn)
lastInsertIDOutputInterstitial := scope.Dialect().LastInsertIDOutputInterstitial(quotedTableName, returningColumn, columns) lastInsertIDOutputInterstitial := scope.Dialect().LastInsertIDOutputInterstitial(quotedTableName, returningColumn, columns)
var lastInsertIDReturningSuffix string
if lastInsertIDOutputInterstitial == "" {
lastInsertIDReturningSuffix = scope.Dialect().LastInsertIDReturningSuffix(quotedTableName, returningColumn)
}
if len(columns) == 0 { if len(columns) == 0 {
scope.Raw(fmt.Sprintf( scope.Raw(fmt.Sprintf(

View File

@ -207,7 +207,8 @@ func (mssql) LastInsertIDOutputInterstitial(tableName, columnName string, column
} }
func (mssql) LastInsertIDReturningSuffix(tableName, columnName string) string { func (mssql) LastInsertIDReturningSuffix(tableName, columnName string) string {
return "" // https://stackoverflow.com/questions/5228780/how-to-get-last-inserted-id
return "; SELECT SCOPE_IDENTITY()"
} }
func (mssql) DefaultValueStr() string { func (mssql) DefaultValueStr() string {