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 > Can't declare a cursor on a temp table

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-30-03, 07:04
muggyd muggyd is offline
Registered User
 
Join Date: Sep 2003
Location: London
Posts: 5
Can't declare a cursor on a temp table

I am trying to declare a cursor on a temporary table as follows:

DECLARE GLOBAL TEMPORARY TABLE SESSION.TMP_WORKLIST AS
(SELECT Timesheet.* FROM DB2ADMIN.Timesheet ) DEFINITION ONLY WITH REPLACE ON COMMIT PRESERVE ROWS NOT LOGGED;
DECLARE worklist_Cursor CURSOR WITH HOLD FOR
(SELECT WeekEnding, UserID, ClientID, ProjectID, Task, Subtask, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday, Comments
FROM SESSION.TMP_WORKLIST );

There are no other statements other than DECLAREs before these lines. When I try to compile, I get the error:

SQL0104N An unexpected token "<cursor declaration>" was found following "".
Expected tokens may include: "<SQL statement>". LINE NUMBER=25.

If I move the DECLARE CURSOR statement to before the DECLARE TEMP TABLE it works (but I have to change it to reference a permanent table)
Is it not possible to declare a cursor on a temp table?
Reply With Quote
  #2 (permalink)  
Old 09-30-03, 08:12
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
Yes it is, and there is a little trick to do it. It has to do with the syntax of a compound statement.

In a compound statement the declare section has to be up front. So then how do you declare something that references something else before it is known? Simple, coumpound statements can contain other compound statements, so create an internal coumpound statement.


Try it this way:

DECLARE GLOBAL TEMPORARY TABLE SESSION.TMP_WORKLIST AS
(SELECT Timesheet.* FROM DB2ADMIN.Timesheet ) DEFINITION ONLY WITH REPLACE ON COMMIT PRESERVE ROWS NOT LOGGED;

begin atomic

DECLARE worklist_Cursor CURSOR WITH HOLD FOR
(SELECT WeekEnding, UserID, ClientID, ProjectID, Task, Subtask, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday, Comments
FROM SESSION.TMP_WORKLIST );

....

end;

HTH

Andy
Reply With Quote
  #3 (permalink)  
Old 09-30-03, 08:59
muggyd muggyd is offline
Registered User
 
Join Date: Sep 2003
Location: London
Posts: 5
Thumbs up

Worked a treat. Many thanks!
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