How to debug SQL stored procedure script

182 views Asked by At

I am trying to create a temp table, adding accounts to temp table and getting eventid joining temp table and "contractactivity". I am using SQL stored procedure (Aqua Data Studio 7.0.39).

This is what I have so far:

  DROP PROCEDURE Demo_Prod ()                           
  GO

CREATE PROCEDURE Demo_Prod ()
language sql
begin
    declare SQLSTATE CHAR(5);
    declare del_eventid bigint;
    declare v_rows integer;
    DECLARE GLOBAL TEMPORARY TABLE TEMP_OTC (
      ACCOUNTID INTEGER, 
      DEAL VARCHAR(32), 
      DEALVERSION INTEGER
    )
    ON COMMIT PRESERVE ROWS
    WITH REPLACE
    INSERT INTO SESSION.TEMP_OTC
    select ACCOUNTID, DEAL, DEALVERSION
    from CONTRACTPOSITION b
    where BUSITHRU > '2020-01-01 00:00:00'
    and PROCOUT > '2020-01-01 00:00:00'

    and ACCOUNTID in (11111,22222,33333,44444,55555)
    and OPENCLOSEIND = 'O'
    
    declare recdata cursor with hold for
    select eventId
    from CONTRACTACTIVITY a
    where DOCIN < '2020-01-01 00:00:00'
    and (ACCOUNTID, DEAL, DEALVERSION) not in  (
        select ACCOUNTID, DEAL, DEALVERSION from SESSION.TEMP_OTC    
    )
    and ACCOUNTID in (
        11111,22222,33333,44444,55555
    )
    for read only with ur

   
 
    close recdata;
    commit;
end 

And I am getting the following error:-

>[Error] Script lines: 3-84 -------------------------
 DB2 SQL Error: SQLCODE=-104, SQLSTATE=42601, SQLERRMC=INSERT;WS
    WITH REPLACE
;<psm_semicolon>, DRIVER=3.50.152
 Message: An unexpected token "INSERT" was found following "WS
    WITH REPLACE
".  Expected tokens may include:  "<psm_semicolon>".. SQLCODE=-104, SQLSTATE=42601, DRIVER=3.50.152
 Line: 13 

---code from line 1 - 21 :-

1.  DROP PROCEDURE Demo_Prod ()                           
2.  GO
3.  
4.  CREATE PROCEDURE Demo_Prod()
5.  language sql
6.  begin
7.  declare SQLSTATE CHAR(5);
8.  declare del_eventid bigint;
9.  declare v_rows integer;
10. DECLARE GLOBAL TEMPORARY TABLE TEMP_OTC (
11. ACCOUNTID INTEGER, 
12. DEAL VARCHAR(32), 
13. DEALVERSION INTEGER
14. )
15. ON COMMIT PRESERVE ROWS
16. WITH REPLACE
17. INSERT INTO SESSION.TEMP_OTC
18. select ACCOUNTID, DEAL, DEALVERSION
19. from CONTRACTPOSITION b
20. where BUSITHRU > '2020-01-01 00:00:00'
21. and PROCOUT > '2020-01-01 00:00:00'

What am I doing wrong, and how can I resolve the error?

1

There are 1 answers

1
Joe Stefanelli On

Terminate the creation of the temp table with a semicolon after WITH REPLACE.

...
DECLARE GLOBAL TEMPORARY TABLE TEMP_OTC (
  ACCOUNTID INTEGER, 
  DEAL VARCHAR(32), 
  DEALVERSION INTEGER
)
ON COMMIT PRESERVE ROWS
WITH REPLACE;
INSERT INTO SESSION.TEMP_OTC
...