有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

javacom。微软sqlserver。jdbc。SQLServerException:“@P0”附近的语法不正确

我有一个SQLServer2008过程,它返回一个out参数,我从java调用它。我的代码如下

存储过程代码是

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

Java调用代码是

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

但是被感染了。执行();正在生成以下错误

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

请告诉我哪里有问题


共 (1) 个答案

  1. # 1 楼答案

    正如@GregHNZ提供的this link所建议的,SQLServer需要在调用周围使用一组大括号

    代码中的行如下所示:

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