How to pass Table-Valued Parameters (Array-like Pa

2020-02-14 08:44发布

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?

3条回答
不美不萌又怎样
2楼-- · 2020-02-14 09:09

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.

查看更多
叛逆
3楼-- · 2020-02-14 09:21

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.

查看更多
一夜七次
4楼-- · 2020-02-14 09:24

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.

查看更多
登录 后发表回答