使用 CallableStatement.setObject(int,Object) 时出现 SQLServerException?

发布于 2024-11-04 23:22:19 字数 2282 浏览 1 评论 0原文

快速问题,为什么在使用以下代码时出现以下异常;

请注意,这是使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

べ繥欢鉨o。 2024-11-11 23:22:19

弄清楚了,如果你问我的话,这是对一个不明显问题的蹩脚错误;

CallableStatement cs = con.prepareCall("countrySelect(?)");

不起作用,而是使用;

CallableStatement cs = con.prepareCall("{call countrySelect(?)}");

区别在于单词“call”和括起来的 { } 括号。

Figured it out, rather lame error to an un-obivous problem if you ask me;

CallableStatement cs = con.prepareCall("countrySelect(?)");

Does NOT work, instead use;

CallableStatement cs = con.prepareCall("{call countrySelect(?)}");

Difference being the word "call" and the enclosing { } brackets.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文