Questo è ciò che ho finito per usare per creare una sottocartella, dato che GetPathLocator() non mi genera un nuovo valore path_locator — interpreterà solo hierarchyids esistenti.
DECLARE @parentdir table(path hierarchyid not null); DECLARE @subdir_locator hierarchyid -- Create Parent Directory, OUTPUT inserted parent path INSERT INTO FileTable0 (name,is_directory,is_archive) OUTPUT INSERTED.path_locator into @parentdir SELECT 'Directory', 1, 0 -- Create new path_locator based upon parent SELECT @subdir_locator = dbo.GetNewPathLocator(path) from @parentdir -- Create Subdirectory INSERT INTO FileTable0 (name,path_locator,is_directory,is_archive) VALUES ('subdirectory', @subdir_locator, 1, 0); Il blocco di codice qui sopra utilizza il valore predefinito di path_locator scoperto qui, che costruisce una nuova rappresentazione hierarchyid a partire da un GUID (utilizzando il metodo newid() e un semplice parsing). La funzione GetNewPathLocator() non esiste da nessuna parte in SQL Server, per quanto ho potuto trovare (hierarchyid.GetDescendant() è quanto di più vicino sono riuscito a trovare, ma non usava la struttura nativa su cui si basa FileTable). Forse in SQL.NEXT...
CREATE FUNCTION dbo.GetNewPathLocator (@parent hierarchyid = null) RETURNS varchar(max) AS BEGIN DECLARE @result varchar(max), @newid uniqueidentifier -- declare new path locator, newid placeholder SELECT @newid = new_id FROM dbo.getNewID; -- retrieve new GUID SELECT @result = ISNULL(@parent.ToString(), '/') + -- append parent if present, otherwise assume root convert(varchar(20), convert(bigint, substring(convert(binary(16), @newid), 1, 6))) + '.' + convert(varchar(20), convert(bigint, substring(convert(binary(16), @newid), 7, 6))) + '.' + convert(varchar(20), convert(bigint, substring(convert(binary(16), @newid), 13, 4))) + '/' RETURN @result -- return new path locator END GO La funzione GetNewPathLocator() richiede anche una vista SQL getNewID per richiedere un newid() usando il trucco da questo post su SO.
create view dbo.getNewID as select newid() as new_id Per chiamare GetNewPathLocator(), puoi usare il parametro predefinito che genererà un nuovo hierarchyid oppure passare una rappresentazione stringa di un hiearchyid esistente (.ToString()) per creare un hierarchyid figlio, come mostrato sotto...
SELECT dbo.GetNewPathLocator(DEFAULT); -- returns /260114589149012.132219338860058.565765146/ SELECT dbo.GetNewPathLocator('/260114589149012.132219338860058.565765146/'); -- returns /260114589149012.132219338860058.565765146/141008901849245.92649220230059.752793580/