DB2 [SQL0029] INTO clause missing from embedded statement

629 Views Asked by At

UPDATE to my question. I found the answer and will edit the sample code. Thanks to those who tried to help me.

The following test script checks out okay in the tool "Run SQL Scripts" and with an online SQL Syntax checker. However, when deploying it as a Stored Procedure to a DB2 database on our IBM I using IBM Data Studio, it fails with the "[SQL0029] INTO clause missing from embedded statement". Searched far and wide in Google but nothing I found helped. Need a sanity check. What am I doing wrong?

CREATE PROCEDURE uspTotTripsCreatByAgtMoYr (
IN pForMonth    NUMERIC(2),
IN pForYear     NUMERIC(4)
)
    RESULT SETS 1
    LANGUAGE SQL

P1: BEGIN

-- Temp table.
DECLARE GLOBAL TEMPORARY TABLE SESSION.tmpTTBA 
(
    UID CHAR(8) NOT NULL,
    NUMTRPCREATED   NUMERIC(6)  NOT NULL,
    ENDATEYR    NUMERIC(4)  NOT NULL,
    ENDATEMO    NUMERIC(2)  NOT NULL,
    DIVCODE CHAR(2) DEFAULT 'ZZ'
) WITH REPLACE ON COMMIT PRESERVE ROWS;

-- BEGIN PROOF OF CONCEPT
INSERT INTO SESSION.tmpTTBA (UID, NUMTRPCREATED, ENDATEYR, ENDATEMO)
SELECT  
    TM."RecordEntryUserID", 
    COUNT(TM."TripNumber"),
    TM."RecordEntryDateYear", 
    TM."RecordEntryDateMonth"   
FROM FXDTAZZ."TripMaster" "TM" 
GROUP BY TM."RecordEntryUserID",
    TM."RecordEntryDateYear",
    TM."RecordEntryDateMonth"
HAVING (TM."RecordEntryDateYear" = pForYear)     
AND (TM."RecordEntryDateMonth" = pForMonth);    

-- END PROOF OF CONCEPT

-- Sanity check POC
-- SELECT * FROM SESSION.tmpTTBA;
-- The previous SELECT line by itself is what was causing 
-- the SQL0029 error in the RUN SQL SCRIPT Editor. 
-- Here is the way to do it. Slaps himself in the forehead
-- for not figuring it out earlier. The important thing was
-- enclosing it in a new BEGIN...END routine block, along
-- with declaring and opening a cursor.
    BEGIN 
        DECLARE c_tmp CURSOR WITH RETURN TO CLIENT FOR           
            -- returns all rows
            SELECT * FROM SESSION.tmpTTBA;  
        OPEN c_tmp;
    END;

-- BEGIN AGGREGATION CODE FOR ALL SCHEMAS HERE
-- Start with FXDTAAA and loop to the end with FXDTAZZ

-- END AGGREGATION 

-- Clean up
DROP TABLE SESSION.tmpTTBA;
END P1
1

There are 1 best solutions below

2
RockBoro On

this compiles. select into a variable. Not sure how it runs without returning a result set.

CREATE PROCEDURE uspTotTripsCreatByAgtMoYr (
IN pForMonth    NUMERIC(2),
IN pForYear     NUMERIC(4)
)
    RESULT SETS 1
    LANGUAGE SQL

P1: BEGIN

declare     vUid char(8) default ' ' ;

-- Temp table.
DECLARE GLOBAL TEMPORARY TABLE SESSION.tmpTTBA 
(
    UID CHAR(8) NOT NULL,
    NUMTRPCREATED   NUMERIC(6)  NOT NULL,
    ENDATEYR    NUMERIC(4)  NOT NULL,
    ENDATEMO    NUMERIC(2)  NOT NULL,
    DIVCODE CHAR(2) DEFAULT 'ZZ'
) WITH REPLACE ON COMMIT PRESERVE ROWS;

-- BEGIN PROOF OF CONCEPT
INSERT INTO SESSION.tmpTTBA (UID, NUMTRPCREATED, ENDATEYR, ENDATEMO)
SELECT  
    TM."RecordEntryUserID", 
    COUNT(TM."TripNumber"),
    TM."RecordEntryDateYear", 
    TM."RecordEntryDateMonth"   
FROM FXDTAZZ."TripMaster" "TM" 
GROUP BY TM."RecordEntryUserID",
    TM."RecordEntryDateYear",
    TM."RecordEntryDateMonth"
HAVING (TM."RecordEntryDateYear" = pForYear)     
AND (TM."RecordEntryDateMonth" = pForMonth);    

-- END PROOF OF CONCEPT

-- Sanity check POC       
SELECT   uid              
into     vUid             
FROM     SESSION.tmpTTBA  
fetch first row only ;    

-- BEGIN AGGREGATION CODE FOR ALL SCHEMAS HERE
-- Start with FXDTAAA and loop to the end with FXDTAZZ

-- END AGGREGATION 

-- Clean up
DROP TABLE SESSION.tmpTTBA;
END P1