I receive the following error when I try to call err = row.Scan(&resourceList, resourceTypeId)
Scan error on column index 0, name 'ID': Scan not supported, storing driver.Value of type int64 to type *[]authService.Permission"SQL returns the following
type Permission struct { ID int `json:"id"` Name string `json:"name"` Description string `json:"description"` ParentResourceID int `json:"parentResourceId"` } func GetResourcesByResourceTypeId(resourceTypeId string) ([]Permission, string, error) { db, ctx := db.GetDB() query := "CALL usp_GetParentResourceListByResourceTypeID(?)" var resourceList []Permission stmt, err := db.Prepare(query) defer stmt.Close() if err != nil { log.Errorln("Error in preparing statement. " + err.Error()) return nil, "Error in preparing statement.", err } row := stmt.QueryRowContext(ctx, resourceTypeId) err = row.Scan(&resourceList, resourceTypeId) if err == nil { return resourceList, "Resource retrieval.", nil } log.Warningln("Resource retrieval failed, ResourceTypeID: " + resourceTypeId + ".") return resourceList, "Resource retrieval failed.", nil }
ID Name 15 Applications 16 Subscriptions 17 PaymentsThe same query works fine when I try to use SQL Server with
EXEC statement in
query.
There are some issues here. First
QueryRowContext
Your question indicates that your statement returns multiple results, so this is not the correct function to use (
QueryContext
would be more appropriate).The second problem is as stated in the error:
The first column in the result set is an integer (in this case probably the value
15
), and you are trying to scan it into[]Permission
. if you changevar resourceList []Permission
tovar resourceList int
The bug will be fixed (but the second parameter also needs to work).View this example documentation. Taking that code and applying it to your situation will result in something like the following (untested; just to point you in the right direction):
Note: Your structure
Permission
contains four elements, but the query returns two columns, so I'm not quite sure how you intend to populate the other two columns (or what the mapping is) .