Skip to content

Instantly share code, notes, and snippets.

@chilismaug
Last active September 16, 2021 14:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save chilismaug/83ac49bde56b9acd9123a991f01e2bc7 to your computer and use it in GitHub Desktop.
Save chilismaug/83ac49bde56b9acd9123a991f01e2bc7 to your computer and use it in GitHub Desktop.
Phil Factor's Hiearchy table declaration for JSON werx
-- Create the data type IF EXISTS (SELECT * FROM sys.types WHERE name LIKE 'Hierarchy')
DROP TYPE dbo.Hierarchy
go
/****** Object: UserDefinedTableType [dbo].[Hierarchy] Script Date: 4/14/2020 8:48:14 AM ******/
CREATE TYPE [dbo].[Hierarchy] AS TABLE(
[element_id] [int] NOT NULL, /* internal surrogate primary key gives the order of parsing and the list order */
[sequenceNo] [int] NULL, /* the place in the sequence for the element */
[parent_ID] [int] NULL, /* if the element has a parent then it is in this column. The document is the ultimate parent, so you can get the structure from recursing from the document */
[Object_ID] [int] NULL, /* each list or object has an object id. This ties all elements to a parent. Lists are treated as objects here */
[NAME] [nvarchar](2000) NULL, /* the name of the object, null if it hasn't got one */
[StringValue] [nvarchar](max) NOT NULL, /*the string representation of the value of the element. */
[ValueType] [varchar](10) NOT NULL, /* the declared type of the value represented as a string in StringValue*/
PRIMARY KEY CLUSTERED
(
[element_id] ASC
)WITH (IGNORE_DUP_KEY = OFF)
)
GO
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment