473,388 Members | 1,408 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

Temporary Tables and Cursors to read them

All,

I am trying to declare a cursor on global temporary table. I declared the global temporary table first and then the cursor but I got the following error:

42601(-104)[IBM][CLI Driver][DB2/NT64] SQL0104N An unexpected token "<cursor declaration>" was found following "". Expected tokens may include: "<SQL statement>". LINE NUMBER=11. SQLSTATE=42601

Here is the stored procedure defintion:

Create Procedure get_monitor_info (in userid varchar(10), out rstring varchar(2048))
Dynamic Result Sets 0
Modifies SQL Data
Language SQL
Begin
Declare tmpstring varchar(2048);
Declare cstring varchar(200);
Declare recordcnt integer default 0;
Declare loopcnt integer default 0;
Declare global temporary table monitortmp (name varchar(200)) on commit preserve rows not logged;
declare c1 cursor for select name from session.monitortmp;

begin
open c1
fetch
close cl
drop temporary table
end

I have had some success if I use multiple begin and end blocks. I declare the temporary table in the first block and then declare the cursor in the second block. The problem is that the second block does not seem to be executed.

Create Procedure DB2MON.get_monitor_info (in userid varchar(10), out rstring varchar(2048))
Dynamic Result Sets 0
Modifies SQL Data
Language SQL
S1: Begin
Declare tmpstring varchar(2048);
Declare cstring varchar(200);
Declare recordcnt integer default 0;
Declare loopcnt integer default 0;
Declare global temporary table monitortmp (name varchar(200)) on commit preserve rows not logged;
declare c1 cursor for select name from session.monitortmp;

insert into session.monitortmp
select count(*) into recordcnt from session.monitortmp;

S2: Begin
declare c1 cursor for select name from session.monitortmp;
open c1;
while loopcnt <= recordcnt do
fetch c1 into cstring;
set tmpstring = tmpstring || cstring || ';';
set loopcnt = loopcnt + 1;
end while;
close c1;
End S2;
set rstring = tmpstring;
/* drop table session.monitortmp; */
End S1

Questions:

1. What is the best way to create a temp table and a cursor against it?
2. How do I get the stored procedure to execute the second block of code?
May 16 '07 #1
1 12351
frozenmist
179 Expert 100+
Hi Kburton,

I did not not try out the explicit cursor way. But i tries with implicit cursor and it worked fine.
This is what I did
Expand|Select|Wrap|Line Numbers
  1. create procedure db2admin.sample
  2. language sql
  3. begin
  4. declare global temporary table sample_table (col1 integer) on commit preserve rows;
  5. insert into session.sample_table values(1);
  6. for v_cursor as select * from session.sample_table
  7. do
  8.  update session.sample_table set col1= col1+v_cursor.col1;
  9. end for;
  10. end
  11.  
  12.  
The value in the Global temporary table was 2 after executing the procedure.
Hope this helps.
However I will try with explicit cursor later. In the meantime some one else may be able to help you on that.

Cheers
May 17 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Corrine | last post by:
Hi, I am creating a global temporary table that is session-specific. I insert a BLOB into this table, and then select the BLOB from this table into a ResultSet. The ResultSet sees this BLOB...
1
by: goonsquad | last post by:
I have a multi-statement sql script that creates two temporary tables and then runs a query on them. The query runs fine. However, I want to use phpMyAdmin to export the data to .csv format. The...
11
by: randi_clausen | last post by:
Using SQL against a DB2 table the 'with' key word is used to dynamically create a temporary table with an SQL statement that is retained for the duration of that SQL statement. What is the...
5
by: Todd Huish | last post by:
I have noticed something disturbing when retrieving datasets over a relatively slow line (multiple T1). I am looking at about 25 seconds to retrieve 500 rows via a php-odbc link. This same select...
11
by: Hemant Shah | last post by:
Folks, I have a perl script that creates and uses global termporary table. This script worked fine with UDB 7.2 on AIX. Sometime ago I moved the database to UDB 8 on Linux. The select statement...
7
by: Krisa | last post by:
I've begun using the technique of putting temporary tables in a temporary database in my C:\Temp folder. Everything is fine except I can't kill the database when exiting Access. I get a...
4
by: Coleen | last post by:
Hi All :-) Can anyone give me a URL where I can find a good example of code on how to create a temporary SQL table using VB.net? I've checked the Microsoft site at: ...
1
by: Stefan van Roosmalen | last post by:
Hi there, Is there a way to list the TEMPORATY tables? I have tried SHOW TABLES, but this command only list the regular tables. Thank you very much for your answer. Regards, Stefan.
5
by: Rahul B | last post by:
Hi, I have very little knowledge about creating Procedures/functions in DB2. When i tried to create the test function like CREATE FUNCTION GET_TEST (P_TEST_ID INTEGER, P_SEL_OR_SORT...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.