0

我正在尝试编写一个简单的 PL/SQL 过程,但我不断收到我不理解的错误。有一些语法错误,如果有人可以帮助我,我将不胜感激。我需要创建一个程序,允许我在表中插入具有两个日期(DATE)的特定人员(varchar2),即他的合同期限。但是他的合同不能在现有日期之间,因此不能重叠,同一个人可以有多次联系,但只能在完全不同的时期之间。我知道我需要使用 INTERSECT,只是我很困惑如何在这里使用它,因为我只有一张桌子。我以前与两张表相交,我如何在这里相交?我也不确定我是否可以有 2 个 if 语句 谢谢!

CREATE OR REPLACE PROCEDURE test1
(name             VARCHAR2,
 startDate        DATE,
 endDate          DATE) 
AS
overlap       NUMBER := 0;
CURSOR cursor IS 
SELECT date_from, date_to FROM contractTable;
BEGIN
   FOR row IN cursor LOOP
   /*check if there is an overlap between dates*/
     IF (startDate, endDate) INTERESECT (SELECT date_from, date_to FROM ContractTable) THEN
  overlap := overlap + 1;       
  END IF;
  /*if there isn't, then insert new contact for this person*/
  IF overlap <= 0 THEN
    INSERT INTO ContractTable VALUES(name, startDate, endDate);
  END IF;
  END test1; 
4

1 回答 1

0
CREATE PROCEDURE test1 (l_name IN VARCHAR2, l_start_date IN DATE, l_end_date IN DATE) 
IS 

    l_overlap_check NUMBER;

BEGIN

    SELECT count(*) INTO l_overlap_check
      FROM contract 
     WHERE (l_start_date between start_date and end_date 
           OR
            l_end_date between start_date and end_date)
      AND l_name = name;

    IF l_overlap_check = 0 THEN
        INSERT INTO contract (name, start_date, end_date)
                      VALUES (l_name, l_start, l_end);
   END IF;

END;

我没有方便的 Oracle 环境,但这应该是您需要的。

于 2012-12-19T11:31:35.793 回答