I am trying to use SimpleJdbcCall to invoke a stored procedure without providing the parameter that have default values but SimpleJdbcCall fails with the below errors. Would really appreciate if some one could help me understand what is wrong with the code.
Database I use is SQL Server.
I followed this tutorial - https://docs.spring.io/spring-framework/docs/3.0.0.M3/reference/html/ch13s05.html
Stored procedure that I am trying to invoke from the Java code:
CREATE PROCEDURE createUser
@address varchar(250) = NULL,
@name varchar(25) ,
@Id numericid = 0 OUTPUT
AS
BEGIN
INSERT INTO TCustomer (name, address)
VALUES (@name, @address)
SELECT @Id = id
FROM TCustomer
WHERE name = @name AND address = @address
SELECT @Id
RETURN 0
END
Below are the different ways I tried to get it to work
Scenario:1 with withNamedBinding() and @ as part of the input variable name
public int createUser(){
SimpleJdbcCall spCall = new SimpleJdbcCall(jdbcTemplate)
.withProcedureName("createUser")
.withSchemaName("dbo")
.withoutProcedureColumnMetaDataAccess()
.useInParameterNames("@name")
.withNamedBinding()
.declareParameters(new SqlParameter("@name", Types.VARCHAR),
new SqlOutParameter("@Id", Types.NUMERIC));
Map<String, Object> in = new HashMap<>();
in.put("name","Rahul");
Map<String, Object> retValue = spCall.execute(in);
}
Error Message: Error message org.springframework.dao.InvalidDataAccessApiUsageException: Required input parameter '@name' is missing.
Scenario : 2 with withNamedBinding() and without @ as part of the input variable name
public int createUser(){
SimpleJdbcCall spCall = new SimpleJdbcCall(jdbcTemplate)
.withProcedureName("createUser")
.withSchemaName("dbo")
.withoutProcedureColumnMetaDataAccess()
.useInParameterNames("name")
.withNamedBinding()
.declareParameters(new SqlParameter("name", Types.VARCHAR),
new SqlOutParameter("Id", Types.NUMERIC));
Map<String, Object> in = new HashMap<>();
in.put("name","Rahul");
Map<String, Object> retValue = spCall.execute(in);
}
Error message org.springframework.jdbc.BadSqlGrammarException: CallableStatementCallback; bad SQL grammar [{call createUser(name => ?, Id => ?)}]; nested exception is java.sql.SQLException: Invalid parameter index 1.
Scenario : 3 without withNamedBinding() and without @ as part of the input variable name
public int createUser(){
SimpleJdbcCall spCall = new SimpleJdbcCall(jdbcTemplate)
.withProcedureName("createUser")
.withSchemaName("dbo")
.withoutProcedureColumnMetaDataAccess()
.useInParameterNames("name")
.declareParameters(new SqlParameter("name", Types.VARCHAR),
new SqlOutParameter("Id", Types.NUMERIC));
Map<String, Object> in = new HashMap<>();
in.put("name","Rahul");
Map<String, Object> retValue = spCall.execute(in);
}
Error message org.springframework.jdbc.BadSqlGrammarException: CallableStatementCallback; bad SQL grammar [{call TCustomer(?, ?)}]; nested exception is java.sql.SQLException: The formal parameter "@name" was not declared as an OUTPUT parameter, but the actual parameter passed in requested output.
Scenario:4 without withNamedBinding() and with @ as part of the input variable name
public int createUser(){
SimpleJdbcCall spCall = new SimpleJdbcCall(jdbcTemplate)
.withProcedureName("createUser")
.withSchemaName("dbo")
.withoutProcedureColumnMetaDataAccess()
.useInParameterNames("@name")
.declareParameters(new SqlParameter("@name", Types.VARCHAR),
new SqlOutParameter("@Id", Types.NUMERIC));
Map<String, Object> in = new HashMap<>();
in.put("name","Rahul");
Map<String, Object> retValue = spCall.execute(in);
}
Error message org.springframework.dao.InvalidDataAccessApiUsageException: Required input parameter '@name' is missing