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 > cursor and data fetching when columns are not known

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 12-10-09, 06:21
rpkulkarni rpkulkarni is offline
Registered User
 
Join Date: Nov 2009
Posts: 11
cursor and data fetching when columns are not known

How to fetch the data from cursor where number of columns returned and their data type is unknown.
Here is the example

Code:
declare c1 cursor for select * from table1;
fetch c1 into .....
What can replace ..... if I do not know the schema of table1.

My DB2 version : DB2 universal database 9.5

Please advise.
Reply With Quote
  #2 (permalink)  
Old 12-10-09, 08:10
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
I suggest you figure out the columns from the catalog.

Andy
Reply With Quote
  #3 (permalink)  
Old 12-10-09, 10:27
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
Have a look at the DESCRIBE statement.
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
  #4 (permalink)  
Old 12-10-09, 11:36
Lenny77 Lenny77 is offline
Registered User
 
Join Date: Jul 2009
Location: NY
Posts: 886
Lightbulb Use DCLGEN of table1 and INCLUDE...

Quote:
Originally Posted by rpkulkarni View Post
How to fetch the data from cursor where number of columns returned and their data type is unknown.
Here is the example

Code:
declare c1 cursor for select * from table1;
fetch c1 into .....
What can replace ..... if I do not know the schema of table1.

My DB2 version : DB2 universal database 9.5

Please advise.
You can make DCLGEN of table1 and INCLUDE this DCLGEN in your program.

Compiler will find something like this:

Code:
EXEC SQL DECLARE table1 TABLE    
( column1 ............................
)
01  DCLGEN-TABLE1.  
    10 COLUMN1........................
Then you can
Code:
fetch c1 into :DCLGEN-TABLE1
Lenny
Reply With Quote
  #5 (permalink)  
Old 12-10-09, 12:15
rpkulkarni rpkulkarni is offline
Registered User
 
Join Date: Nov 2009
Posts: 11
My Question is about the variable names and their data type.

Code:
OPEN C1; FETCH c1 into var1, var2 ...
I can get the column names of the table. But for fetching data into variables var1, var2 etc. I need to define them before using them in FETCH statements. How this can be done dynamically? I hope I am able to convey my problem correctly.

Please advise.
Reply With Quote
  #6 (permalink)  
Old 12-10-09, 12:24
Lenny77 Lenny77 is offline
Registered User
 
Join Date: Jul 2009
Location: NY
Posts: 886
Thumbs down

Quote:
Originally Posted by rpkulkarni View Post
My Question is about the variable names and their data type.

Code:
OPEN C1; FETCH c1 into var1, var2 ...
I can get the column names of the table. But for fetching data into variables var1, var2 etc. I need to define them before using them in FETCH statements. How this can be done dynamically? I hope I am able to convey my problem correctly.

Please advise.
I shown to you how. You can't understand....

I can't help you anymore.

Lenny
Reply With Quote
  #7 (permalink)  
Old 12-11-09, 11:24
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
Quote:
Originally Posted by rpkulkarni View Post
My Question is about the variable names and their data type.

Code:
OPEN C1; FETCH c1 into var1, var2 ...
I can get the column names of the table. But for fetching data into variables var1, var2 etc. I need to define them before using them in FETCH statements. How this can be done dynamically? I hope I am able to convey my problem correctly.

Please advise.
You can fetch into a SQLDA and then process the SQLDA entries. It is a bit more involved than directly fetching into a host variable, but it's not complicated either.
__________________
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