【问题标题】:Calling a stored procedure from within a CLR stored procedure with a table valued parameter使用表值参数从 CLR 存储过程中调用存储过程
【发布时间】:2012-01-05 07:11:18
【问题描述】:

情况:

一个构建数据表的 Clr 存储过程,然后尝试使用 TVP 参数调用 SQL 存储过程。

代码:

  public class tvp_test : System.Data.DataTable
  {
    public tvp_test()
    {
      this.Column.Add("field1",typeof(System.Int32));
      this.Column.Add("field2",typeof(System.Int32));
    }
  }

.............................................................

  {
    var tvp = new tvp_test();

.............................................................

   using(SqlConnection con = new SqlConnection("context connection=true"))
   {
      con.Open();
      var command = con.CreateCommand();

      command.CommandType = CommandType.StoredProcedure;
      command.CommandTest = "prc_test";
      command.Parameters.Add("@a",System.Data.SqlDbType.Structured);
      command.Parameters["@a"].Value = tvp;
      command.ExecuteNonQuery();

      con.Close();
   }
  }

.................................................. .....................

create procedure prc_test
  @a tvp_test readonly
begin
  insert into #tmp (field1,field2) /* #tmp is created before the call of the CLR SP */
  select field1,field2 from @a
end

问题:

行为不稳定。有一次它起作用,在 x(其中 x 是随机的)调用之后,它会给我 y(其中 y 是随机的)“发生严重错误”而没有其他细节,之后循环将重新开始。从我所见,问题出在 CLR SP 方面,因为当错误开始发生时,SQL SP 可以更改并且由于错误保持不变而出现故障。 也许CLR SP和TVP之间没有爱,但我没有找到任何参考。

附带说明,当它工作时,它可能会工作很长时间,但如果我删除 SP 计划,它就会开始出现故障。 (在那个“触发器”中也找不到逻辑)

有什么想法吗? 提前谢谢你。

【问题讨论】:

  • 你能告诉我们表类型的定义吗?
  • 它复制 DataTable, tvp_test 与 2 个 int 可以为空:创建类型 dbo.tvp_test 作为表 (field1 int null,field2 int null)

标签: sql-server-2008 sqlclr clrstoredprocedure table-valued-parameters


【解决方案1】:

看起来这一行有错误。

command.CommandTest = "prc_test";

改成

command.CommandText = "prc_test";

【讨论】:

  • 虽然这是真的,但我认为这种行为与此无关。由于SqlCommand 中没有CommandTest 属性,因此程序根本无法编译。
猜你喜欢
  • 1970-01-01
  • 2017-08-22
  • 1970-01-01
  • 2013-04-05
  • 1970-01-01
  • 2013-06-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多