The sysname datatype defaults to not NULL.

4

August 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';

4 thoughts on “The sysname datatype defaults to not NULL.

  1. Chad Estes says:

    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 :).

  2. […] Kenneth Fisher discovers a secret of the sysname datatype: […]

  3. […] 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 […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Enter your email address to follow this blog and receive notifications of new posts by email.

Join 3,755 other subscribers

Follow me on Twitter

ToadWorld Pro of the Month November 2013
%d bloggers like this: