pl / sql功能中的多个查询

时间:2018-08-14 08:24:53

标签: oracle plsql

我在pl / sql中有一个函数,我希望有三个查询,每个查询都会设置一个变量(存在,撤回和日期),返回的字符串取决于此变量的组合。

CREATE OR REPLACE FUNCTION get_cat_status (systemId IN NUMBER)
    RETURN VARCHAR2
AS
    status      VARCHAR2 (2000) := 'Null';
    exist       NUMBER := 1;
    withdrawn   NUMBER := 0;
    dates       NUMBER := 0;
BEGIN
    BEGIN
          SELECT DISTINCT result
            INTO exist
            FROM SEEDTEST_RESULT
           WHERE     SEEDTEST_RESULT.RESULT = '1'
                 AND SEEDTEST_RESULT.TEST_TYPE = 'C'
                 AND SEEDTEST_RESULT.SLRN_ID = systemId;
    EXCEPTION
        WHEN NO_DATA_FOUND
        THEN
            exist := 1;
    END;

    BEGIN
      SELECT 1
        INTO withdrawn
        FROM CROP
       WHERE     CROP.SYSTEM_ID = systemId
             AND NVL ( (CROP.ORIG_AREA - NVL (CROP.ADD_AREA, 0)), 0) = 0;

   EXCEPTION
    WHEN NO_DATA_FOUND
    THEN
        withdrawn := 1;
   END;

   BEGIN
      SELECT DATE_3_PROC  --replaced with 1 and works
        INTO dates
        FROM CROP
       WHERE CROP.SYSTEM_ID = systemId;
   EXCEPTION
    WHEN NO_DATA_FOUND
    THEN
        dates := 1;
   END;

    IF (dates = 0 AND withdrawn = 1)
    THEN
        status := 'NO RESULT';
    END IF;

    IF (dates = 1 AND exist = 1)
    THEN
        status := 'REJECTED';
    END IF;

    RETURN (status);
END get_cat_status;

我已尝试添加它们,但到目前为止它不起作用。有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

此功能正在执行我现在想要的操作。在第三次查询之前,由于DATE_3_PROC是一个日期,因此我无法将其分配给一个数字 dates 。在原始Access应用程序中,只需检查每个查询是否都没有返回记录并更新相应的变量即可。在Oracle中,我将其与 into 混合在一起,后者已经将值分配给了变量,但这没有任何意义。现在,当查询不返回任何记录时,我分配0而不是1,并且当有记录时它将分配为1。

Create or replace FUNCTION get_cat_status(systemId IN Number) 
 RETURN VARCHAR2 
 AS 
   status VARCHAR2(2000) := ' ';
   exist NUMBER;
   withdrawn NUMBER;
   dates NUMBER;
 BEGIN 

  BEGIN
   SELECT DISTINCT 1 INTO exist
   FROM SEEDTEST_RESULT
   WHERE SEEDTEST_RESULT.RESULT = '1'
     AND SEEDTEST_RESULT.TEST_TYPE = 'C'
     AND SEEDTEST_RESULT.SLRN_ID = systemId;
   EXCEPTION
     WHEN NO_DATA_FOUND THEN
       exist := 0;  
   END;  

   BEGIN      
     SELECT 1 INTO withdrawn
    FROM CROP
    WHERE CROP.SYSTEM_ID = systemId
      AND NVL((CROP.ORIG_AREA - NVL(CROP.ADD_AREA, 0)), 0) = 0;
    EXCEPTION
      WHEN NO_DATA_FOUND THEN
        withdrawn := 0;  
  END;

  BEGIN
    SELECT 1 INTO dates 
    FROM CROP
    WHERE CROP.SYSTEM_ID = systemId;
    EXCEPTION
      WHEN NO_DATA_FOUND THEN
        dates := 0;  
  END;      

  IF (dates = 1 and withdrawn = 0) THEN 
    status := 'NO RESULT';
  END IF;
  IF (dates = 0 and exist = 0) THEN 
    status := 'REJECTED';
  END IF;

  RETURN(status); 

END get_cat_status;