本站源代码
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.

32 lines
565B

  1. // +build !go1.10
  2. package mssql
  3. import (
  4. "database/sql/driver"
  5. "errors"
  6. )
  7. func (r *Result) LastInsertId() (int64, error) {
  8. s, err := r.c.Prepare("select cast(@@identity as bigint)")
  9. if err != nil {
  10. return 0, err
  11. }
  12. defer s.Close()
  13. rows, err := s.Query(nil)
  14. if err != nil {
  15. return 0, err
  16. }
  17. defer rows.Close()
  18. dest := make([]driver.Value, 1)
  19. err = rows.Next(dest)
  20. if err != nil {
  21. return 0, err
  22. }
  23. if dest[0] == nil {
  24. return -1, errors.New("There is no generated identity value")
  25. }
  26. lastInsertId := dest[0].(int64)
  27. return lastInsertId, nil
  28. }
上海开阖软件有限公司 沪ICP备12045867号-1