使用表值参数从 CLR 存储过程中调用存储过程

发布于 2024-12-25 12:11:12 字数 1455 浏览 3 评论 0原文

情况:

一个 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 计划,那么它就会开始出现故障。 (也找不到该“触发器”中的逻辑)

有什么想法吗? 先感谢您。

The situation:

One Clr stored procedure that builds a data table and then it tries to call an SQL Stored procedure with a TVP parameter.

The code:

  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

The problem:

The behavior is erratic. One time it works, after x (where x is random) calls it will give me y (where y is random) "severe error occurred" with no other details, and after that the cycle will start again. From what I see the problem is on the CLR SP side as when errors start to occur, the SQL SP can be changed and be faulty as the error remains the same.
Maybe there is no love between CLR SP and TVPs, but I didn't find any reference to that.

As a side note, when it works it may work for a long time, but if I delete the SPs plan then it will start faulting. (can't find the logic in that "trigger" also)

Any thoughts ?
Thank you in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

苏璃陌 2025-01-01 12:11:12

看起来这一行有错误。

command.CommandTest = "prc_test";

将其更改为

command.CommandText = "prc_test";

Looks like this line has an error.

command.CommandTest = "prc_test";

Change it to

command.CommandText = "prc_test";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文