Question

The data type keyword "UNIQUEIDENTIFIER" is a chore to type. I want a shorthand replacement.

At first glance, it seems that SSMS provides two; the words "GUID" and "UID" are both rendered in blue in the query editor (using the default syntax color scheme).

But they are not listed as reserved or future keywords in Microsoft Docs. I tried to use them like non-reserved data type keywords (such as "DATE" or "INT")

Declare @guid uid;

and got this error message:

Msg 2715, Level 16, State 3, Line 1 Column, parameter, or variable #1: Cannot find data type uid. Parameter or variable '@guid' has an invalid data type.

I can create a UDT for UNIQUEIDENTIFIER named "UID". The result appears to behave as a non-reserved keyword:

Create Type [uid] From uniqueidentifier null;
GO
Declare @guid uid = newid();
Print @guid;
GO

B398DA7A-0DF2-49B6-8415-82D31EAA9013

So I have two questions.

  1. Are the words "GUID" and "UID" actually keywords, or does SSMS color them for a different reason?
  2. What considerations/risks might come with using the words "GUID" or "UID" as UDT names? I'm mostly worried about future tSQL language compliance.
Was it helpful?

Solution

I can't tell you the why behind Microsoft included GUID and UID in their reserved words list, but they have which is why they're color coded as blue in SSMS. Probably for future proofing in case they do decide to actually implement them as functional keywords or to match other database systems standards, but at this time I don't know of any functional purpose to those two words.

It's best practice and recommended to avoid using reserved words when naming things to avoid any potential complications, which for those two words are unknown at this time, and will depend on if Microsoft ever does implement meaning behind them. If you were to use them in your UDT name then you should always reference them with the square brackets to minimize potential issues down the road. But my personal recommendation is to avoid them all together and pick a different name such as UG.

OTHER TIPS

The words guid and uid are coloured like any other object or possible column in a SQL statement in SSMS.

When typing out the columns in the following statement, each column is coloured blue, because it is a column from the tables I am referencing.

SELECT sao.name AS object_name,
       sao.type_desc AS object_type,
       sac.name AS column_name
FROM   sys.all_columns sac
       JOIN sys.all_objects sao
            ON  sac.object_id = sao.object_id
WHERE  sac.name LIKE 'guid'
       OR  sac.name LIKE 'uid' 

Coloured as per my SSMS:

colour coded statement in SSMS

The above statement queries all objects in the sys views all_objects and all_columns and displays the corresponding object type.

+--------------------------------------+----------------------------------+-------------+
|             object_name              |           object_type            | column_name |
+--------------------------------------+----------------------------------+-------------+
| sysguidrefs                          | SYSTEM_TABLE                     | guid        |
| sysobjects                           | VIEW                             | uid         |
| sysusers                             | VIEW                             | uid         |
| systypes                             | VIEW                             | uid         |
| sysprotects                          | VIEW                             | uid         |
| sysprocesses                         | VIEW                             | uid         |
| cryptographic_providers              | VIEW                             | guid        |
| syscacheobjects                      | VIEW                             | uid         |
| fn_trace_gettable                    | SQL_INLINE_TABLE_VALUED_FUNCTION | GUID        |
| dm_cryptographic_provider_properties | VIEW                             | guid        |
| dm_xe_packages                       | VIEW                             | guid        |
+--------------------------------------+----------------------------------+-------------+

As you can see, there are a couple of columns named either guid or uid in a SQL Server instance, that could possibly be referenced in a statement and this is why SSMS will colour code them blue.

Licensed under: CC-BY-SA with attribution
Not affiliated with dba.stackexchange
scroll top