0

我必须将FlightDate,FlightNumbers作为参数传递给 Oracle 存储过程,如下所示。

CREATE OR REPLACE PROCEDURE GetPaxDetails(
   FlyingDate IN PAX_DETAILS.FlightDate%TYPE, 
   FlightNumbers IN VARCHAR(300))
IS
BEGIN
    -- Assume that we've received **FlightNumbers** as '0620,0712,0154'
    -- Basically I am trying to select all passenger details (PAX_DETAILS) 
    -- for those flights passed in as a parameter(**FlightNumbers**) 
    -- for the specified dates
    -- In this procedure the query should be generated something like below:
    SELECT * 
      FROM PAX_DETAILS 
     WHERE TO_DATE(FlightDate,'DDMONYY') BETWEEN TO_DATE(FlyingDate,'DDMONYY') 
                                             AND TO_DATE(FlyingDate,'DDMONYY') + 1 
       AND FlightNo IN ('0620,0712,0154')

END

我不明白如何将参数转换为查询内部的FlightNumbers东西。AND FlightNo IN ('0620,0712,0154')

4

1 回答 1

4

作为其中一种方法,regexp_substr()regexp_count()正则表达式函数可用于将源字符串拆分为表行,然后您可以轻松地将该结果包含在您的IN子句中:

-- splitting a comma separated string
select regexp_substr(FlightNumbers, '[^,]+', 1, level) as num
  from dual
connect by level <= regexp_count(FlightNumbers, '[^,]+')

因此,例如,如果您传入FlightNumbers'0620,0712,0154',上述查询将产生以下结果:

NUM
--------
0620
0712
0154

有了它,您的最终查询将是:

with numbers(num) as(
   select regexp_substr(FlightNumbers, '[^,]+', 1, level) as num
     from dual
  connect by level <= regexp_count(FlightNumbers , '[^,]+')
 )
SELECT *           -- do not forget INTO clause
  FROM PAX_DETAILS 
 WHERE TO_DATE(FlightDate,'DDMONYY') BETWEEN TO_DATE(FlyingDate,'DDMONYY') 
                                         AND TO_DATE(FlyingDate,'DDMONYY') + 1 
   AND FlightNo IN (select num
                      from numbers);
于 2013-07-23T05:34:47.830 回答