Error "Incorrect syntax near '@P0'" while calling stored procedure

7,505 views
Skip to first unread message

om5

unread,
Apr 6, 2011, 6:52:04 PM4/6/11
to mybatis-user
I have searched the forum but have not been able to figure out this
particular problem. The error is as follows. Your help is appreciated.

org.apache.ibatis.exceptions.PersistenceException:
### Error updating database. Cause:
com.microsoft.sqlserver.jdbc.SQLServerException: Incorrect syntax near
'@P0'.
### The error may involve
org.mybatis.guice.sample.mapper.UserMapper.insertUser-Inline
### The error occurred while setting parameters
### Cause: com.microsoft.sqlserver.jdbc.SQLServerException: Incorrect
syntax near '@P0'.
at
org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:
8)
at
org.apache.ibatis.session.defaults.DefaultSqlSession.update(DefaultSqlSession.java:
120)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:
39)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:
25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.ibatis.session.SqlSessionManager
$SqlSessionInterceptor.invoke(SqlSessionManager.java:253)
at $Proxy42.update(Unknown Source)
at
org.apache.ibatis.session.SqlSessionManager.update(SqlSessionManager.java:
187)
at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:
59)
at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:38)
at $Proxy43.insertUser(Unknown Source)
at
org.mybatis.guice.sample.service.FooServiceMapperImpl.insertUser(FooServiceMapperImpl.java:
60)


Environment: myBatis 3.0.4 and SQL Server 2008

Mapper:
<update id="insertUser"
parameterType="org.mybatis.guice.sample.domain.User"
statementType="CALLABLE">
call insert_user (#{id,mode=IN,jdbcType=VARCHAR},
#{name,mode=IN,jdbcType=VARCHAR})
</update>

Stored Procedure:
CREATE PROCEDURE insert_user @id varchar(80), @name varchar(80)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;

-- Insert statements for procedure here
INSERT users (id, name) VALUES (@id, @name)

END
GO

UserMapper.java:
public interface UserMapper {

User getUser(String userId);
void updateUser(User user);
void insertUser(User user);
}

FooServiceImpl.java
public User insertUser(User user)
{
this.userMapper.insertUser(user);
return user;
}



Poitras Christian

unread,
Apr 7, 2011, 8:43:57 AM4/7/11
to mybati...@googlegroups.com
Hi,

I think most drivers don't care, but the correct syntax to call a procedure is
{call <procedure-name>[(<arg1>,<arg2>, ...)]}

So try to add these brackets {} around your procedure call.

Christian

-----Message d'origine-----
De : mybati...@googlegroups.com [mailto:mybati...@googlegroups.com] De la part de om5
Envoyé : April-06-11 6:52 PM
À : mybatis-user
Objet : Error "Incorrect syntax near '@P0'" while calling stored procedure

om5

unread,
Apr 7, 2011, 9:33:26 AM4/7/11
to mybatis-user
That worked. The brackets appear to be a requirement for MS SQL
Server driver. Thanks.

On Apr 7, 7:43 am, Poitras Christian <Christian.Poit...@ircm.qc.ca>
wrote:
Reply all
Reply to author
Forward
0 new messages