You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
31 lines
565 B
31 lines
565 B
// +build !go1.10
|
|
|
|
package mssql
|
|
|
|
import (
|
|
"database/sql/driver"
|
|
"errors"
|
|
)
|
|
|
|
func (r *Result) LastInsertId() (int64, error) {
|
|
s, err := r.c.Prepare("select cast(@@identity as bigint)")
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
defer s.Close()
|
|
rows, err := s.Query(nil)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
defer rows.Close()
|
|
dest := make([]driver.Value, 1)
|
|
err = rows.Next(dest)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
if dest[0] == nil {
|
|
return -1, errors.New("There is no generated identity value")
|
|
}
|
|
lastInsertId := dest[0].(int64)
|
|
return lastInsertId, nil
|
|
}
|
|
|