Com.microsoft.sqlserver.jdbc.SQLServerException: Incorrect syntax near '@ P0'

I have a SQL Server 2008 procedure that returns a single parameter, I call it from java. My code is below

Stored Procedure Code

Create PROCEDURE countInfected @infected int out AS Select @infected = COUNT(*) from userInfo where userID NOT IN (Select userID from deletedInfo); 

Java Calling Code -

 CallableStatement infected = null; infected = con.prepareCall("call countInfected(?)"); infected.registerOutParameter(1, java.sql.Types.INTEGER); infected.execute(); System.out.println("Infected"+ infected.getInt(1)); 

but infected.execute (); generates the following error

com.microsoft.sqlserver.jdbc.SQLServerException: invalid syntax next to '@ P0'

kindly advise me where the problem is

+7
source share
1 answer

As this link suggested by @GregHNZ suggested, SQL Server requires a set of braces around the call.

The line in your code will look like this:

 infected = con.prepareCall("{ call countInfected(?) }"); 
+10
source

All Articles