简体   繁体   English

SQL Server存储过程的输出参数被截断为4000个字符

[英]Output parameter from SQL Server stored procedure truncated at 4000 characters

I have a problem with the output parameter of a stored procedure when it contains more than 4000 characters. 当存储过程的输出参数包含4000个以上的字符时,我遇到了问题。 The response seems to be truncated by the JDBC driver? 响应似乎被JDBC驱动程序截断了吗? How can I get the full result? 如何获得完整结果?

The stored procedure answers with the complete response (> 4000 characters) but I can not open it from Java. 存储过程以完整的响应(> 4000个字符)回答,但是我无法从Java中打开它。 I have tried both jTDS and Microsoft's JDBC driver 6.0. 我已经尝试了jTDS和Microsoft的JDBC驱动程序6.0。 Here is my code: 这是我的代码:

CallableStatement pstmt = con.prepareCall("{call sp_horus_get_consultorios_stv(?)}"); 
pstmt.registerOutParameter(1, -1); 
pstmt.setString(1, ""); 
pstmt.execute(); 
String sp_horus_get_consultorios_stv = pstmt.getString(1);

This works with stored procedures in sybase. 这适用于sybase中的存储过程。

I was able to recreate your issue using Microsoft JDBC Driver 6.x. 我能够使用Microsoft JDBC Driver 6.x重新创建您的问题。 I found that I could avoid the problem by commenting out the setString call: 我发现我可以通过注释掉setString调用来避免此问题:

try (CallableStatement pstmt = conn.prepareCall("{call usp_horus_get_consultorios_stv(?)}")) {
    pstmt.registerOutParameter(1, Types.LONGNVARCHAR); 
    //pstmt.setString(1, "");  // disabled
    pstmt.execute(); 
    String sp_horus_get_consultorios_stv = pstmt.getString(1);
    System.out.println(sp_horus_get_consultorios_stv.length());  // > 4000 characters
}

Unfortunately, that fix did not solve the problem under jTDS 1.3.1. 不幸的是,该修补程序无法解决jTDS 1.3.1下的问题。 It appears that jTDS still suffers from the limitation described here . 看来jTDS仍然受到此处描述的限制。 So, for jTDS it appears that we have to do something like this: 因此,对于jTDS,看来我们必须执行以下操作:

String sql = 
        "DECLARE @out NVARCHAR(MAX);" +
        "EXEC usp_horus_get_consultorios_stv @out OUTPUT;" +
        "SELECT @out;";
try (
        Statement st = conn.createStatement();
        ResultSet rs = st.executeQuery(sql)) {
    rs.next();
    String sp_horus_get_consultorios_stv = rs.getString(1);
    System.out.println(sp_horus_get_consultorios_stv.length());  // > 4000 characters
}

From what I understand, your output parameter is of type NVARCHAR(8000), which is the maximum explicit number allowed, and outputs a 4000 character Unicode string lenght (2 bytes per char). 据我了解,您的输出参数的类型为NVARCHAR(8000),这是允许的最大显式数,并输出4000个字符的Unicode字符串长度(每个字符2个字节)。

However, and lucky you, there another possibility : NVARCHAR(MAX), that basically allows an infinite string lenght (well, not infinite, but almost : What is the maximum number of characters that nvarchar(MAX) will hold? 但是,幸运的是,还有另一种可能性:NVARCHAR(MAX),它基本上允许无限的字符串长度(嗯,不是无限的,但是几乎是: nvarchar(MAX)可以容纳的最大字符数是多少?

You should change your output paramater type to NVARCHAR(MAX). 您应该将输出参数类型更改为NVARCHAR(MAX)。

Happy coding ;) 快乐的编码;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从SQL Server存储过程中获取输出参数和ResultSet值 - get both output parameter and ResultSet values from SQL Server stored procedure 如何从休眠中的存储过程中获取OUTPUT值(SQL SERVER) - How to get OUTPUT value from stored procedure in hibernate (SQL SERVER) 获取存储过程camel-sql的输出参数 - Get output parameter of a stored procedure camel-sql 在SQL Server 2008中使用表类型输入参数调用存储过程 - Calling stored procedure with table type input parameter in sql server 2008 Spring SimpleJdbcCall 和 SQL Server 系统存储过程 - 期望未提供参数,但它是? - Spring SimpleJdbcCall & SQL Server system Stored Procedure - expects parameter not supplied, but it is? 从存储过程中获取 Output 参数而不调用 execute() - Get Output parameter from stored procedure without calling execute() 如何在 Java 中获取 SQL 服务器存储过程的 output - How to get output of an SQL Server stored procedure in Java JDBC SQL 服务器存储过程,带有 ResultSet、返回值和 output 参数 - JDBC SQL Server Stored Procedure with ResultSet, return value, and output parameters MsSql 中的存储过程期望输出参数作为输入 - Stored Procedure in MsSql expecting output parameter as input 从SQL Server生成存储过程到IntelIJ中的Hibernate POJO类 - generate stored procedure from SQL server to Hibernate POJOs classes in IntelIJ
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM