-
Notifications
You must be signed in to change notification settings - Fork 494
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Insert a new row and get the auto generated id in the same call #487
Comments
I can work on this. |
I switched to minus5/gofreetds driver, installed freetds and LastInsertId() was returning the correct last generated id... Then I switched back to denisenkom/go-mssqldb and magically LastInsertId() started to work... Now I'm totally confused ... :) Before I haven't had a freetds installed.. Ok uninstalled freetds and LastInsertId() is still working :) No idea what's going on :) |
I have no idea what your issue was, but I'm glad to hear it's working. I'll still work on creating an example for this scenario. |
Please be aware that there is no 100% correct way with SQL server to return a LastInsertID without modifying the SQL and returning it in the select statement. If you are using SQL Server DO NOT USE OR RELY on LastInsertID. You should use something like:
|
Alright I'll explain what i'm doing
|
Not magical, each connection is a unique session. calling db.Conn(), then calling exec then scope_identity on the same connection is what you want to do. Making a Tx is another way to do that. |
I see but how to execute this? With a db.Exec or with something else? db.Exec doesn't work. |
There is no such thing as "exec" in SQL Server. Exec for the DB interface just means disregard all rows. |
The error will also be hidden if I replace the
They both do not actually insert the duplicate rows into the table . Any suggestion about how to capture the raw database error when insert duplicated rows ? |
@scbizu What is your table schema? What is the query you're running? Why can't duplicate rows be insert into the table (I'm guessing there is a unique column in your table)? I tried both using The following is the queries I tried to run:
and
where baz is a unique column. |
@v-kaywon Are you sure the first case error is the duplicated error ? I got the conversion error even if I use the rows, err := db.Query("MY INSERT QUERY; select ID = convert(bigint, SCOPE_IDENTITY())")
if err != nil {
return err
}
defer rows.Close()
if !rows.Next() {
if err:= rows.Err();err!=nil{
return err
}
return INSERTID_NOT_FOUND_ERROR
}
var id int64
err := rows.Scan(&id); err!=nil {
return err
} As for Anyway, Thanks for your help. |
I doubled checked and tried again with the query |
Hi, I'm also having this problem, and able to reproduce using this code:
func TestDriver(t *testing.T) {
db, err := sql.Open("mssql", dsn())
fmt.Println(db, err)
fmt.Println(db.Ping())
defer db.Close()
schema := "CREATE TABLE [dbo].[Foo3]([ID] [int] IDENTITY(1,1) NOT NULL,[Name] [varchar](100) NOT NULL,CONSTRAINT [PK_Foo3] PRIMARY KEY CLUSTERED ([ID] ASC)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY],CONSTRAINT [IX__Name] UNIQUE NONCLUSTERED ([Name] ASC)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]) ON [PRIMARY]"
_, err = db.Exec(schema)
fmt.Println(err)
// first record, return nil is expected
_, err = db.Query("INSERT INTO Foo3([Name]) "+
"VALUES (?)", "foo")
fmt.Println("first record: ", err) //nil
// duplicate record, error returned
_, err = db.Query("INSERT INTO Foo3([Name]) "+
"VALUES (?)", "foo")
fmt.Println("duplicate record no id returned: ", err) // mssql: Violation of UNIQUE KEY constraint
// duplicate record with output inserted, no error returned???
_, err = db.Query("INSERT INTO Foo3([Name]) OUTPUT INSERTED.ID "+
"VALUES (?)", "foo")
fmt.Println("duplicate with inserted: ", err) // nil??
// duplicate record with output inserted, no error returned???
_, err = db.Query("INSERT INTO Foo3([Name]) "+
"VALUES (?); select ID = convert(bigint, SCOPE_IDENTITY())", "foo")
fmt.Println("duplicate with select id: ", err) // nil??
// works: error returned
_, err = db.Query("INSERT INTO Foo3([Name]) OUTPUT INSERTED.ID " +
"VALUES ('foo')")
fmt.Println("duplicate with inserted, literal only: ", err) //Real error!
// duplicate record with output inserted, no error returned???
_, err = db.Exec("INSERT INTO Foo3([Name]) OUTPUT INSERTED.ID "+
"VALUES (?)", "foo")
fmt.Println("duplicate with inserted using exec: ", err) // nil??
// duplicate record with output inserted, no error returned???
_, err = db.Exec("INSERT INTO Foo3([Name]) "+
"VALUES (?); select ID = convert(bigint, SCOPE_IDENTITY())", "foo")
fmt.Println("duplicate with select id using exec: ", err) // nil??
} output:
it seems |
Hi,
Can someone post an example how to do this?
The text was updated successfully, but these errors were encountered: