我正在尝试编写一个简单的PL / SQL过程,但我不断收到错误,我不明白。有一些 语法失败,如果有人可以帮助我,我将不胜感激。 我需要创建一个程序,允许我在表格特定的人(varchar2)中插入两个日期(DATE),他的合同期限。 然而,他的合同不能在现有日期之间,因此不能重叠,同一个人可以有多个联系人,但只能在完全不同的时期之间。 我知道我需要使用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;
答案 0 :(得分: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环境,但这应该是您需要的。