I have never thought of this question until recently I was troubled by one of my tasks:
So, in my store procedure, I declare a parameter @temp_address
with datatype VARCHAR
.
This @temp_address
will temporary store the result from multiple columns, for example:
SET @temp_address = (Select Address1 + Address2 + City + State + ZipCode + Country
FROM some_table)
Because I don't really know exactly the combined size of - now the complete - address, I simply use VARCHAR(MAX)
. However, my boss rejected my work and told me not to use MAX
, and I have to specify a size.
While it is possible for me to figure out the maximum possible size (by looking at each columns' size and sum them up), I'm just really curious, why do we need VARCHAR(N)
instead of just simple VARCHAR(MAX)
?
Same goes to NVARCHAR(MAX)
vs NVARCHAR(N)