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 > Selecting a variable table name, how to do it?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-11-07, 05:30
tingshen tingshen is offline
Registered User
 
Join Date: Oct 2006
Posts: 115
Question Selecting a variable table name, how to do it?

for a simple embedded SQL query in Cobol like this:
Depending on parameter given, if parameter = 1, I need to run this query:
select * from file1
if the parameter = 2, i need to run this query:
select * from file2

the name of the "file" is fixed but the number depends on the parameter. How can I achieve it? I can't use the colon sign like we do it for conditions, at the table selection level.
Reply With Quote
  #2 (permalink)  
Old 07-11-07, 12:05
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
You must build your SQL statement in a string and process it as dynamic SQL, i.e. prepare the statement, possibly describe it, ...
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
  #3 (permalink)  
Old 07-11-07, 22:12
tingshen tingshen is offline
Registered User
 
Join Date: Oct 2006
Posts: 115
May I know what's the command line to use in a cobol programme to execute a SQL statement? I'm running on AS400 now.

My problem is, the query is actually part of the cursor which is embedded into my Cobol programme.
Reply With Quote
  #4 (permalink)  
Old 07-12-07, 03:56
guyprzytula guyprzytula is offline
Registered User
 
Join Date: Jun 2006
Posts: 471
input parameter : eg. var1 keeping value '1'
you could do :
var2='file'||var1 or concatenate string with variable
or with conditional create var2
if var1 =1 then
var2=...
end
var3='select * from '||var2
exec sql prepare xxx from var3
exec sql declare ci1 cursor for xxx
exec sql open cursor C1
__________________
Best Regards, Guy Przytula
DB2 UDB LUW certified V6/7/8
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