Reputation: 1567
I have a stored procedure which inserts an entity and returns its new UUID, and while I can generate the code to create the right query, it errors out when using go-sql-driver/mysql
. So the following code...
func test_insert() *sql.Rows {
db := openDbConnection()
defer db.Close()
results, err := db.Query("call saveargument(null, 'Test Argument', 'Test Argument', '1', null, null, null, 1, 'test_user', @newargumentid);\nselect @newargumentid;")
toolkit.HandleError(err)
return results
}
func openDbConnection() *sql.DB {
var db, err = sql.Open("mysql", getConnectionString(entities.GetConfig()))
toolkit.HandleError(err)
return db
}
... produces the following error:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'select @newargumentid' at line 2
I'm not sure why such a basic piece of SQL could be so problematic. Any insights anyone?
Upvotes: 2
Views: 1663
Reputation: 469
I just tested @Bill's answer and I can confirm it does not work as at Go1.14. My workaround was to rewrite my stored procedure to "return" values by doing a select at the end:
CREATE PROCEDURE foo()
BEGIN
# do stuff...
SELECT 'bar';
END;
And then in Go just read it like any other query:
res, _ := db.Query("CALL foo()")
res.Next()
var bar string
res.Scan(&bar)
println(bar)
Note this method also works for multiple columns and multiple rows.
Upvotes: 0
Reputation: 562260
You can't run more than one statement in a single db.Query()
call. This is true of most query interfaces in all programming languages.
Call your stored procedure in one call to db.Query()
, then query select @newargumentid
in a second call to db.Query()
.
Upvotes: 3