【问题标题】:SQLServerException when using CallableStatement.setObject(int,Object)?使用 CallableStatement.setObject(int,Object) 时出现 SQLServerException?
【发布时间】:2011-05-03 14:12:22
【问题描述】:

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

请注意,这是使用 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

【问题讨论】:

  • 能否请您也发布您的程序的源代码?语法错误在过程本身中
  • 编辑原件以包含存储的过程代码
  • 感谢您的更新。上面看起来像过程体,@P0 不是它的一部分。 @P0 可以是一个参数,但是,你可以包含过程的签名吗?即CREATE PROCEDURE countrySelect(@P0... )
  • 创建过程 dbo.usp_Admin_countrySelect @countryCode NVARCHAR(10)
  • 那么这个过程呢:dbo.ufn_Admin_countryExists ?

标签: java sql-server jdbc


【解决方案1】:

想通了,如果你问我,对于一个不明显的问题,这是一个蹩脚的错误;

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

不工作,改为使用;

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

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

【讨论】:

    猜你喜欢
    • 2020-10-07
    • 1970-01-01
    • 2018-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多