tags:

views:

10

answers:

0

I'm trying to debug this function to get the desired results. Where am I going wrong ? See attachment for the function code and the test data insert script.

CREATE OR REPLACE
FUNCTION          VC_CALC_WD_DATE(p_date in date, p_days IN NUMBER)
RETURN DATE AS
   v_delta  NUMBER(10,0) DEFAULT 0;
   v_start_id  vc_workingdays.wk_id%type DEFAULT 0;
   SWV_RetExp DATE;
   v_date vc_workingdays.wk_date%type;
BEGIN
   v_delta := p_days;
   v_date := trunc(p_date);

-- Get the actual start date as a working day
SELECT nvl(WK_ID,0) INTO v_start_id
  FROM VC_WORKINGDAYS
WHERE WK_DATE = v_date;

if v_start_id = 0 then
  For cnt in (SELECT WK_DATE FROM VC_WORKINGDAYS where v_date <> WK_DATE)
  loop
     IF p_days >= 0 THEN
            SELECT MIN(WK_ID)
              INTO v_start_id
            FROM VC_WORKINGDAYS
           WHERE WK_DATE > V_DATE;
      ELSE
            SELECT MAX(WK_ID) INTO v_start_id
            FROM VC_WORKINGDAYS
            WHERE WK_DATE < v_date;
           -- EXCEPTION
            --WHEN NO_DATA_FOUND THEN NULL;

      END IF;
  end loop;
END IF;
-- Modify v_delta to take account of following rules
-- If the value -1 is passed the function should return the current working day, or the most recent working day
-- If the value 0 is passed then the current day, or next working day if a passed a non-working day, should be returned.
   IF v_delta < 0 then
      v_delta := v_delta+1;
   END IF;
   --begin
      SELECT nvl((SELECT WK_DATE
                FROM VC_WORKINGDAYS
                WHERE WK_ID = v_start_id+v_delta) ,sysdate)
        INTO SWV_RetExp
      FROM dual;
      /*EXCEPTION
      WHEN NO_DATA_FOUND THEN NULL;*/
   --end;
   RETURN SWV_RetExp;
   exception
      WHEN NO_DATA_FOUND THEN 
      dbms_output.put_line ( SQLERRM );
      return sysdate;
       NULL;
END;

----Create Test Table

CREATE TABLE VC_WORKINGDAYS 
(
WK_ID number NUMBER(10,0),
WK_DATE DATE,
);

-- Insert test Data

INSERT INTO VC_WORKINGDAYS_1 VALUES (308, '25-MAR-11');
INSERT INTO VC_WORKINGDAYS_1 VALUES (316, '06-APR-11');
INSERT INTO VC_WORKINGDAYS_1 VALUES (324, '18-APR-11');
INSERT INTO VC_WORKINGDAYS_1 VALUES (332, '03-MAY-11');
INSERT INTO VC_WORKINGDAYS_1 VALUES (340, '13-MAY-11');
INSERT INTO VC_WORKINGDAYS_1 VALUES (348, '25-MAY-11');
INSERT INTO VC_WORKINGDAYS_1 VALUES (356, '06-JUN-11');
INSERT INTO VC_WORKINGDAYS_1 VALUES (364, '16-JUN-11');
INSERT INTO VC_WORKINGDAYS_1 VALUES (372, '28-JUN-11');
INSERT INTO VC_WORKINGDAYS_1 VALUES (380, '08-JUL-11');
INSERT INTO VC_WORKINGDAYS_1 VALUES (388, '20-JUL-11');
INSERT INTO VC_WORKINGDAYS_1 VALUES (396, '01-AUG-11');
INSERT INTO VC_WORKINGDAYS_1 VALUES (404, '11-AUG-11');

.

SQL ----- Result Should be WHY

SELECT (VC_CALC_WD_DATE(LAST_DAY(TRUNC(SYSDATE)),1)) FROM DUAL 14/JUL/10 15/JUL/10 is 1 working day from today
SELECT (VC_CALC_WD_DATE(LAST_DAY(TRUNC(SYSDATE)),2)) FROM DUAL 14/JUL/10 16/JUL/10 is 2 working days from today
SELECT (VC_CALC_WD_DATE(LAST_DAY(TRUNC(SYSDATE)),3)) FROM DUAL 14/JUL/10 19/JUL/10 is 3 working days from today