SQL Server存储过程不返回值

时间:2017-03-07 13:35:52

标签: java sql sql-server java-ee

下面是我从Java程序调用的存储过程,如果数据库中有任何记录退出,我想返回,这里一切正常,但是当if或else条件执行时,它不会返回任何值,因为在Java中我将返回值存储在变量file_generation中,并且该变量始终保持值为1。

USE [esp_server]
GO
/****** Object:  StoredProcedure [dbo].[cs_file_generation_lock]    Script Date: 3/7/2017 1:26:21 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[cs_file_generation_lock]
    @pid INTEGER
AS
BEGIN

    DECLARE @RESULT VARCHAR(6);
    DECLARE @X_LOCK INTEGER;
    DECLARE @records INTEGER;
    DECLARE @FLAG INT = 0;
    DECLARE @FLAG1 INT = 1;

        IF exists (SELECT PID from cs_file_generation_flag)
                BEGIN
                    INSERT INTO cs_file_generation_flag VALUES(4056,@FLAG)
                    Return @FLAG
                END

        ELSE
                BEGIN
                    INSERT INTO cs_file_generation_flag VALUES(@pid,@FLAG1)
                    Return @FLAG1       
                END 


END

Below is my java code
try
            {
                Connection cn = null;
                CallableStatement stmt = null;
                try
                {
                    cn = JdbcUtils.getConnection();
                    long pid = ConfigServer.getPID();

                    stmt = cn.prepareCall("{call cs_file_generation_lock(?)}");
                    stmt.setLong(1, pid);
                    int file_generation = stmt.executeUpdate();
                    JdbcUtils.commitAndClose(cn, stmt);
                    if(file_generation == 1)
                    {
                        trace1 = TerminalIdTracer.getInstance();
                        trace1.log(ConfigServerTrace.INFO, "--------------file generation process will start and the file generation value is-- " + file_generation + "process ID :" + pid);
                        new PreProcess().go(args);
                    }
                    else
                    {
                        trace1.log(ConfigServerTrace.INFO, "---------------Please wait the other file generation is in process---------------" + file_generation);

                    }

                }
                finally
                {
                    JdbcUtils.cleanup(cn, stmt);
                }

            }

1 个答案:

答案 0 :(得分:1)

如果我没弄错的话,你试图打电话给Stored Procedure with a Return Status。在这种情况下,您应该注册您的返回值,并在通话中指定它:

stmt = cn.prepareCall("{?=call cs_file_generation_lock(?)}");
stmt.registerOutParameter(1, java.sql.Types.INTEGER);
stmt.setLong(2, pid);
stmt.execute();
System.out.println("Flag: " + stmt.getInt(1));