The sysname datatype defaults to not NULL.
4August 26, 2021 by Kenneth Fisher
The other day I was creating a table to store some metadata. Since the metadata I was collecting (sys.databases.name for example) uses the datatype sysname I used the same datatype. In case you didn’t know sysname is an nvarchar(128) user defined datatype used in a number of the system views.
Anyway, I was a bit surprised (and later realized I probably shouldn’t be) that the default for sysname is to not allow nulls.
CREATE TABLE sysname_test (
Col1 sysname
,Col2 nvarchar(128)
);
GO
EXEC sp_help sysname_test;
GO
DROP TABLE sysname_test;
GO

Now you can override that if you need to, it’s just the default, but the final definition for sysname is going to be:
sysname := nvarchar(128) NOT NULL
Which you can see pretty easily by looking in the sys.types system view.
SELECT * FROM sys.types
WHERE NAME = 'sysname';

You’ve piqued my interest; why would you need/want a nullable field of the sysname type? I would think you’d only use this for table, index, column, etc. names, so I’d be curious to know why any of these values would be null.
In this case I’m saving a bunch of metadata and occasionally I’ll have a row that’s missing a piece of information. I’d be surprised to see a nullable sysname in a system view, but when I’m using it in my own code it happens occasionally :).
[…] Kenneth Fisher discovers a secret of the sysname datatype: […]
[…] because that’s usually plenty. You might need more (or even less) or you could even use sysname. I typically use original_login() because it ignores impersonation. However, depending on how your […]