LastInsertId is returning 0, when using database/sql package with oracle driver


I am using database/sql package with oracle driver (“”), when I am inserting the data, it’s LastInsertId Method is returning 0, while data is successfully inserted.
Attaching code.

import (

_ ""


func main() {
conn, err = sql.Open(“ora”, username+"/"+password+"@"+host+":"+port+"/"+sid)
query := “INSERT INTO Table (C2) VALUES (:C2)”
result, err := conn.Exec(query, “Test”)
if err!= nil {
lastId := result.LastInsertId() // returning 0

Please tell me why this is happening?

result.LastInsertId() should also return an error as a second value, please check if it is non-nil. Not all databases support this function.

As you do not use the second value in the code pasted, I’m not even sure if it compiles at all, and I can not test as I’m in a mobile.


Please also read

1 Like

It is bette idea to use an SP and in this SP return the last id

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.