是否可以将SQL FileTable path_locator转换回用于生成它的GUID

时间:2014-08-15 12:23:53

标签: sql filetable

我使用以下代码在SQL FileTable中创建路径

SET @path = '/' +
        CONVERT(VARCHAR(20), CONVERT(BIGINT, SUBSTRING(CONVERT(BINARY(16), @customerId), 1, 6))) + '.' +
        CONVERT(VARCHAR(20), CONVERT(BIGINT, SUBSTRING(CONVERT(BINARY(16), @customerId), 7, 6))) + '.' +
        CONVERT(VARCHAR(20), CONVERT(BIGINT, SUBSTRING(CONVERT(BINARY(16), @customerId), 13, 4)));

其中@customerId是UniqueIdentifier

稍后我需要检索文档以使用path_locator并从路径返回customerId。

我尝试过各种不同的方法,但我找不到解决办法。下面是我的TSQL,我从customerId开始并将其转换为路径,然后尝试再次转换回来。我可以获得标识符的BINARY(16)版本的字符串表示,但后来我卡住了。

最值得赞赏的任何帮助。

DECLARE @customerId     UNIQUEIDENTIFIER;
SET @customerId = '680EC8FA-F54C-44D6-9308-61F421E11E64';
SELECT @customerId AS customerId;

DECLARE @path   VARCHAR(512);
SET @path = '/' +
        CONVERT(VARCHAR(20), CONVERT(BIGINT, SUBSTRING(CONVERT(BINARY(16), @customerId), 1, 6))) + '.' +
        CONVERT(VARCHAR(20), CONVERT(BIGINT, SUBSTRING(CONVERT(BINARY(16), @customerId), 7, 6))) + '.' +
        CONVERT(VARCHAR(20), CONVERT(BIGINT, SUBSTRING(CONVERT(BINARY(16), @customerId), 13, 4)));

DECLARE @temp   VARCHAR(512);
DECLARE @part1  VARCHAR(20);
DECLARE @part2  VARCHAR(20);
DECLARE @part3  VARCHAR(20);

SET     @part1 = SUBSTRING(@path, 2, CHARINDEX('.', @path) -2);
SET     @temp  = SUBSTRING(@path, CHARINDEX('.', @path) + 1, 500);
SET     @part2 = SUBSTRING(@temp, 1, CHARINDEX('.', @temp) -1);
SET     @part3  = SUBSTRING(@temp, CHARINDEX('.', @temp) + 1, 500);

DECLARE @int1   BIGINT;
DECLARE @int2   BIGINT;
DECLARE @int3   BIGINT;

SET     @int1 = CONVERT(BIGINT, @part1);
SET     @int2 = CONVERT(BIGINT, @part2);
SET     @int3 = CONVERT(BIGINT, @part3);

SELECT  'Target',
        CONVERT(BIGINT, SUBSTRING(CONVERT(BINARY(16), @customerId), 1, 6)),
        CONVERT(BIGINT, SUBSTRING(CONVERT(BINARY(16), @customerId), 7, 6)),
        CONVERT(BIGINT, SUBSTRING(CONVERT(BINARY(16), @customerId), 13, 4));
SELECT  'Result', @part1, @part2, @part3

SELECT  'Target', 
        SUBSTRING(CONVERT(BINARY(16), @customerId), 1, 6),
        SUBSTRING(CONVERT(BINARY(16), @customerId), 7, 6),
        SUBSTRING(CONVERT(BINARY(16), @customerId), 13, 4)

SELECT  'Result',
        CONVERT(BINARY(16), @int1), CONVERT(BINARY(16), @int2), CONVERT(BINARY(16), @int3);

SELECT  CONVERT(BINARY(16), @int1 + @int2 + @int3),master.dbo.fn_varbintohexstr(CONVERT(BINARY(16), @int1 + @int2 + @int3))
        , @customerId, @path, CONVERT(BINARY(16), @customerId)

DECLARE @binaryString VARCHAR(50)
SET @binaryString = RIGHT(master.dbo.fn_varbintohexstr(@int1), 12) +
                    RIGHT(master.dbo.fn_varbintohexstr(@int2), 12) +
                    RIGHT(master.dbo.fn_varbintohexstr(@int3), 8);
SELECT  'Binary String', @binaryString;

1 个答案:

答案 0 :(得分:1)

一旦你获得了@int个变量,你就完成了:

SELECT CONVERT(uniqueidentifier,
            CONVERT(binary(6),@int1) +
            CONVERT(binary(6),@int2) +
            CONVERT(binary(4),@int3))

这只是使用SUBSTRING将原始数据切成块的大小。