Quick test. Why won’t this return the string “Hi”?
CREATE PROC dbo.Testing @Val VARCHAR AS IF @Val = 'One' SELECT 'Hi' AS msg; GO
EXEC dbo.Testing @Val = 'One';
Try it yourself, and answer in the comments. I’ll update this (in comments) in a week or so with the answer, if no one’s gotten it.
And yes, I did this exact thing. You’re never too old to do something like this.
It doesn’t select “One”, it selects “Hi”.
[Editor: This answer was right, until I edited the blog to ask the correct question.]
You’re right, but that was a mistake on my part. I meant to ask why doesn’t it select “Hi”.
Because you are not specifying length
Yeah. It’s almost embarrassing how long it took me to notice that.
The definition parameter @Val needs to include a length, like
@Val VARCHAR(16)
Otherwise, @Val is a VARCHAR(1)
Correct!
The default length for varchar (and char) in variable declaration = 1
Needs to define the varchar length! Easy to see in a tiny query … easy to miss in a larger script.