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 > db2 stored procedure involving global temporay table

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-21-05, 16:25
poorviparikh poorviparikh is offline
Registered User
 
Join Date: Jul 2005
Posts: 12
db2 stored procedure involving global temporay table

The following gets created fine:
connect to iiradb@

drop procedure poorvi.updt_rank@

create procedure poorvi.updt_rank (in lvl int)
result sets 1
LANGUAGE SQL
begin
declare r_date date;
declare r_id varchar(30);
declare r_broker char(4);
declare r_totqty bigint;
declare r_rank int;
declare end int default 0;

declare global temporary table session.temp (
trans_date date ,
id varchar(12),
broker char(4),
tot_qty bigint,
rank int )
on commit preserve rows with replace in user_temp;

insert into session.temp
select trans_date,
id,
broker,
tot_qty,
dense_rank() over (partition by id order by (tot_qty) desc)
from
poorvi.advrank
where
aggrlvl = lvl ;
declare C1 cursor for select r.trans_date,r.id,r.broker,r.tot_qty,r.rank
from session.temp r ;
end
@

connect reset@


however when this is modified as follows:
connect to iiradb@

drop procedure poorvi.updt_rank@

create procedure poorvi.updt_rank (in lvl int)
result sets 1
LANGUAGE SQL
begin
declare r_date date;
declare r_id varchar(30);
declare r_broker char(4);
declare r_totqty bigint;
declare r_rank int;
declare end int default 0;

declare global temporary table session.temp (
trans_date date ,
id varchar(12),
broker char(4),
tot_qty bigint,
rank int )
on commit preserve rows with replace in user_temp;

declare C1 cursor for select r.trans_date,r.id,r.broker,r.tot_qty,r.rank
from session.temp r ;
DECLARE continue HANDLER FOR not found set end = 1;
(lines added)

insert into session.temp
select trans_date,
id,
broker,
tot_qty,
dense_rank() over (partition by id order by (tot_qty) desc)
from
poorvi.advrank
where
aggrlvl = lvl ;
declare C1 cursor for select r.trans_date,r.id,r.broker,r.tot_qty,r.rank
from session.temp r ;
end
@

connect reset @

i get the following error:
DB21034E The command was processed as an SQL statement because it was not a valid Command Line Processor command. During SQL processing it returnedQL0104N An unexpected token "<cursor declaration>" was found following "". Expected tokens may include: "<SQL statement>". LINE NUMBER=20. SQLSTATE=42601


I understand it does not like the cursor declaration, however this is what i need to do: read row by row from temporary table so i use this to update rows in another table.


any ideas anybody?
-- thank you
Reply With Quote
  #2 (permalink)  
Old 09-21-05, 17:47
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
That's because DECLARE CURSOR must be in the declaration section, and the global temp table declaration ends the declaration section.

I guess a workaround could be something like this:
Code:
DECLARE qry VARCHAR() DEFAULT "select r.trans_date,r.id,r.broker,r.tot_qty,r.rank
from session.temp r ";

DECLARE CURSOR c1 FOR stmt;

DECLARE GLOBAL TEMPORARY TABLE...

PREPARE stmt FROM qry;

OPEN c1; 

...
Reply With Quote
  #3 (permalink)  
Old 09-22-05, 08:01
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
The better way is like this:

DECLARE GLOBAL TEMPORARY TABLE ...

-- Then start a new compound statement to declare the cursor that references the GTT

BEGIN
DECLARE CURSOR ...

OPEN CURSOR ...
END; -- Begin

HTH

Andy
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