Java Spring Jdbc中如何从存储过程中读取返回值(过程中没有out参数)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19582322/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 18:31:51  来源:igfitidea点击:

How read returned value from stored procedure in Spring Jdbc (without out parameters in procedure)

javaspringstored-proceduressybase-asespring-jdbc

提问by Malahov

How can I read returned value from stored procedure in Spring Jdbc (without out parameters) ?

如何从 Spring Jdbc 中的存储过程读取返回值(没有输出参数)?

I am use Sybase ASE database.

我使用的是 Sybase ASE 数据库。

Procedure Example:

程序示例:

CREATE PROCEDURE dbo.procedureA (
 @a int
)
as
begin
    IF EXISTS (SELECT 1 FROM dbo.T WHERE a = @a)
        return 1
    ELSE
        return -1
end

回答by Gerard Ribas

The best approach for handle this is to use SimpleJdbcCallthat it's in the Spring-Jdbc project.

处理此问题的最佳方法是使用它在 Spring-Jdbc 项目中的SimpleJdbcCall

With SimpleJdbcCall you can declare in and out parameters for this purpose.

使用 SimpleJdbcCall 您可以为此目的声明输入和输出参数。

You can do it something like this:

你可以这样做:

SimpleJdbcCall simpleJdbcCall = new SimpleJdbcCall(dataSource);
simpleJdbcCall.withCatalogName("dbo");
simpleJdbcCall.withProcedureName("procedureA ");
simpleJdbcCall.setAccessCallParameterMetaData(false);
simpleJdbcCall.declareParameters(new new SqlOutParameter("a",Types.NUMERIC));
simpleJdbcCall.execute();

More info here

更多信息在这里

Hope it helps.

希望能帮助到你。

回答by Siddiqi

This might help, although I haven't tried it myself yet;

这可能会有所帮助,尽管我自己还没有尝试过;

http://forum.spring.io/forum/spring-projects/data/114859-return-value-of-stored-procs-using-storedprocedure-class-and-not-simplejdbccall

http://forum.spring.io/forum/spring-projects/data/114859-return-value-of-stored-procs-using-storedprocedure-class-and-not-simplejdbccall

public MyStoredProc(JdbcTemplate jdbcTemplate) {
    super(jdbcTemplate, "mystoredproc");
    setFunction(true);
    declareParameter(new SqlOutParameter("Result", Types.NUMERIC));
    declareParameter(new SqlInOutParameter("otherparam1", Types.INTEGER));
    declareParameter(new SqlParameter("otherparam2", Types.INTEGER));
    allowsUnusedParameters();
    compile();
}

回答by Jeff

Reference: http://www.databaseskill.com/1270151/Key point is: SimpleJdbcCall.withReturnValue() directs the SimpleJdbcCall object to put the return value in the result map with key "RETURN_VALUE". So you can access it like (Integer)simpleJdbcCall.execute().get("RETURN_VALUE")

参考:http://www.databaseskill.com/1270151/ 重点是:SimpleJdbcCall.withReturnValue() 指示 SimpleJdbcCall 对象将返回值放入结果映射中,键为“RETURN_VALUE”。所以你可以像 (Integer)simpleJdbcCall.execute().get("RETURN_VALUE") 一样访问它

For Sybase, it is not necessary to declare SqlOutParameter "RETURN_VALUE"

对于 Sybase,不需要声明 SqlOutParameter "RETURN_VALUE"