可能的选项:
您可以使用Execute SQL Task
来获取记录集,而不是使用Data Flow Task
.
SSIS 2012 包说明了示例:
让我们假设dbo.FileNames
您的数据数据库中有一个表,其中包含一些示例行。
创建和填充表脚本:
CREATE TABLE dbo.FileNames(
srcPath nvarchar(255) NULL,
srcName nvarchar(60) NULL
);
GO
INSERT INTO dbo.FileNames (srcPath, srcName) VALUES
('C:\temp', 'File_1.txt')
, ('C:\temp', 'File_2.txt')
, ('C:\temp', 'File_3.txt')
;
GO
如下所示配置控制流选项卡。放置一个Execute SQL Task
以读取表数据并将其存储在对象数据类型中。将 aForeach Loop Container
放在 Execute SQL Task 之后,并将 aScript Task
放在Foreach 循环容器中

创建以下变量。建议@[User::srcPath] + "\\" + @[User::srcFile]
在变量上使用表达式,srcFull
这样如果路径缺少尾部反斜杠,它将相应地添加到路径和文件名之间。
Variable name Scope Data type Expression
-------------- ------------- ---------- -----------
FileNames SO_15072939 Object
srcFile SO_15072939 String
srcFull SO_15072939 String @[User::srcPath] + "\\" + @[User::srcFile]
srcPath SO_15072939 String

配置执行 SQL 任务以针对连接管理器运行以下查询以读取文件路径和名称信息。
SELECT srcPath, srcName FROM dbo.FileNames

查询返回的结果集应该存储到FileNames
数据类型的变量中Object
。

配置以使用Foreach Loop Container
读取变量FileNamesForeach ADO Enumerator
。

配置两个包变量以在循环遍历结果集时存储列值。

配置脚本任务编辑器以读取变量srcFull

将以下代码添加到脚本任务。
用于 SSIS 2008 及更高版本的 C# 脚本任务代码:
#region Namespaces
using System;
using System.Data;
using Microsoft.SqlServer.Dts.Runtime;
using System.Windows.Forms;
using System.IO;
#endregion
namespace ST_f1d7b6ab42e24ad5b5531684ecdcae87
{
[Microsoft.SqlServer.Dts.Tasks.ScriptTask.SSISScriptTaskEntryPointAttribute]
public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
{
public void Main()
{
string filePath = Dts.Variables["User::srcFull"].Value.ToString();
MessageBox.Show(filePath);
Dts.TaskResult = File.Exists(filePath)
? (int)ScriptResults.Success
: (int)ScriptResults.Failure;
}
#region ScriptResults declaration
enum ScriptResults
{
Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
};
#endregion
}
}

执行包将显示变量值在MessageBox
.

此示例中的包将失败,因为路径C:\temp\File_1.txt
不存在。

回复 OP 的评论:
我做了你在这里展示给我的事情,似乎我的问题是因为 User::srcFile 变量作为 ReadWriteVariables 添加到脚本任务中(因为我需要在同一个脚本中用它做一些事情)它有点被锁定所以定义为 @[User::srcPath] + @[User::srcFile] 的 User::srcFull 变量无法计算。
我在脚本任务编辑器上将变量srcFull
从ReadOnlyVariables
更改为,并且表达式仍然正确评估。我在示例输入中创建了这三个文件,并且包在消息框中正确显示了每个路径并成功完成了执行。ReadWriteVariables
将变量添加srcPath
到脚本任务编辑srcFile
器ReadWriteVariables
部分只会导致冲突,因为现在脚本任务和Foreach 循环容器都在尝试更新相同的变量。