I'm executing a stored procedure (using go-mssqldb driver), and I'm getting multiple sets with results and then selecting the output. This is the snippet:
query := "DECLARE @ErrorMessage nvarchar(4000);"+
"EXEC [Schema].[Action] "+
"@myInputParam=?,"+
"@ErrorMessage = @ErrorMessage OUTPUT;"+
"SELECT ErrorMessage = @ErrorMessage;"
ctx: ...
rows, err := r.DB.QueryxContext(ctx, query, "blabla")
in this way I can read and scan the results sets in the rows with the combination of rows.Next()
and rows.NextResultSet()
.
And in the end I Scan
the ErrorMessage in last set.
BUT it happens that the stored may return only the ErrorMessage set, breaking my expectations in the code.
So what I need is to always read the output first and then, if no error, proceed with the waltz of the reads and scans of the other sets.
I tried this way hoping to populate the var errorMessage:
QueryxContext(ctx, query, "blabla", sql.Named("ErrorMessage", sql.Out{Dest: &errorMessage}))
but it doesn't work and it complains with sql: statement expects 1 inputs; got 2
EDIT1
this is the stored I'm calling
CREATE PROCEDURE [Schema].[Action]
@myInputParam VARCHAR(100),
@ErrorMessage nvarchar(4000) OUTPUT
AS
BEGIN
SELECT @Id = Id
FROM ...
WHERE Param = @myInputParam
IF @Id IS NULL
BEGIN
SET @ErrorMessage = "some error";
END
END
GO