SQL Server存储过程不返回值

时间:2022-08-23 10:14:21

below is my stored procedure I am calling from the Java program, I want to return if any record exits in the database, Here everything is working fine, but when if or else condition executes, it doe not return any value, Because in the Java am storing the return value in the variable file_generation and this variable is always holding the value 1.

下面是我从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 个解决方案

#1


1  

If I'm not mistaken you're trying to call Stored Procedure with a Return Status. In this case you should register your return value, and specify it in the call:

如果我没弄错的话,你试图用退货状态调用存储过程。在这种情况下,您应该注册您的返回值,并在通话中指定它:

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));

#1


1  

If I'm not mistaken you're trying to call Stored Procedure with a Return Status. In this case you should register your return value, and specify it in the call:

如果我没弄错的话,你试图用退货状态调用存储过程。在这种情况下,您应该注册您的返回值,并在通话中指定它:

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));