Friday, June 27, 2008

SQL Server: how to prohibit empty strings globally?

We'd like to prohibit empty strings by SQL Server means (not
programmatically on C# level); only NULLs (for nullable fields) should
be allowed.

We can use

ALTER TABLE TableName ADD CONSTRAINT ck_FieldName CHECK ((FieldName <> '') OR (FieldName IS NULL))

But how to do it globally on the level of a database (or SQL Server
instance), so that it wouldn't be necessary to remember to add the
above constraint to each and every table?

(We are using SQL Server 2008.)