将字符串变量转换为 GUID

Convert string variable to GUID

我想对数据库 table 中的 return varbinary 数据使用 Execute SQL Task 查询。该查询需要一个 UniqueIdentifier 作为 参数 ,它在包变量中存储为 string。此查询的结果(varbinary 数据)将存储在字节类型的第二个变量中。

下面的 table 显示了我的局部变量列表,请注意 DocumentGuid 由控制流的另一部分预先填充

| Variable Name | Qualified Name     | Scope   | Data Type | Comments                                 |
|---------------|--------------------|---------|-----------|------------------------------------------|
| DocumentGuid  | User::DocumentGuid | Package | String    | Used to store a GUID value               |
| DocumentData  | User::DocumentData | Package | Byte      | Used to hold varbinary data from a query |

当我尝试在这样的 Execute SQL Task 查询中使用它时:

SELECT DocData 
FROM  docsRepo
WHERE DocumentGuid = ?

传入参数为

| Variable name      | Direction | Data Type | Parameter Name | Parameter Size |
|--------------------|-----------|-----------|----------------|----------------|
| User::DocumentGuid | Input     | GUID      | 0              | 100            |

和结果集

| Result Name | Variable Name      |
|-------------|--------------------|
| DocData    | User::DocumentData |

我收到以下错误:

[Execute SQL Task] Error: Executing the query "SELECT DocData FROM
dbo.docsRepo..." failed with the following error: "Conversion failed when converting from a character string to uniqueidentifier.". Possible failure reasons: Problems with the query, "ResultSet" property not set correctly, parameters not set correctly, or connection not established correctly.

我是不是漏掉了某种基本逻辑?以下脚本在 SQL 服务器中运行良好:

SELECT DocData 
FROM  docsRepo
WHERE DocumentGuid = '53A394A7-5D2B-40C0-A04D-90553E4535C3'

你可以尝试显式 CAST:

SELECT DocData 
FROM  docsRepo
WHERE DocumentGuid = CAST(? AS UNIQUEIDENTIFIER);

看起来您在 DocumentGuid 中存储的值可能不是有效的 GUID,或者您传递给查询的 GUID 无效

要查找您的 table 是否包含无效的 GUID,您可以 运行:

SELECT DocumentGuid
FROM  docsRepo
WHERE TRY_CAST(DocumentGuid AS UNIQUEIDENTIFIER) IS NULL

我对您的示例进行了两处更改,并且能够使其正常工作。

首先是在执行 SQL 任务中将参数类型更改为字符串。我知道,它是一个 guid,但由于它是 SSIS 中的一个字符串变量,因此请保持数据类型对齐并让后端 rdbms/driver 处理转换。

我更改的另一件事是 DocData 的数据类型。您定义为 Byte 但从未使用过它,我担心它是一个完整的字节,而不是字节。无论如何,对于我创建的 table,使用 SSIS 的对象数据类型使其工作。

来源查询

这是我的 table 和其中的一个值

IF NOT EXISTS
(
    SELECT
        *
    FROM
        sys.tables
    WHERE
        name = 'docsRepo'
)
BEGIN
    CREATE TABLE dbo.docsRepo
    (
        DocumentGuid uniqueidentifier
    ,   DocumentData varbinary(MAX)
    );

    INSERT INTO
        dbo.docsRepo
    SELECT
        '53A394A7-5D2B-40C0-A04D-90553E4535C3'
    ,   CAST('Hello world' AS varbinary(MAX));
END;

检索查询

SELECT D.DocumentData FROM dbo.docsRepo AS D WHERE D.DocumentGuid = ?;

已配置完整结果集。使用 OLE DB 驱动程序。参数名称0,数据类型varchar,变量User::DocumentGuid。结果选项卡,结果集名称为 0,变量 User::DocumentData

Biml 将创建一个演示所有这些的 SSIS 包

<Biml xmlns="http://schemas.varigence.com/biml.xsd">
    <Connections>
        <OleDbConnection Name="localhost" ConnectionString="Provider=SQLNCLI11;Integrated Security=SSPI;Data Source=.\dev2014;Initial Catalog=tempdb" />
    </Connections>
    <Packages>
        <Package Name="SO_50028154" ConstraintMode="Linear">
            <Variables>
                <Variable Name="DocumentGuid" DataType="String">53A394A7-5D2B-40C0-A04D-90553E4535C3</Variable>
                <Variable Name="DocumentData" DataType="Object" />
            </Variables>
            <Tasks>
                <ExecuteSQL Name="SQL GenerateTable" ConnectionName="localhost">
                    <DirectInput>IF NOT EXISTS(SELECT * FROM sys.tables WHERE name='docsRepo')BEGIN CREATE TABLE dbo.docsRepo(DocumentGuid uniqueidentifier, DocumentData varbinary(max)); INSERT INTO dbo.docsRepo SELECT '53A394A7-5D2B-40C0-A04D-90553E4535C3', CAST('Hello world' AS varbinary(max)); END</DirectInput>
                </ExecuteSQL>
                <ExecuteSQL Name="SQL Retrieve data" ConnectionName="localhost" ResultSet="Full">
                    <DirectInput>SELECT D.DocumentData FROM dbo.docsRepo AS D WHERE D.DocumentGuid = ?;</DirectInput>
                    <Parameters>
                        <Parameter DataType="AnsiString" Name="0" VariableName="User.DocumentGuid" />
                    </Parameters>
                    <Results>
                        <Result Name="0" VariableName="User.DocumentData" />
                    </Results>
                </ExecuteSQL>
            </Tasks>
        </Package>
    </Packages>
</Biml>

当您想将 uniqueIdentifer 作为 varchar 发送到您的执行 SQL 任务时,您可以在您的执行 SQL 任务中使用此查询:

DECLARE @docGuid AS UniqueIdentifier 
SET @docGuid  = CAST ( '{' + ? + '}' AS UniqueIdentifier )

SELECT DocData 
FROM  docsRepo
WHERE DocumentGuid = @docGuid

那个?按参数发送为大小为 50

的 varchar