【问题标题】:Passing Table Type Input Parameter To Stored Procedure in Execute SQL Task in SSIS在 SSIS 中执行 SQL 任务中将表类型输入参数传递给存储过程
【发布时间】:2016-05-13 14:01:38
【问题描述】:

我正在尝试从 SSIS 中的执行 SQL 任务调用存储过程。存储过程接受两个表类型变量,我在另一个执行 SQL 任务中构造它们并存储在变量的对象类型中。 因为我需要传递对象类型的变量,所以我使用 ADO.NET 连接管理器。 另外,由于我使用的是 ADO.NET 连接管理器,因此无法将输入参数传递为?直接输入标记,我需要编写表达式。 但是表达式不支持对象类型变量。 出路是什么?

谢谢

【问题讨论】:

    标签: sql-server ssis


    【解决方案1】:

    您将无法使用本机 ADO/OLE 源组件。您必须使用脚本组件作为源并使用 .NET 来创建

    来自我的SQL TVP post的相关位

    string connectionString = @"Data Source=localhost;Initial Catalog=master;Integrated Security=True";
    System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(connectionString);
    System.Data.DataTable dataTable = null;
    
    // note that my data table does not have to be the same
    // as my UDTT, nor do my columns have to have the same name
    // Types-yes. Ordinal, probably so
    dataTable = new System.Data.DataTable("Sample");
    dataTable.Columns.Add("tweep", System.Type.GetType("System.String"));
    dataTable.Columns.Add("have_met", System.Type.GetType("System.Boolean"));
    
    // add rows to my data table but really, this could be any source
    dataTable.Rows.Add(new object[] { "billinkc", true });
    dataTable.Rows.Add(new object[] { "BrentO", true });
    dataTable.Rows.Add(new object[] { "buckwoody", false });
    
    // Hooray for #sqlsat35 and meeting Jen & Sean
    dataTable.Rows.Add(new object[] { "MidnightDBA", true });
    
    System.Data.SqlClient.SqlConnection connection = null;
    System.Data.DataSet results = null;
    System.Data.SqlClient.SqlCommand command = null;
    System.Data.SqlClient.SqlDataReader dataReader = null;
    connection = new System.Data.SqlClient.SqlConnection(connectionString);
    try
    {
        connection.Open();
        command = new System.Data.SqlClient.SqlCommand("TwitterAdd");
        command.CommandType = System.Data.CommandType.StoredProcedure;
        command.Connection = connection;
    
        // Assigning a table valued parameter looks much like any other parameter
        System.Data.SqlClient.SqlParameter tvp = command.Parameters.AddWithValue("@tvp", dataTable);
    
        // this is the only special sauce (not required but helpful)
        tvp.SqlDbType = System.Data.SqlDbType.Structured;
        tvp.TypeName = "dbo.CONTRIVED_EXAMPLE";
    
        dataReader = command.ExecuteReader();
    
        if (dataReader.HasRows)
        {
            // pseudo logic here
            while(dataReader.Read())
            {
                MyBuffer.AddRow();
                MyBuffer.Col1 = dataReader["Col1"].ToString();
            }
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex);
    }
    

    【讨论】:

    • 谢谢blinkc。我正在通过连接到 ADO.NET 连接管理器来准备执行 sql 任务中的对象变量。但是当我在脚本任务中读取只读变量时,数据集中的表显示为空。当我在 SSMS 中执行相同操作时,我能够看到表类型变量中的数据。我正在读取数据集中的变量并使用第一个表传递给 SP。我不知道为什么这个对象是空的。
    • 执行 sql 任务似乎无法用数据填充对象变量,因为我无法通过设置断点查看手表中的数据。这是一个 select 语句,从连接的几个表中获取一些行。不确定是什么问题。我已将结果集设置为完整结果集。将其更改为单行会引发错误“无法填充单行结果类型的结果列。查询返回空结果集”
    • 抱歉添加了多个 cmets。随着我越来越接近它的工作,我正在添加。现在我面临一个奇怪的问题。我有一个变量,我在执行 SQL 任务中存储来自 DB 的链接 (abcd/xyz/ijk.aspx)。现在,当我在另一个执行 sql 任务中使用它创建对象变量时,我收到“https:附近的语法不正确”错误。我不确定这与任何转义序列有关或需要任何特殊处理。
    • 嗯,这真是让我头疼。我可以以某种方式绕过斜线问题,但有一个新问题。在执行 sql 任务的表达式中,如果我这样写,“select "+[User::UserName]+" as Username,employeeid from employee where alias = "+[User::Alias] It throws me an error Incorrect column ' soumyadeb' 这是用户名变量的评估值。知道为什么会这样吗?
    • 感谢 billinkc 的输入。我终于设法使它工作。我使用脚本任务将对象变量传递给存储过程并完成我的工作。我想分享一个我无法从逻辑上解释的观察结果。我的对象变量具有以下结构。 {employeeid int, dept_id int, empname varchar(100), Gender varchar(50) } 下一条评论的剩余部分。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-18
    • 1970-01-01
    相关资源
    最近更新 更多