[英]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.