PL / SQL:使用oracle函数忽略SQL语句

时间:2019-02-11 07:16:51

标签: sql oracle plsql

我尝试编译该函数,但它们显示错误“ PL / SQL:SQL语句已忽略”,并提及函数“ PO_PROD_CODE”。 您能帮忙提些意见吗?

  

PL / SQL:使用oracle函数(函数“ PO_PROD_CODE”)忽略了SQL语句

  FUNCTION PO_PROD_CODE (s_prod_code VARCHAR2) RETURN VARCHAR2 IS

   v_po_prod_code_result   VARCHAR2(100);

   BEGIN 

     SELECT FLEX_VALUE 
        INTO v_po_prod_code_result
      FROM apps.FND_FLEX_VALUES_VL

    WHERE  attribute1 IN ('BXX', 'EXX', 'GXX', 'HKXX', 'PDX', 'PSX', 'GPX') 
      AND FLEX_VALUE = s_prod_code ;

  RETURN v_po_prod_code_result;
  END;
  /

   FUNCTION PO_DOC_LOCATION_TEXT (N_ORG_ID NUMBER, N_PO_HEADER_ID NUMBER) RETURN VARCHAR2 IS

  v_po_doc_location_text  VARCHAR2(150);                                      
  s_prod_code VARCHAR2(20); 
   BEGIN

      SELECT 'PO\'||PO_DIVISION_CODE||'\'||PO_PRODUCT_LINE||'\'||PO_NUM||'\' AS "DOC_TEXT"
        INTO v_po_doc_location_text
        FROM (SELECT  poh.SEGMENT1 PO_NUM,
                      poh.ORG_ID,
                      poh.ATTRIBUTE14 AS "PO_PRODUCT_LINE",
                      poh.ATTRIBUTE15 AS "PO_DIVISION",
            CASE (SELECT ffvv.attribute1 bs_group
                        FROM apps.fnd_flex_values_vl ffvv,
                            apps.fnd_flex_value_sets ffvs
                        WHERE ffvv.flex_value_set_id = ffvs.flex_value_set_id
                            AND ffvs.flex_value_set_name = 'XXX_PROD_LINES'
                            AND ffvv.enabled_flag = 'Y'
                            AND ((ffvv.attribute4 = 'Y' AND poh.ORG_ID = 83) OR (ffvv.attribute5 = 'Y' AND poh.ORG_ID = 87))
                            AND ffvv.flex_value = PO_PROD_CODE (v_po_prod_code_result, s_prod_code) --call the "PO_PROD_CODE" function get the sql feild "prod_line_code")

                                  WHEN 'ABC'    THEN 'AB'   --FLEX_VALUE_ID,FLEX_VALUE PRO_CODE,
                                  WHEN 'DEF'    THEN 'DE'
                                  WHEN 'GSD'    THEN 'HK'
                                  WHEN 'HIG'    THEN 'HG'
                                  WHEN 'PIG'    THEN 'PG'
                                  WHEN 'BCE'    THEN 'BE'
                                  WHEN 'GP'     THEN 'HK' 

                                  ELSE ''
                                 END PO_DIVISION_CODE  
                FROM APPS.PO_HEADERS_ALL poh
                WHERE poh.ORG_ID = N_ORG_ID
                  AND poh.po_header_id = N_PO_HEADER_ID

       );

    EXCEPTION
      WHEN OTHERS THEN
      v_po_doc_location_text := 'PO\MO\';
    END;
    RETURN v_po_doc_location_text + s_prod_code;
END;

成功!  当我尝试运行脚本时,详细信息如下:

SELECT abc.XXX_util.PO_DOC_LOCATION_TEXT ('83','1145225') from dual;

错误消息说

  

ORA-06502:PL / SQL:数字或值错误:字符到数字的转换错误

2 个答案:

答案 0 :(得分:1)

它不是FROM ... INTO,而是INTO ... FROM

