denisenkom / go-mssqldb

Microsoft SQL server driver written in go language
BSD 3-Clause "New" or "Revised" License
1.82k stars 499 forks source link

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

Open mihailboev opened 1 year ago

mihailboev commented 1 year ago

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.

Breeze0806 commented 1 year ago

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