TSQL使用变量重新连接字符串文本

时间:2018-02-12 14:00:16

标签: tsql variables ssis

我正在尝试将下面代码第3行(将@ProjectBinary)中的文件夹位置替换为变量。

原始代码:

DECLARE @ProjectBinary as varbinary(max)
DECLARE @operation_id as bigint
Set @ProjectBinary = (SELECT * FROM OPENROWSET(BULK 'C:\MyProjects\ SSISPackage_ProjectDeployment.ispac', SINGLE_BLOB) as BinaryData)

Exec catalog.deploy_project @folder_name = 'SSIS Packages', @project_name = 'DeployViaStoredProc_SSIS', @Project_Stream = @ProjectBinary, @operation_id = @operation_id out

我创建了一个变量: DECLARE @Location AS varchar(250)='C:\ SSIS \ DWH_IS.ispac'

DECLARE @ProjectBinary as varbinary(max)
DECLARE @operation_id as bigint
Set @ProjectBinary = ('SELECT * FROM OPENROWSET(BULK ' + @Location + ', SINGLE_BLOB') as BinaryData)

Exec catalog.deploy_project @folder_name = 'SSIS Packages', @project_name = 'DeployViaStoredProc_SSIS', @Project_Stream = @ProjectBinary, @operation_id = @operation_id out

我一直收到错误: Msg 257,Level 16,State 3,Line 61 不允许从数据类型varchar到varbinary(max)的隐式转换。使用CONVERT函数运行此查询。

有关如何更改此代码以获取代码的任何建议吗?

1 个答案:

答案 0 :(得分:0)

您需要将其细分如下

DECLARE @SQL nvarchar(1000), @ProjectBinary as varbinary(max);
SET @SQL = N'SELECT @Blob = (SELECT * FROM OPENROWSET(BULK ''' + @Location + ''', SINGLE_BLOB) as BinaryData)'
EXEC sp_executesql @SQL, N'@Blob varbinary(max) OUTPUT', @ProjectBinary OUTPUT;

Set @ProjectBinary = (..无法动态或参数化