The sysname datatype defaults to not NULL.

3

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

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

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 )

Google photo

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

Twitter picture

You are commenting using your Twitter 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,569 other followers

Follow me on Twitter

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