Oracle中的重叠函数

时间:2014-04-29 13:36:35

标签: sql oracle oracle11g

我正在写一个包来学习Oracle。我想创建一个OVERLAP函数来检查两个日期范围是否相互重叠。

  FUNCTION OVERLAP(p_START_DATE_1 DATE, p_END_DATE_1 DATE,
   p_START_DATE_2 DATE, p_END_DATE_2 DATE) RETURN VARCHAR2 AS

   lv_RESULT VARCHAR2(1);
  BEGIN 

   lv_RESULT := SELECT 'T' AS overlap FROM dual
       WHERE (p_START_DATE_1, p_END_DATE_1) overlaps (p_START_DATE_2, p_END_DATE_2);

   IF (lv_RESULT = 'T')
     RETURN 'T';

   RETURN 'N';

  END OVERLAP;

我尝试执行我的功能,但收到错误ORA-04063: package body 'XYZ' contains errors ...

SELECT KP_XYZ_PACKAGE_SQL.OVERLAP(
       TO_DATE('01/01/2014', 'DD/MM/YYYY'),
       TO_DATE('01/12/2014', 'DD/MM/YYYY'),
       TO_DATE('01/02/2014', 'DD/MM/YYYY'),
       TO_DATE('01/05/2014', 'DD/MM/YYYY'))
FROM DUAL;

我认为SELECT运作正常。但是错误发生(我想)在这里:lv_RESULT := SELECT...。为什么呢?

2 个答案:

答案 0 :(得分:2)

试试这个:

CREATE OR REPLACE FUNCTION OVERLAP(p_START_DATE_1 DATE, p_END_DATE_1 DATE,
   p_START_DATE_2 DATE, p_END_DATE_2 DATE) RETURN VARCHAR2 AS

   lv_RESULT VARCHAR2(1);
  BEGIN 
       lv_RESULT := 'N';      

       SELECT 'T' into lv_RESULT FROM dual
           WHERE (p_START_DATE_1, p_END_DATE_1) overlaps (p_START_DATE_2, p_END_DATE_2);

   IF (lv_RESULT = 'T') THEN
     RETURN 'T';
   END IF;

   RETURN 'N';

  END OVERLAP;

IF声明也是不完整的 - 那我和END IF都丢失了,我现在已经补充了。

SELECT INTO语句的文档是 here 。页面末尾有一些示例链接。

答案 1 :(得分:0)

更正后的版本:对于True,返回'T',对于False,返回N'

CREATE OR REPLACE FUNCTION OVERLAP(p_START_DATE_1 DATE, p_END_DATE_1 DATE,
    p_START_DATE_2 DATE, p_END_DATE_2 DATE) RETURN VARCHAR2 AS

  lv_RESULT VARCHAR2(1);

 BEGIN 
    SELECT 'T' into lv_RESULT FROM dual WHERE (p_START_DATE_1, p_END_DATE_1) overlaps (p_START_DATE_2, p_END_DATE_2);
 RETURN lv_RESULT;

  EXCEPTION
      WHEN NO_DATA_FOUND THEN lv_RESULT := 'N'; 
  RETURN lv_RESULT;

END OVERLAP;
/