FUNCTION PO_PROD_CODE (s_prod_code VARCHAR2) RETURN VARCHAR2 IS
   v_po_prod_code_result   VARCHAR2(100);

BEGIN 
     SELECT FLEX_VALUE 
      FROM apps.FND_FLEX_VALUES_VL           --> this is
      INTO v_po_prod_code_result             --> wrong
    WHERE  attribute1 IN ('BXX', 'EXX', 'GXX', 'HKXX', 'PDX', 'PSX', 'GPX') 
      AND FLEX_VALUE = s_prod_code ;
  RETURN v_po_prod_code_result;
END;
/

应该是

     SELECT FLEX_VALUE 
      INTO v_po_prod_code_result             
      FROM apps.FND_FLEX_VALUES_VL          

[编辑]

第二个函数的CASE无效。该SELECT应该移出它,它的表用于主SELECT的FROM子句中。像这样的东西(语法上可以,我不知道它是否可以工作):

FUNCTION PO_DOC_LOCATION_TEXT (N_ORG_ID NUMBER, N_PO_HEADER_ID NUMBER)
   RETURN VARCHAR2
IS
   v_po_doc_location_text  VARCHAR2 (150);
BEGIN
   SELECT    'PO\'
          || PO_DIVISION_CODE
          || '\'
          || PO_PRODUCT_LINE
          || '\'
          || PO_NUM
          || '\'
             AS "DOC_TEXT"
     INTO v_po_doc_location_text
     FROM (SELECT poh.SEGMENT1 PO_NUM,
                  poh.ORG_ID,
                  poh.ATTRIBUTE14 AS "PO_PRODUCT_LINE",
                  poh.ATTRIBUTE15 AS "PO_DIVISION",
                  CASE ffvv.attribute1
                     WHEN 'ABC' THEN 'AB' --FLEX_VALUE_ID,FLEX_VALUE PRO_CODE,
                     WHEN 'DEF' THEN 'DE'
                     WHEN 'GSD' THEN 'HK'
                     WHEN 'HIG' THEN 'HG'
                     WHEN 'PIG' THEN 'PG'
                     WHEN 'BCE' THEN 'BE'
                     WHEN 'GP' THEN 'HK'
                     ELSE ''
                  END
                     PO_DIVISION_CODE
             FROM APPS.PO_HEADERS_ALL poh,
                  apps.fnd_flex_values_vl ffvv,
                  apps.fnd_flex_value_sets ffvs
            WHERE     ffvv.flex_value_set_id = ffvs.flex_value_set_id
                  AND ffvs.flex_value_set_name = 'XXX_PROD_LINES'
                  AND ffvv.enabled_flag = 'Y'
                  AND (   (    ffvv.attribute4 = 'Y'
                           AND poh.ORG_ID = 83)
                       OR (    ffvv.attribute5 = 'Y'
                           AND poh.ORG_ID = 87))
                  AND ffvv.flex_value =
                         PO_PROD_CODE (v_po_prod_code_result, s_prod_code)
                  AND poh.ORG_ID = N_ORG_ID
                  AND poh.po_header_id = N_PO_HEADER_ID);

   RETURN v_po_doc_location_text;
EXCEPTION
   WHEN OTHERS
   THEN
      v_po_doc_location_text   := 'PO\MO\';
END;

答案 1 :(得分:1)

您的代码中有几个错误,您必须全部纠正它们:

  • 您应该先编写SELECT INTO来编写INTO语句,然后再编写FROM(如您在PO_DOC_LOCATION_TEXT函数中编写的一样)
  • 您的WHEN-THEN比较中缺少CASE一词
  • 在PO_DOC_LOCATION_TEXT函数中,您通过传递两个参数来调用PO_PROD_CODE函数,但该函数仅接受一个参数。您应该只传递输入参数。如果要用PO_PROD_CODE函数的返回值填充局部变量,则应在SQL查询之外进行。
  • 您在PO_DOC_LOCATION_TEXT函数中的查询中缺少一个)括号,我想这是在call the "PO_PROD_CODE" function get the sql feild "prod_line_code"注释中丢失的那个括号
  • 您要在RETURN语句之前结束PO_DOC_LOCATION_TEXT函数。您应该在BEGIN之前添加一个SELECT INTO

