在Sybase数据库中使用Spring无法获得单个输出值

时间:2013-04-01 22:10:18

标签: java spring stored-procedures sybase

存储过程

CREATE PROCEDURE Proc1
AS
BEGIN
DECLARE @retVal int,
    @Err    int,
    @Rows   int

SELECT @retVal = isnull(max(col1), 0) + 1 FROM Table1

BEGIN tran

INSERT INTO Table1(
    col1,
    col2

)
VALUES(@col1,@col2)

SELECT @Err = @@error, @Rows = @@rowcount

IF @Err != 0 or @Rows != 1
BEGIN
     rollback tran
     return -75
END

commit tran
return @retVal
END

Java代码

public class InsTable1 extends StoredProcedure implements Executable
{


    public InsTable1 (DataSource dataSource)
    {
        super(dataSource, "Proc1");


        super.declareParameter(new SqlOutParameter("retval", Types.INTEGER));

        super.compile();


    }



    public String executeQuery()
    {
        String retval = "";
        Map<String, Object> inParams = new HashMap<String, Object>(1);

        Map outParams = execute(inParams);

        retval = outParams.get("retval") == null ? "" : outParams.get("retval").toString().trim();
        return retval;
    }
}

错误

  

可调用语句没有返回与应用程序为

注册的输出参数一样多的输出参数

记录确实已插入。但即使我删除输出参数声明它也不起作用。 这可能是什么解决方案?

1 个答案:

答案 0 :(得分:0)

您需要获取返回值,这不是输出参数。

在声明参数时添加withReturnValue(),确保您在outParams地图中获得“返回”键。

我认为您的代码应如下所示:(未经过测试)

public class InsTable1 extends StoredProcedure implements Executable
{
    public InsTable1 (DataSource dataSource)
    {
        super(dataSource, "Proc1");
        super.declareParameter(new SqlOutParameter("retval", Types.INTEGER));
        super.withReturnValue();
        super.compile();
    }

    public String executeQuery()
    {
        String retval = "";
        Map<String, Object> inParams = new HashMap<String, Object>(1);
        Map outParams = execute(inParams);
        retval = outParams.get("return") == null ? "" : outParams.get("return").toString().trim();
        return retval;
    }
}