Home > other >  Unfamiliar SQL Syntax in a Scripted-Out table script
Unfamiliar SQL Syntax in a Scripted-Out table script

Time:09-02

I've jut recently scripted out a table from an existing DB on one of our SQL Servers, which brings back the following glorious result:

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[queuemembership](
    [Id] [uniqueidentifier] NOT NULL,
    [versionnumber] [bigint] NULL,
    [queueid] [uniqueidentifier] NULL,
    [queuemembershipid] [uniqueidentifier] NULL,
    [systemuserid] [uniqueidentifier] NULL,
 CONSTRAINT [EPK[dbo]].[queuemembership]]] PRIMARY KEY CLUSTERED 
(
    [Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO

I'm not familiar with the EPK bit of CONSTRAINT [EPK[dbo]].[queuemembership]]] PRIMARY KEY CLUSTERED

If I recreate the table from scratch I can replace EPK with other text, which makes me think this is some sort of external metadata or something.

Can anyone shed any light on this?

CodePudding user response:

I've covered this in the comments, but to put it into an answer, [EPK[dbo]].[queuemembership]]] is a delimit identified and properly escaped value for an object (specifically a CONSTRAINT) called EPK[dbo].[queuemembership].

This is, in truth, a very poorly named object; it contains multiple different characters that require the name to need to be delimit identified and a multiple characters that need escaping. Any of the right brackets (]) need escaping, in the same way that you would a single quote (') in a literal string; double them up. Then, because of the said right brackets, as well as the left brackets ([) and the periods (.), which is a part separator, this means that the entire value needs to be delimit identified.

As a result, you end up with that value [EPK[dbo]].[queuemembership]]]. You can also double check this by running PRINT QUOTENAME(N'EPK[dbo].[queuemembership]');.

Honestly, I suggest naming you CONSTRAINT something better, that doesn't need delimit identification or escaping. PK_queuemembership is likely more than fine.

  • Related