我还可以看到您的PO_PROD_CODE函数(如现在所写)始终返回输入值(s_prod_code)。 在说完全相同的fnd_flex_values_vl.flex_value必须等于flex_value(即函数的输入参数)之后,就选择了s_prod_code作为返回变量。

我想这就是您希望代码像这样的方式:

FUNCTION PO_PROD_CODE (s_prod_code VARCHAR2)
    RETURN VARCHAR2
IS
    v_po_prod_code_result   VARCHAR2 (100);

BEGIN
    SELECT flex_value
      INTO v_po_prod_code_result
      FROM apps.fnd_flex_values_vl
     WHERE     attribute1 IN ('BXX',
                              'EXX',
                              'GXX',
                              'HKXX',
                              'PDX',
                              'PSX',
                              'GPX')
           AND flex_value = s_prod_code; -- STILL JUST RETURNS THE INPUT PARAMETER

    RETURN v_po_prod_code_result;
END;
/

FUNCTION PO_DOC_LOCATION_TEXT (N_ORG_ID NUMBER, N_PO_HEADER_ID NUMBER)
    RETURN VARCHAR2
IS
    v_po_doc_location_text   VARCHAR2 (150);
    s_prod_code VARCHAR2(20); -- NEEDS TO HAVE SOME VALUE ASSIGNED BEFORE PASSING IT TO "PO_PROD_CODE" FUNCTION BELOW IN CODE

BEGIN
    BEGIN
        SELECT 'PO\' || PO_DIVISION_CODE || '\' || PO_PRODUCT_LINE || '\' || PO_NUM || '\' AS "DOC_TEXT"
          INTO v_po_doc_location_text
          FROM (SELECT poh.SEGMENT1 PO_NUM,
                       poh.ORG_ID,
                       poh.ATTRIBUTE14 AS "PO_PRODUCT_LINE",
                       poh.ATTRIBUTE15 AS "PO_DIVISION",
                       CASE (SELECT ffvv.attribute1 bs_group
                               FROM apps.fnd_flex_values_vl ffvv, apps.fnd_flex_value_sets ffvs
                              WHERE     ffvv.flex_value_set_id = ffvs.flex_value_set_id
                                    AND ffvs.flex_value_set_name = 'XXX_PROD_LINES'
                                    AND ffvv.enabled_flag = 'Y'
                                    AND ( (ffvv.attribute4 = 'Y' AND poh.ORG_ID = 83) OR (ffvv.attribute5 = 'Y' AND poh.ORG_ID = 87))
                                    AND ffvv.flex_value = PO_PROD_CODE (s_prod_code) --call the "PO_PROD_CODE" function get the sql feild "prod_line_code", "v_po_prod_code_result" removed because PO_PROD_CODE expects only one parameter and because only input parameters can be written here
                                                                                                           )
                           WHEN 'ABC' THEN 'AB' --FLEX_VALUE_ID,FLEX_VALUE PRO_CODE,
                           WHEN 'DEF' THEN 'DE'
                           WHEN 'GSD' THEN 'HK'
                           WHEN 'HIG' THEN 'HG'
                           WHEN 'PIG' THEN 'PG'
                           WHEN 'BCE' THEN 'BE'
                           WHEN 'GP' THEN 'HK'
                           ELSE ''
                       END
                           PO_DIVISION_CODE
                  FROM APPS.PO_HEADERS_ALL poh
                 WHERE poh.ORG_ID = N_ORG_ID AND poh.po_header_id = N_PO_HEADER_ID);
    EXCEPTION
        WHEN OTHERS
        THEN
            v_po_doc_location_text := 'PO\MO\';
    END;

    RETURN v_po_doc_location_text;
END;

希望我能帮上忙!