2017-03-07 97 views
-1

下面是我从Java程序调用的存储过程,我想返回如果有任何记录在数据库中退出,这里一切工作正常,但是当if或else条件执行时,它母鹿不返回任何价值,因为在我的Java的变量file_generation存储返回值,这个变量总是抱着值1SQL服务器存储过程不返回值

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

      } 
+2

我们需要你正在试图做的,正在发生的事情是什么,你希望发生什么更多的信息.​​..样本数据和期望的结果有很长的路要走。 – Siyual

回答

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