2

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.

3 Answers 3

2

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.

8
  • 1
    Thanks for the input. I suppose you're right that it's still better to play it safe even if there's no formal "why" from MS. I was hoping to make use of the coloring for "UID", but I just realized I can probably change my SSMS settings for UDT color.
    – KevinP
    Mar 2, 2021 at 1:22
  • @KevinP No problem, always better to play it safe and follow best practice over laziness (which as developers we all are :). Yup, if you like the color coding, you can definitely adjust the settings in SSMS so it flags your UDT and color codes it accordingly too. :)
    – J.D.
    Mar 2, 2021 at 1:32
  • 2
    Don't trust the pretty colors in SSMS. Neither GUID nor UID is in the reserved keyword list, present or future.
    – Dan Guzman
    Mar 2, 2021 at 3:28
  • @DanGuzman Neither is UNIQUEIDENTIFIER :-/ Mar 2, 2021 at 15:32
  • @FernandoSibaja, good point. SQL Server data type names are not necessarily reserved because they are not keywords .
    – Dan Guzman
    Mar 2, 2021 at 16:14
2

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.

3
  • 1
    I think when you said "any other object or possible column" you actually possibly meant "any other system object or possible system column"?...as user defined columns are by default black colored in a SQL query with the out-of-the-box settings in SSMS.
    – J.D.
    Mar 2, 2021 at 16:58
  • 1
    @J.D: Thanks for noticing the fine-tuning.
    – John K. N.
    Mar 2, 2021 at 19:06
  • @JohnK.N., An interesting observation that may explain uid and guid. But further tuning is needed, as not any possible system column is colored blue. Ex. twin sys cols precision (blue) and scale (black). Only precision is a reserved keyword. Maybe there is no unpublished non-reserved (key)words list, and it just comes down to arbitrary choices by SSMS team? I know I can't always explain my personal coding style in a systematic way. Side note, sys col uid is smallint, guid is uniqueidentifier. Side side note, Uid and Guid are my favorite Tolkien dwarves.
    – KevinP
    Mar 4, 2021 at 18:38
0

I stumbled across another partial answer as to why guid gets color. It is a keyword used in ODBC escape sequences.

Martin Smith - Example Usage

MS Docs - ODBC SQL Data Types

I haven't tracked down an answer for uid yet, but at least my confidence is restored in Microsoft (perhaps foolishly) that there is meaning behind the colorization, not mere whim.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.