SQL QUERY不返回ORACLE APEX CLASSIC REPORT的数据

时间:2018-01-18 15:41:01

标签: oracle plsql oracle-apex sql-function oracle-apex-5

我在Oracle application Express 4.2中有一个SQL查询(PL / SQL函数体返回SQL查询)。我创建了一个经典报告,这是我在下面使用的查询。我的查询当前工作正常,但我需要使用另一个查询更新此函数中的现有查询,当我尝试更改查询时它不起作用。我究竟做错了什么?任何帮助表示赞赏。

以下是我的工作查询:

declare
  a_query varchar2(5000);
  this_strin varchar2(50);
begin
  a_query := 'select flight_nmbr, sequence_nmbr '||
         'from flights '||
         'where sequence_nmbr >= 0 and data_version_name = ''' ||
    :P3_DATA_VERSION || '''';
  if :P3_SEARCH_NUMBER is not null then
    if instr(:P3_SEARCH_NUMBER, '%') > 0 then
      this_strin := :P3_SEARCH_NUMBER;
  else
      this_strin := '%'||:P3_SEARCH_NUMBER||'%';
  end if;
  a_query := a_query||chr(10)||
            ' and flight_nmbr like '''|| upper(this_strin) ||'''';
  end if;
  return a_query;
    end;

我需要将此函数内的查询更新为以下内容:

SELECT FLIGHT_NMBR, SCHEDULED_LAUNCH_DATE
FROM FLIGHTS
WHERE DATA_VERSION_NAME = 'WORKING' AND
  sequence_nmbr >= 0
ORDER BY (CASE WHEN to_date(scheduled_launch_date, 'DD-MON-YY') 
BETWEEN add_months(trunc(sysdate, 'MON'), 0) 
AND add_months(trunc(sysdate, 'MON'), 6) THEN 1 ELSE 2 END),
     (CASE WHEN to_date(scheduled_launch_date, 'DD-MON-YY') 
BETWEEN add_months(trunc(sysdate, 'MON'), 0) 
     AND add_months(trunc(sysdate, 'MON'), 6) 
THEN SCHEDULED_LAUNCH_DATE END),
     sequence_nmbr;

我自己尝试这样做,这就是我在下面得到的(这不起作用):

   declare
       a_query varchar2(5000);
    this_strin varchar2(50);
   begin
      a_query := 'select flight_nmbr, sequence_nmbr '||
         'from flights '||
         'where sequence_nmbr >= 0 and data_version_name = ''' ||
         'ORDER BY (CASE WHEN to_date(scheduled_launch_date, 'DD-MON-YY'
          BETWEEN add_months(trunc(sysdate, 'MON'), 0) 
          AND add_months(trunc(sysdate, 'MON'), 6) THEN 1 ELSE 2 END),
          (CASE WHEN to_date(scheduled_launch_date, 'DD-MON-YY') BETWEEN 
           add_months(trunc(sysdate, 'MON'), 0) 
     AND add_months(trunc(sysdate, 'MON'), 6) THEN
             SCHEDULED_LAUNCH_DATE END),
             sequence_nmbr' ||
      :P3_DATA_VERSION || '''';
        if :P3_SEARCH_NUMBER is not null then
        if instr(:P3_SEARCH_NUMBER, '%') > 0 then
           this_strin := :P3_SEARCH_NUMBER;
        else
             this_strin := '%'||:P3_SEARCH_NUMBER||'%';
        end if;
          a_query := a_query||chr(10)||
            ' and flight_nmbr like '''|| upper(this_strin) ||'''';
        end if;
         return a_query;
          end;

1 个答案:

答案 0 :(得分:5)

它不起作用,因为查询中有很多符号'。你需要复制每个引用(你只是为第一个和最后一个做了这个),或者使用以下技巧:

a_query := q'[SELECT FLIGHT_NMBR, SCHEDULED_LAUNCH_DATE
    FROM FLIGHTS
    WHERE DATA_VERSION_NAME = 'WORKING' AND
      sequence_nmbr >= 0
    ORDER BY (CASE WHEN to_date(scheduled_launch_date, 'DD-MON-YY') 
    BETWEEN add_months(trunc(sysdate, 'MON'), 0) 
    AND add_months(trunc(sysdate, 'MON'), 6) THEN 1 ELSE 2 END),
         (CASE WHEN to_date(scheduled_launch_date, 'DD-MON-YY') 
    BETWEEN add_months(trunc(sysdate, 'MON'), 0) 
         AND add_months(trunc(sysdate, 'MON'), 6) 
    THEN SCHEDULED_LAUNCH_DATE END),
         sequence_nmbr]';

q'[ ... ]'内的文字可以包含单个qoutes,并且将被视为内部有qoutes的字符串值。

<强> UPD
举例说明这个想法的一个小例子:

select q'[I'm here]' text from dual;

TEXT   
--------
I'm here