So I did some tests and came to somewhat of a conclusion regarding the limitations of hierarchyid
:
If I run for example the following code:
DECLARE @i BIGINT = 1
DECLARE @h hierarchyId = '/'
WHILE 1=1
BEGIN
SET @h = @h.ToString() + '1/'
PRINT CONVERT(nvarchar(max), @i)
SET @i = @i+1
END
I will get to 1427 levels deep before I get an error. Since I am using the value 1
for each level, this ought to be the most compact tree from which I draw the conclusion that I will not ever be able to create a tree with more than 1427 levels.
However, if I use for example 99999999999999
for each level (eg. /99999999999999/99999999999999/99999999999999/...
, the error occurs already at 118 levels deep. It also seems that 14 digits are the maximum for an id at each level, since it fails immediately if I use a 15 digit number.
So with this in mind, if I only use whole integer identifiers (i.e. don't insert nodes between other nodes etc.) I should be able to guarantee up to at least 100 levels deep in my scenario, and at no time will I be able to exceed much more than 1400 levels.