Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
I have a query like this
GoLang
var db, err = sql.Open("mssql", connString)
	if err != nil {
		log.Println("Open connection failed:", err)
	}
	err = db.Ping()
	if err != nil {
		log.Println("Cannot connect: ", err)
	}
	defer db.Close()

	if err != nil {
		fmt.Println(err.Error())
		return "Failed to Connect to Database", err
	}
	tx, err := db.Begin()
	var query string
	query = `	`
   
_, err = db.Exec(query)
	if err != nil {
		return err.Error(), nil
	}

for _, statusHistory := range data.StatusHistory {
		var querydetail = ``
}
	_, err = db.Exec(querydetail)
		if err != nil {
			return err.Error(), nil
		}
	}
	if err != nil {
		tx.Rollback()
		return err.Error(), nil
	} else {
		tx.Commit()
		return "success to  Insert Data : ", nil
	}

}

the problem is when querydetail is failed i want to also rollback the query

how to solve that?

What I have tried:

I Have try so many different way,

please Help me
Posted
Updated 9-Jan-24 2:00am
v2
Comments
Richard Deeming 9-Jan-24 8:03am    
Well, you're missing a tx.Rollback() from your inner if err != nil { block; that's where I'd start looking.

NB: I don't know GoLang, but to me it looks like the code that executes queryDetail is outside of the for loop, whereas the queryDetail variable is defined inside the for loop. And all of your queries seem to be missing.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900