If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > Help With Procedure

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 06-15-11, 15:05
snowcl16 snowcl16 is offline
Registered User
 
Join Date: Oct 2010
Posts: 5
Help With Procedure

I have the following query:

Code:
CREATE PROCEDURE MYPROC( 
IN @TABLES VARCHAR(500)) 
DYNAMIC RESULT SETS 1 
LANGUAGE SQL 
SPECIFIC MYPROC
NOT DETERMINISTIC 
MODIFIES SQL DATA 
CALLED ON NULL INPUT 
BEGIN 

 --declare variables 

DECLARE @TABLE VARCHAR ( 100 ) ; 
DECLARE SQLCODE INTEGER DEFAULT 0;
DECLARE C1 CURSOR FOR   
SELECT * FROM TABLE(split(@TABLES, ';')) AS FUNC; 

 -- create temp table 
DECLARE GLOBAL TEMPORARY TABLE SESSION . KEYS 
( 
    INMATEID INTEGER NOT NULL , 
    ROWID INT GENERATED ALWAYS AS IDENTITY ( START WITH 1 , INCREMENT BY 1 ) 
) 

WITH REPLACE ON COMMIT PRESERVE ROWS NOT LOGGED ; 

IF ( @INMATEID IS NOT NULL ) THEN 
    INSERT INTO SESSION . KEYS ( INMATEID ) VALUES ( @INMATEID ) ; 
ELSE 

    INSERT INTO SESSION . KEYS ( INMATEID ) 
    SELECT MyTable.ID
    FROM MYTABLE;
END IF ; 

 -- select tables 

OPEN C1;
FETCH C1 INTO @TABLE;

WHILE (SQLCODE = 0) DO
BEGIN

If (@TABLE = 'INMATES') THEN
   SELECT INMATES.* FROM ATABLE  <-- THIS IS THE PROBLEM SPOT!
END IF;

FETCH C1 INTO @TABLE;
END;

END WHILE;
CLOSE C1;
END;
The problem is marked in the above procedure. I want to loop through the passed in 'tables' and SELECT data from the tables that are passed in. The problem is that I cannot do a SELECT statement within my while loop and if statement. Are there any syntax problems that you see or is it something else entirely?
Reply With Quote
  #2 (permalink)  
Old 06-16-11, 11:35
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
What should happen with the rows returned by the SELECT statement? You don't consume them. Do you want to open a cursor that is to be returned to the caller? Do you want to fetch the results and do something with them? Either way, you'll have to declare a cursor for this query and do something with that cursor.
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On