4

I'm currently debugging an SQL query that deletes through C# an item in a SQL server table based on a number of parameters. Currently this statement is failing.

One of the parameters supplied is based on a version ID of the item.

The SQL parameter for this version ID is:

new SqlParameter("@versionId", SqlDbType.Int, 4)

If, for example, the query was supplied with the version ID 51096, would it fail on this integer parameter with a set length of '4'?

2
  • @ Serpiton. I'm on a virtual machine with limited connections. I'm currently 'debugging' by sight and was just after a quick yes or no elimination. Commented Apr 10, 2014 at 8:59
  • You're misunderstanding what the length means. It's completely ignored for types with fixed length (which includes int). You shouldn't pass it to the SqlParameter constructor at all. And even if it did have an effect on integer, it would still mean four bytes (about -2G to +2G range), not four decimal digits. On the other hand, if you use e.g. char, you would get four decimal digits (if that's how you store it), but even that wouldn't cause an error - it would truncate the value. Commented Apr 10, 2014 at 9:46

1 Answer 1

3

No it won't fail. Even if you put 0 here, in size, it will work but if your db type is varchar or nvarchar then it will truncate your value to the supplied size, in this case 4.

If your parameter value is more than what an int can hold (2,147,483,647) then it will throw an error at runtime.

Sign up to request clarification or add additional context in comments.

1 Comment

Excellent answer thank you. It turned out to be an incorrect ID.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.