How to modify stored procedure to work on date range?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gershwyn
    New Member
    • Feb 2010
    • 122

    How to modify stored procedure to work on date range?

    I have a procedure that populates the orders table with all orders with a delivery date in a seven day period, beginning with today. What I would like to do is be able to specify a beginning and ending date with input parameters, to override the default one week period. The dates in the table are stored as integers in YYYYMMDD format.

    Here is my current (working) procedure:
    Code:
    CREATE PROCEDURE CVTORDERS
    LANGUAGE SQL MODIFIES SQL DATA
    BEGIN
      DECLARE TODAY INTEGER;
      SET TODAY = (SELECT YEAR(CURRENT DATE) * 1000
        + MONTH(CURRENT DATE) * 100 
        + DAY(CURRENT DATE) FROM SYSIBM.SYSDUMMY1);
      DELETE FROM ORDTABLE;
      INSERT INTO ORDTABLE
      SELECT ACCT, ITEM, QTY, PRICE, WKDAY FROM V_ORDERS
      WHERE DDATE BETWEEN TODAY AND TODAY + 6;
    END
    Here is my attempted update, which fails:
    Code:
    CREATE PROCEDURE CVTORDERS
    (IN SDATE INTEGER, IN EDATE INTEGER)
    LANGUAGE SQL MODIFIES SQL DATA
    BEGIN
      DECLARE TODAY INTEGER;
      SET TODAY = (SELECT YEAR(CURRENT DATE) * 1000
        + MONTH(CURRENT DATE) * 100 
        + DAY(CURRENT DATE) FROM SYSIBM.SYSDUMMY1);
      IF SDATE IS NULL THEN
        SET SDATE = TODAY;
      END IF;
      IF EDATE IS NULL THEN
        SET EDATE = TODAY + 6;
      END IF;
      DELETE FROM ORDTABLE;
      INSERT INTO ORDTABLE
      SELECT ACCT, ITEM, QTY, PRICE, WKDAY FROM V_ORDERS
      WHERE DDATE BETWEEN SDATE AND EDATE;
    END
    Running that I get:
    Pointer not set for location referenced.
    Application error. MCH3601 unmonitored by CVTORDERS at statement 0000000010, instruction X'0000'.

    I'm sure it is probably something simple that I'm missing, but I'm not even sure what that error is complaining about. Any advice is appreciated.
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    IN SDATE INTEGER
    I'm not sure the IN is supposed to be there.

    But aside from that, I don't know what else could be wrong. I do see a potential problem though. 20110131. You can't add 6 to that. It is no longer a valid date.

    Comment

    Working...