I have created a stored procedure which takes a parameter of type varchar(5)
. My stored procedure was working fine, and returning the correct result, until the time I passed it a string of 6 or more characters.
What happened is that it ignored 6th onward character, and returned result only based on first 5 characters, which was a wrong result. I expect it to throw an error when I am passing a longer string.
Is this a bug or there is way to change this behavior of SQL Server?
create procedure usp_testproc
@param1 varchar(5)
as
begin
if @param1 = '12345'
begin
select 'you got this right'
end
else
begin
select 'String Mismatch'
end
end
No matter whether we call
exec usp_testproc '12345'
or
exec usp_testproc '123456'
we get the same result
varchar(5)
It means you will get only the first 5 chars so it will ignore the rest, the number inside of the "()" shows how many symbols you will store in this param. You can ensure yourself with the longest possible string that can be returned from your procedure and it will be fine Here's a little extra that you can read : char and varchar (docs.microsoft)
You need to specify the correct type of the input parameter:
create procedure usp_testproc @param1 varchar(<max number of chars here>)
as...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With