如何使用 JDBC 将表值参数(类似数组的参数)传递给 Microsoft SQL Server 2008 R2 中的存储过程?

发布于 2024-12-06 07:17:00 字数 114 浏览 1 评论 0原文

如何使用 Microsoft SQL Server 2008 R2 JDBC 驱动程序将表值参数(类似数组的参数)传递给 Microsoft SQL Server 2008 R2 中的存储过程? jTDS 可以吗?

How to pass Table-Valued Parameters (Array-like Parameter) to Stored Procedure in Microsoft SQL Server 2008 R2 using Microsoft SQL Server 2008 R2 JDBC Driver ?
Is it possible with jTDS?

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

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

发布评论

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

评论(3

奶茶白久 2024-12-13 07:17:00

当前 (3.0) Microsoft 驱动程序不支持传递 TVP。

微软曾一度为 TVP 与 Bulk Copy 征求投票:

http://blogs.msdn.com/b/jdbcteam/archive/2011/09/22/tvp-or-bulk-copy.aspx

TVP 获得了更多选票,但仍看看实际做了什么。最新的 CTP 4.0 版似乎不支持 TVP。

The current (3.0) Microsoft driver doesn't support passing TVPs.

At one point, Microsoft was soliciting votes for TVP vs. Bulk Copy:

http://blogs.msdn.com/b/jdbcteam/archive/2011/09/22/tvp-or-bulk-copy.aspx

TVP got more votes, but it remains to be seen what actually got done. The most recent CTP for version 4.0 doesn't appear to have TVP support.

梦断已成空 2024-12-13 07:17:00

虽然这个问题是关于 SQL Server 2008 的,而且当时确实不可能传递表值参数,但现在可以了。 JDBC 驱动程序手册。例如,可以这样做:

SQLServerDataTable table = new SQLServerDataTable();
table.addColumnMetadata("i" ,java.sql.Types.INTEGER);
table.addRow(1);
table.addRow(2);
table.addRow(3);
table.addRow(4); 

try (SQLServerPreparedStatement stmt=
    (SQLServerPreparedStatement) connection.prepareStatement(
       "SELECT * FROM some_table_valued_function(?)")) {

    // Magic here:
    stmt.setStructured(1, "dbo.numbers", table);  

    try (ResultSet rs = stmt.executeQuery()) {
        ...
    }
}

我最近也在此处发表了有关此内容的博客

While this question was about SQL Server 2008, and while it really wasn't possible to pass table valued parameters at the time, it is now. This is documented here in the JDBC driver manual. For example, it could be done like this:

SQLServerDataTable table = new SQLServerDataTable();
table.addColumnMetadata("i" ,java.sql.Types.INTEGER);
table.addRow(1);
table.addRow(2);
table.addRow(3);
table.addRow(4); 

try (SQLServerPreparedStatement stmt=
    (SQLServerPreparedStatement) connection.prepareStatement(
       "SELECT * FROM some_table_valued_function(?)")) {

    // Magic here:
    stmt.setStructured(1, "dbo.numbers", table);  

    try (ResultSet rs = stmt.executeQuery()) {
        ...
    }
}

I've also recently blogged about this here.

岁吢 2024-12-13 07:17:00

我自己解决了这个问题。我创建了 CLR .Net Stored Proc 并接受 BLOB 参数。这个 BLOB 只是序列化 INT 的列表。可以使用 T-SQL 或 .Net CLR SP 对其进行反序列化。
.Net CLR SP 具有更好的性能,这对我的项目非常重要。

I have solved this problem by myself. I have created CLR .Net Stored Proc with accepts a BLOB parameter. This BLOB is just a list of serialized INTs. It is possible to deserialize it using T-SQL or .Net CLR SP.
.Net CLR SP has better performance, which was really important for my project.

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