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

Analyzer to warn against SQL varchar parameters without a max length #86

Open
bretehlert opened this issue Aug 11, 2019 · 1 comment
Open

Comments

@bretehlert
Copy link

bretehlert commented Aug 11, 2019

When constructing a SqlParameter or adding a parameter to an SqlParameterCollection, and the parameter type is (n)varchar, a max length should always be specified. If a max length is not specified then the length of the value is used causing a different execution plan each different length of parameter value.

Examples of violations include:

new SqlParameter("@parameter", SqlDbType.VarChar) { Value = value },
new SqlParameter("parameter", value)
cmd.Parameters.AddWithValue("parameter", value);
cmd.Parameters.Add("parameter", SqlDbType.VarChar)
@yaakov-h
Copy link
Member

Does setting the size to -1 specify a max-length execution plan every time, or does SQL still generate different execution plans based on the string length?

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

No branches or pull requests

2 participants