Monday, 26 August 2013

nvarchar change max size on the fly

nvarchar change max size on the fly

I've always been bothered by the need for max lengths on SQL string
columns. There is some data for which there is no true max length. For
example, let's say you have a field to store someone's first name, and you
make it NVARCHAR(50). It's always possible (although highly unlikely) that
someone has a name longer than 50 chars.
Would it be feasible to change the field's max length on the fly? What I
mean by this is, when you do an INSERT/UPDATE, you check if the person's
name is longer than 50 chars, and ALTER the table if need be before you do
the INSERT/UPDATE. (Or perhaps, catch an exception and perform an ALTER if
need be).
Would the ALTER be a slow operation if the table had a lot of data in it?
Let's say you alter the column to be NVARCHAR(100). Would a SELECT from
this table be slower than if you'd made it NVARCHAR(100) from the
beginning?

No comments:

Post a Comment