java com.microsoft.sqlserver.jdbc.SQLServerException: '@P0' 附近的语法不正确

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17268901/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 01:32:04  来源:igfitidea点击:

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

javasql-serversql-server-2008jdbc

提问by WiXXeY

I have an SQL server 2008 procedure which is returning one out parameter, i am giving call to it from java. My code is given below

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

Stored procedure code is

存储过程代码是

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

Java Calling Code is

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));

but infected.execute(); is generating the following error

但受感染.execute(); 正在生成以下错误

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

com.microsoft.sqlserver.jdbc.SQLServerException: '@P0' 附近的语法不正确

kindly guide me where is problem

请指导我哪里有问题

回答by Donald.McLean

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

正如@GregHNZ 提供的这个链接所建议的那样,SQL Server 需要在调用周围使用一组花括号。

The line in your code would look like this:

代码中的行将如下所示:

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