Вопрос по sql – Создание подкаталога через SQL INSERT с использованием FileTable

9

Error: User Rate Limit ExceededError: User Rate Limit ExceededFileTable without using File I/O APIsError: User Rate Limit Exceededparent_path_locatorError: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

INSERT INTO FileTable0 (name,is_directory,is_archive) VALUES ('Directory', 1, 0);

Error: User Rate Limit Exceeded

Ваш Ответ

4   ответа
10

GetPathLocator()path_locatorhierarchyids.

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);

hierarchyidutilizing newid()GetNewPathLocator()hierarchyid.GetDescendant()

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

GetNewPathLocator()getNewIDnewid().

create view dbo.getNewID as select newid() as new_id 

GetNewPathLocator()hierarchyidhiearchyid.ToString()hierarchyid

SELECT dbo.GetNewPathLocator(DEFAULT); -- returns /260114589149012.132219338860058.565765146/
SELECT dbo.GetNewPathLocator('/260114589149012.132219338860058.565765146/'); -- returns /260114589149012.132219338860058.565765146/141008901849245.92649220230059.752793580/
0

The function returns hierarchyid instead of string

If there is a parent, hierarchyid::GetReparentedValue function is used to generate a new ID instead of string concatenation.

create function doc.GetNewPathLocator (@parent hierarchyid = null) returns hierarchyid
as
begin 
    declare @id uniqueidentifier = (select new_id from dbo.GetNewID);
    declare @path hierarchyid = (convert(hierarchyid, '/' + 
            convert(varchar(20), convert(bigint, substring(convert(binary(16), @id), 1, 6))) + '.' +     
            convert(varchar(20), convert(bigint, substring(convert(binary(16), @id), 7, 6))) + '.' +     
            convert(varchar(20), convert(bigint, substring(convert(binary(16), @id), 13, 4))) + '/'));
    return case when @parent is null then @path else @path.GetReparentedValue(hierarchyid::GetRoot(), @parent) end;
end
go
0

0

DECLARE @pathID hierarchyid;
DECLARE @parentdir table(path hierarchyid not null);

IF NOT EXISTS(SELECT 1 FROM FileAsset WHERE is_directory = 1 AND file_stream.GetFileNamespacePath() = '\Assets\Test')
INSERT INTO FileAsset (name, is_directory) VALUES( 'Test', 1)

SELECT @pathID = FileAsset.path_locator FROM FileAsset WHERE file_stream.GetFileNamespacePath() = '\Assets\Test'

INSERT INTO FileAsset (name, file_stream) OUTPUT INSERTED.path_locator into @parentdir VALUES('MyDoc.txt', 0x)

UPDATE FileAsset SET path_locator = '/' + REPLACE(@pathID.ToString(), '/','') + path_locator.ToString() WHERE path_locator = (SELECT [path] FROM @parentdir)

CREATE PROCEDURE InsertFileAsset

    @fileName varchar(255),
    @dirName varchar(255),
    @data varbinary(MAX),
    @stream_id uniqueidentifier OUTPUT
AS
BEGIN
    DECLARE @pathID hierarchyid;
    DECLARE @parentdir table(path hierarchyid not null);
    DECLARE @streamID table(streamID uniqueidentifier not null);

    IF NOT EXISTS(SELECT 1 FROM FileAsset WHERE is_directory = 1 AND file_stream.GetFileNamespacePath() = '\Assets\' + @dirName)
    INSERT INTO FileAsset (name, is_directory) VALUES( @dirName, 1)

    SELECT @pathID = FileAsset.path_locator FROM FileAsset WHERE file_stream.GetFileNamespacePath() = '\Assets\' + @dirName

    INSERT INTO FileAsset (name, file_stream) OUTPUT INSERTED.path_locator into @parentdir VALUES(@fileName, @data)

    UPDATE FileAsset SET path_locator = '/' + REPLACE(@pathID.ToString(), '/','') + path_locator.ToString() OUTPUT inserted.stream_id INTO @streamID WHERE path_locator = (SELECT [path] FROM @parentdir)

    SELECT @stream_id = streamID FROM @streamID

    RETURN
END
GO
Error: User Rate Limit Exceeded

Похожие вопросы