【发布时间】: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