Questo è quello che ho finito con l'utilizzo di creare una sottodirectory dal GetPathLocator()
non genererà un nuovo valore path_locator
per me - interpreterà solo esistente hierarchyids
.
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 sopra utilizza la default path_locator value discovered here che costruisce una nuova rappresentazione hierarchyid
da un GUID (utilizzando newid()
metodo e semplice analisi). La funzione GetNewPathLocator()
non esiste da nessuna parte in SQL Server che ho trovato (hierarchyid.GetDescendant()
è la più vicina che ho trovato, ma non ha utilizzato la struttura nativa che FileTable si basa su). 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 la richiesta di newid()
utilizzando il trick from this SO post.
create view dbo.getNewID as select newid() as new_id
Per chiamare GetNewPathLocator()
, è possibile utilizzare il parametro di default che genererà un nuovo hierarchyid
o di passare in una rappresentazione hiearchyid
stringa esistente (.ToString()
) per creare un bambino hierarchyid
come si vede qui sotto ...
SELECT dbo.GetNewPathLocator(DEFAULT); -- returns /260114589149012.132219338860058.565765146/
SELECT dbo.GetNewPathLocator('/260114589149012.132219338860058.565765146/'); -- returns /260114589149012.132219338860058.565765146/141008901849245.92649220230059.752793580/
Dopo alcuni test rigorosi, sembra l'istruzione di aggiornamento è incline a bloccaporte, quando è in parallelo. – Tod