Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3312686
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T22:01:29+00:00 2026-05-17T22:01:29+00:00

I have a stored procedure that adds an object to the database and returns

  • 0

I have a stored procedure that adds an object to the database and returns the generated ID number.

For reference, its basic structure goes something like this:

ALTER PROCEDURE [dbo].[myProc] 
    @Name    nvarchar(50),
    @Creator nvarchar(50),
    @Text    nvarchar(200),
    @Lat     float,
    @Lon     float,
    @myID    int OUTPUT
AS
BEGIN
    SET NOCOUNT ON;

    INSERT INTO myTable --# blah blah blah
    SELECT @myID = scope_identity(); --# grab the auto-inc key from myTable
    INSERT INTO anotherTable --# blah blah blah
END

I ran this in SQL Server Management Studio and verified that it worked correctly.

Now I want to call that stored procedure from Java. I wrote this code to do it:

CallableStatement cs = con.prepareCall("EXEC myProc "
  + "@Name = ?, @Creator = ?, @Text = ?, @Lat = ?, @Lon = ?, @myID = ? OUTPUT");
cs.setString(1, aString);
cs.setString(2, anotherString);
cs.setString(3, yetAnotherString);
cs.setFloat(4, aFloat);
cs.setFloat(5, anotherFloat);
cs.registerOutParameter(6, java.sql.Types.INTEGER);

cs.execute();

But the execute() call throws an exception:

java.sql.SQLException: [Microsoft][ODBC SQL Server Driver][SQL Server]Error converting data type nvarchar to int.

What is going wrong here? Why is it even trying to convert an nvarchar to an int? I’m not even trying to fetch the return value via getInt() yet (that comes on the next line).

What I’ve tried:

  • Building the query by string manipulation to check whether the problem could possibly be in the input parameters. Same exception. At least that narrows the problem down.
  • Changing the type of the output parameter to Types.NVARCHAR, just in case. But that’s not even supported by the JdbcOdbcDriver which I am using.
  • Messing around with the call syntax (I haven’t used SQL Server with Java before). Always ended up with syntax errors. This included trying to make myID a return value instead of an output parameter.
  • Searching Google. A lot. Ended up with a bunch of unhelpful forum threads and EE “answers”.

Now I’m stumped. Is it really this hard or am I just missing the obvious?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T22:01:29+00:00Added an answer on May 17, 2026 at 10:01 pm

    Your code looks ok. The only thing that is wrong is that “float” in SQL Server maps to double in Java. “real” in MS SQL maps to float in Java. But this does not produce the problem.

    The problem is the buggy ODBC Bridge. You should use a type 4 JDBC driver for MS SQL. If you want to use the ODBC Bridge then you can test setting a value for the 6th parameter:

    cs.setInt(6, 0);
    

    But I does not know if this will work. The message means that the driver is using the data type NVARCHAR for the last parameter. It look like the ODBC Bridge does not forward the type with registerOutParameter.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a strange, sporadic issue. I have stored procedure that returns back 5
I have a stored procedure that returns multiple tables. How can I execute and
I have a stored procedure that returns values from a temp table. In my
I have a stored procedure that returns two int Values. The first can not
Suppose I have a stored procedure that manages its own transaction CREATE PROCEDURE theProc
I have a stored procedure that consists of a single select query used to
I have a stored procedure that looks like: CREATE PROCEDURE dbo.usp_TestFilter @AdditionalFilter BIT =
I have a Stored Procedure that rolls-back a series of operations. I want to
I have a stored procedure that needs to convert hexadecimal numbers to their decimal
I have a stored procedure that ideally should be able to accept a list/table

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.