使用 CallableStatement.setObject(int,Object) 时出现 SQLServerException?
快速问题,为什么在使用以下代码时出现以下异常;
请注意,这是使用 Java 通过存储过程调用 MS SQL Server 2008 R2。
Connection con = getDataSource().getConnection();
CallableStatement cs = con.prepareCall("countrySelect(?)");
cs.setObject(1, "GB");
cs.execute();
结果是;
Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: Incorrect syntax near '@P0'.
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:197)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.getNextResult(SQLServerStatement.java:1493)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.doExecutePreparedStatement(SQLServerPreparedStatement.java:390)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement$PrepStmtExecCmd.doExecute(SQLServerPreparedStatement.java:340)
at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:4575)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:1400)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeCommand(SQLServerStatement.java:179)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeStatement(SQLServerStatement.java:154)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.execute(SQLServerPreparedStatement.java:322)
这个“@P0”来自哪里?这是Java内存引用吗?为什么它不能正确转换为 SQL 类型?
如果可以的话,我也会得到同样的东西;
cs.setString(1, "GB");
存储的Proc代码如下(不是我写的);
DECLARE @dbid INTEGER;
DECLARE @dbName NVARCHAR(125);
DECLARE @prcName NVARCHAR(125);
SET @dbid = DB_ID();
SET @dbName = DB_NAME();
SET @prcName = 'usp_countrySelect'
IF dbo.ufn_Admin_countryExists(@countryCode) = 1
SELECT countryCode
, countryName
, nationalVerificationPrice
, vatCharged
, VATPercentage
, sortingPrecedence
FROM dbo.country
WHERE countryCode = @countryCode ;
ELSE
RAISERROR ( N'ERROR: A country with countryCode %s does not exist - proc Name:%s, database ID:%i, the database name is: %s.'
, 16 -- Severity
, 1 -- State
, @countryCode -- First subbed argument
, @prcName -- Second subbed argument
, @dbid -- Third subbed argument
, @dbName -- Fourth subbed argument
);
END
Quick question, why am I getting the below exception when using the following code;
Note this is using Java to call into MS SQL Server 2008 R2 using stored procs.
Connection con = getDataSource().getConnection();
CallableStatement cs = con.prepareCall("countrySelect(?)");
cs.setObject(1, "GB");
cs.execute();
Results in;
Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: Incorrect syntax near '@P0'.
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:197)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.getNextResult(SQLServerStatement.java:1493)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.doExecutePreparedStatement(SQLServerPreparedStatement.java:390)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement$PrepStmtExecCmd.doExecute(SQLServerPreparedStatement.java:340)
at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:4575)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:1400)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeCommand(SQLServerStatement.java:179)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeStatement(SQLServerStatement.java:154)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.execute(SQLServerPreparedStatement.java:322)
Where is this "@P0" coming from? Is this the Java memory reference? How come its not converting to SQL types properly?
I get the same thing if I can to;
cs.setString(1, "GB");
Stored Proc code is as follows (not written by me);
DECLARE @dbid INTEGER;
DECLARE @dbName NVARCHAR(125);
DECLARE @prcName NVARCHAR(125);
SET @dbid = DB_ID();
SET @dbName = DB_NAME();
SET @prcName = 'usp_countrySelect'
IF dbo.ufn_Admin_countryExists(@countryCode) = 1
SELECT countryCode
, countryName
, nationalVerificationPrice
, vatCharged
, VATPercentage
, sortingPrecedence
FROM dbo.country
WHERE countryCode = @countryCode ;
ELSE
RAISERROR ( N'ERROR: A country with countryCode %s does not exist - proc Name:%s, database ID:%i, the database name is: %s.'
, 16 -- Severity
, 1 -- State
, @countryCode -- First subbed argument
, @prcName -- Second subbed argument
, @dbid -- Third subbed argument
, @dbName -- Fourth subbed argument
);
END
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
弄清楚了,如果你问我的话,这是对一个不明显问题的蹩脚错误;
不起作用,而是使用;
区别在于单词“call”和括起来的 { } 括号。
Figured it out, rather lame error to an un-obivous problem if you ask me;
Does NOT work, instead use;
Difference being the word "call" and the enclosing { } brackets.