Oracle正在SQL中运行我的函数,用于在WHERE子句中过滤掉的数据

时间:2015-09-11 01:34:12

标签: sql oracle oracle11g

我有一个Oracle SQL语句,它抓取一些数据,并将其传递给返回值的函数。如果收到的数据无效,该函数将引发应用程序错误。

该函数引发错误,因为它正在获取它不应该具有的数据,即使该数据已在where子句中过滤掉(或通过隐式内部联接)。

基本上,似乎Oracle在将数据过滤掉之前将其传递给函数。我该如何防止这种情况?

您可以在查询运行的函数下面看到注释掉的内容。最后一个谓词AND WHSE.WIDX = 5632使这个SQL返回0结果,但是没有它,该值将被传递给PAS.PAS_MERCH_PLAN.GET_WHSE_CUST_PRICE_SCHEME函数。

    SELECT
      DISTINCT WHSE.WIDX
    FROM
      AP21.STKAUD
    LEFT JOIN
      AP21.POZD
      ON POZD.POZDIDX = STKAUD.TRANIDX
    LEFT JOIN
      AP21.PORDD 
      ON PORDD.PORDDIDX = POZD.PORDDIDX
    LEFT JOIN 
      AP21.PORD
      ON PORD.PORDIDX = PORDD.PORDIDX
    INNER JOIN
      AP21.WHSE
      ON WHSE.WIDX = STKAUD.WHSIDX
   -- INNER JOIN
   --   AP21.REFCODE CURRENCY ON
   --   CURRENCY.RCIDX = PAS.PAS_MERCH_PLAN.GET_PRICE_SCHEME_CURRENCY(PAS.PAS_MERCH_PLAN.GET_WHSE_CUST_PRICE_SCHEME(WHSE.WIDX))
    INNER JOIN
      AP21.REFCODE WHSE_RC ON
      WHSE_RC.RCIDX = STKAUD.WHSIDX
    LEFT OUTER JOIN
    (
      SELECT
        BUSDIV,
        WHSEIDX
      FROM
        AP21.GIT_MERCH_PLANNING_CONFIG PLAN_CONF
      UNPIVOT (WHSEIDX FOR WAREHOUSE_CODE IN (WHSE1,WHSE2,WHSE3,WHSE4,WHSE5))
    ) RETAIL_WAREHOUSE ON
      RETAIL_WAREHOUSE.WHSEIDX = WHSE.WIDX
      AND RETAIL_WAREHOUSE.BUSDIV = WHSE.REF1
    WHERE
      (
        WHSE.TYPE = 'R'
        OR RETAIL_WAREHOUSE.WHSEIDX IS NOT NULL
      )
      AND
      (
        STKAUD.TRANTYPE = '231' --TRANSFER OUT
        OR STKAUD.TRANTYPE = '32125' --STOCK WRITEDOWN
        OR STKAUD.TRANTYPE = '32124' --DIRECT STOCK TRANSFER
        OR (STKAUD.TRANTYPE = '210' AND COLUMN1 = 0 AND NVL(PORD.ARMS_LENGTH_DIRECT,0) <> 1 ) --WAREHOUSE PURCHASE ORDER RECEIPTS excluding Intercompany
        OR (STKAUD.TRANTYPE = '104' AND COLUMN1 = 0) --WAREHOUSE PRODUCTION RECEIPTS
      )
      AND TRANDATE >= TRUNC(SYSDATE-1)
      AND TRANDATE < TRUNC(SYSDATE-1)+1
      AND WHSE.WIDX = 5632

1 个答案:

答案 0 :(得分:0)

您可以使用CASE语句强制短路条件,例如写一个函数IS_VALID(widx)

INNER JOIN
AP21.REFCODE CURRENCY ON
CURRENCY.RCIDX = 
    CASE WHEN IS_VALID(WHSE.WIDX) = 'Y' 
    THEN PAS.PAS_MERCH_PLAN.GET_PRICE_SCHEME_CURRENCY(PAS.PAS_MERCH_PLAN.GET_WHSE_CUST_PRICE_SCHEME(WHSE.WIDX))
    END