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

Multiple query execution plans for query with named parameters #803

Open
nicolaepopa opened this issue Jul 4, 2024 · 2 comments
Open

Multiple query execution plans for query with named parameters #803

nicolaepopa opened this issue Jul 4, 2024 · 2 comments

Comments

@nicolaepopa
Copy link

nicolaepopa commented Jul 4, 2024

I am using named parameters to build my queries:

`queryString := 'insert into sch.MyTable(Field1,Field2,Field3) values(@field1,@field2,@field3)'

_, err := sn.DbConn.Exec(queryString,
sql.Named("Field1", myStruct.Field1),
sql.Named("Field2", myStruct.Field2),
sql.Named("Field3", myStruct.Field3)
)`
All fields are strings
Running this multiple times with different sizes of strings result in multiple query plans:

`(@field1 nvarchar(4),@field2 nvarchar(16),@field3 nvarchar(16))

INSERT INTO sch.MyTable (Field1,Field2,Field3)

VALUES (@field1, @field2,@field3);`

The size of the nvarchar field declaration varies depending on the size of the input string and SQL creates a plan for each variation.
Is there something that I can do that would result in a single query plan for this query?

@nicolaepopa
Copy link
Author

@shueybubbles
Copy link
Contributor

AFAIK making this work requires moving away from the Go types to a parameter type that's driver-specific which contains the length/size specification. See microsoft#200

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

2 participants