Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Don't miss out! 2025 Microsoft Fabric Community Conference, March 31 - April 2, Las Vegas, Nevada. Use code MSCUST for a $150 discount. Prices go up February 11th. Register now.

Reply
amokhalgay
New Member

Stored Procedure execution using JDBC is trimming the CHAR, VARCHAR and VARBINARY data

I am executing stored procedure using JDBC driver.  So when I have an output field of type CHAR, VARCHAR or VARBINARY with precision 8000. After executing the SP using JDBC API, it returns only 4000 Chars. Need to know if there is anything I missed in execution. The query is simple 

EXEC SCHEMA_NAME.STORED_PROC_NAME @c_varchar = ?, @c_varchar2=?

In above example c_varchar is input field and c_varchar2 is in/out type field. c_varchar2 has 8000 precision set still the output which i get is 4000 length even though the procedure is designed to return 8000 characters. Please help me rectify this issue.

 

Below is the code snippet:

CallableStatement callStmt = sqlConnection.prepareCall("EXEC dbo.proc_name @c_varchar =? , @c_varchar2= ?" );
callStmt.setString(1, "2345");
callStmt.setNull(2, Types.VARCHAR);
callStmt.registerOutParameter(2, Types.VARCHAR);

callStmt.execute();

 

Procedure design :

CREATE PROC dbo.proc_name

@c_varchar varchar(8000),
@c_varchar2 varchar(8000) OUT

AS
BEGIN
SELECT

@c_varchar2= c_varchar2

FROM dbo.char_SP
WHERE c_varchar = @c_varchar;
END;

 

2 ACCEPTED SOLUTIONS

Try adding

`sendStringParametersAsUnicode=false` to your JDBC connection string.

 

This ensures parameters are sent as `VARCHAR` instead of `NVARCHAR`

View solution in original post

Great it worked.

 

you should ensure that, for any operations involving Unicode content, you either continue using the corresponding Unicode-specific JDBC methods (e.g., setNString) or otherwise verify that the default code page conversion does not corrupt data.


Easiest way is to verify unicode data manually in WH and see i

the impact.

 

 

please accept this answer if this is helpful 

View solution in original post

5 REPLIES 5
amokhalgay
New Member

@nilendraFabric 

This is the code I did to get sql connection from driver:

Driver version is : mssql-jdbc-12.2.0.jre8.jar

Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection sqlConnection = DriverManager.getConnection(prepareJDBCUrl(jdbcConnectString, databaseName), info);

 

public static String prepareJDBCUrl(String jdbcConnectionString, String databaseName) {
StringBuilder sb = new StringBuilder();
return sb.append("jdbc:sqlserver://")
.append(jdbcConnectionString)
.append(":1433;database=")
.append(databaseName)
.append(";encrypt=true;authentication=")
.append("ActiveDirectoryServicePrincipal")
.toString();
}

Try adding

`sendStringParametersAsUnicode=false` to your JDBC connection string.

 

This ensures parameters are sent as `VARCHAR` instead of `NVARCHAR`

this worked, thanks.

But what is the impact of this, because we use Fabric Warehouse tables and stored procedures, there will be tables which contains Unicode characters in column names and data will also can have unicode chars.

Great it worked.

 

you should ensure that, for any operations involving Unicode content, you either continue using the corresponding Unicode-specific JDBC methods (e.g., setNString) or otherwise verify that the default code page conversion does not corrupt data.


Easiest way is to verify unicode data manually in WH and see i

the impact.

 

 

please accept this answer if this is helpful 

nilendraFabric
Solution Supplier
Solution Supplier

Hey @amokhalgay 

 

Problem seems to be with JDBC drivers may treat `VARCHAR` parameters as Unicode (`NVARCHAR`) by default, causing truncation at 4000 characters (since `NVARCHAR(4000)` uses 8000 bytes).

 

before we jump to solution , please share how are you using jdbc driver in your case

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Prices go up Feb. 11th.

JanFabricDE_carousel

Fabric Monthly Update - January 2025

Explore the power of Python Notebooks in Fabric!

JanFabricDW_carousel

Fabric Monthly Update - January 2025

Unlock the latest Fabric Data Warehouse upgrades!

JanFabricDF_carousel

Fabric Monthly Update - January 2025

Take your data replication to the next level with Fabric's latest updates!