go-mssqldb icon indicating copy to clipboard operation
go-mssqldb copied to clipboard

Stored Procedure returned status code is not extracted immediately when calling through QueryContext

Open mihailboev opened this issue 2 years ago • 1 comments

Given a procedure that returns both a status code and a recordset, e.g.

ALTER procedure [dbo].[LoadConfigs]
as
set nocount on

select
	[IdConfig],
	[Value]
from dbo.Config

return 101

called as

var rs mssql.ReturnStatus
rows, err := db.QueryContext(ctx, "theproc", &rs)

if err != nil {
	return err
}

if rs != 0 {
	return errors.New("SP returned non-zero status")
}

for rows.Next() {
	err = rows.Scan(&val)
	//Do stuff with val
}

we are expecting that the return status will be set immediately after QueryContext returns with no errors (based on the implementation in mssql.go, processQueryResponse())

What we are observing is that the return status remains zero because processQueryResponse() breaks its parsing of tokens as soon as it encounters a []columnStruct token.

Note: Return status is then filled by Rows.Next() when it encounters a Return Status token.

mihailboev avatar Dec 02 '22 15:12 mihailboev

It is not a bug. You'd better get error from Rows.Error() after Rows.Next()

Breeze0806 avatar Apr 21 '23 14:04 Breeze0806