Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Behavioural differences between sql.Named with query vs NamedExecContext cause UPDATE error #916

Open
koalazub opened this issue Mar 5, 2024 · 0 comments

Comments

@koalazub
Copy link

koalazub commented Mar 5, 2024

Tools:
go 1.21
github.com/gorilla/schema v1.2.1
github.com/jmoiron/sqlx v1.3.5

I'm currently getting an error when using named parameters for my Azure SQL Database.

Contrived, but when performing the following:

	updateQry := `UPDATE tblIdNumber SET
    Cust1 = @Cust1
WHERE IdNumber = @IdNumber 
`

	_, err := d.DB.NamedExecContext(ctx, updateQry, docket)
	if err != nil {
		slog.Error("couldn't update IdNumber", err)
		return err
	}

I get an error:

mssql: Must declare the scalar variable "@CUST1"."

Yet when I do:

	updateQry := `UPDATE tblIdNumber SET
    Cust1 = @Cust1
WHERE IdNumber = @IdNumber 

	_, err := d.DB.ExecContext(ctx, updateQry,
		sql.Named("Cust1", Person.Cust1),
	)
	if err != nil {
		slog.Error("couldn't update docket", err)
		return err
	}

It correctly updates the table.

I had the expectation that it removed the need to explicitly state each named parameter but that doesn't seem to be the case in this context. I am pretty new to sqlx, so I'm not sure if I'm missing something here. I did see that there were some fixes to named params in #406 for SQL Server

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant