How to pass Table-Valued Parameters (Array-like Parameter) to Stored Procedure in Microsoft SQL Server 2008 R2 using JDBC? [duplicate]

佐手、 提交于 2019-11-29 15:15:18

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.

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!