Skip to content

Commit 24b58d3

Browse files
Update connect-query-go.md
Replace tsql := "INSERT INTO TestSchema.Employees (Name, Location) VALUES (@name, @location); select convert(bigint, SCOPE_IDENTITY());" with tsql := "INSERT INTO TestSchema.Employees (Name, Location) VALUES (@name, @location); " tsql += select isNull(SCOPE_IDENTITY(), -1);" Premise:- 1) One cannot assume that insert succeeded and we have a valid scope_identity 2) if insert failed, then scope_identity will be null 3) Attempt to fetch later on and cast to int64, will subsequently fail var newID int64 err = row.Scan(&newID)
1 parent 83aa02c commit 24b58d3

File tree

1 file changed

+2
-1
lines changed

1 file changed

+2
-1
lines changed

articles/azure-sql/database/connect-query-go.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,8 @@ Get the connection information you need to connect to the database. You'll need
194194
return -1, err
195195
}
196196

197-
tsql := "INSERT INTO TestSchema.Employees (Name, Location) VALUES (@Name, @Location); select convert(bigint, SCOPE_IDENTITY());"
197+
tsql := "INSERT INTO TestSchema.Employees (Name, Location) VALUES (@Name, @Location); "
198+
tsql += select isNull(SCOPE_IDENTITY(), -1);"
198199
199200
stmt, err := db.Prepare(tsql)
200201
if err != nil {

0 commit comments

Comments
 (0)