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 > Dynamic Table Names

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-03-05, 13:13
cthornhi cthornhi is offline
Registered User
 
Join Date: Feb 2005
Posts: 8
Dynamic Table Names

Is it possible to use a stored procedure to create a dynamic table name based on the name passed to the stored proc?

e.g.,

create procedure mktbl(name varchar(10))
begin
create table qgpl.&name(
col01 numeric(10)
);

end;
Reply With Quote
  #2 (permalink)  
Old 02-03-05, 13:55
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
Yes. Use dynamic SQL to do this.

DECLARE STMT varchar(5000);
SET STMT = 'CREATE TABLE ' || TableParm || ...
PREPARE s1 for STMT;
EXECUTE s1;

Andy
Reply With Quote
  #3 (permalink)  
Old 02-03-05, 17:25
cthornhi cthornhi is offline
Registered User
 
Join Date: Feb 2005
Posts: 8
Thumbs up

That worked perfectly.... thank you very much!!!
Reply With Quote
  #4 (permalink)  
Old 02-03-05, 18:22
cthornhi cthornhi is offline
Registered User
 
Join Date: Feb 2005
Posts: 8
Question Insert/Update

Will I need to use the Prepare/Execute method for inserting/updating tables based on a dynamic name, as well?

Also thought of an issue where I have prepared a CURSOR that references a particular table.... How do I use the Prepare/Execute method, or will I even need to?

Thank you....

Last edited by cthornhi; 02-03-05 at 18:55.
Reply With Quote
  #5 (permalink)  
Old 02-03-05, 19:38
jacampbell jacampbell is offline
Registered User
 
Join Date: Jan 2005
Posts: 191
Just as a shortcut, you can use
DECLARE STMT varchar(5000);
SET STMT = 'CREATE TABLE ' || TableParm || ...
EXECUTE IMMEDIATE stmt;
to cut out an entire SQL statement. Personally I only use prepare and execute as two statements if I am going to re-issue the dynamic statement or have parameter markers in the statement.

For insert and update you use either execute immediate or prepare and execute. If you are going to be issuing either insert or update more than once you should probably use parameter markers, prepare once and execute often.

For a cursor use something like:
DECLARE SEL_STMT varchar(5000);
DECLARE C1 CURSOR FOR S1;
SET STMT = 'SELECT ... ';
PREPARE S1 FROM SEL_STMT;
OPEN C1;
FETCH C1 INTO ...;

James Campbell
Reply With Quote
  #6 (permalink)  
Old 02-04-05, 01:01
cthornhi cthornhi is offline
Registered User
 
Join Date: Feb 2005
Posts: 8
Unhappy passing a string

Okay.... I think I'm getting there (thanks to Andy and James), but I am having one more problem.

How do I include strings in my stmt?

For instance,
stmt = SELECT 'Y' FROM table
WHERE col01 = 'someValue'

The ' are causing me problems and I can not figure out how to get around it. In many programming languages it's as simple as using two ', but that's not working for me.

I've also seen online an option to use a cursor and do the following:
DECLARE c1 CURSOR x
stmt = 'SELECT ? FROM table
WHERE col01 = ?';
PREPARE x FROM stmt;
OPEN c1 USING 'Y', 'someValue';

BUT... Couldn't seem to get this to compile, either.

Thank you!!!

Last edited by cthornhi; 02-04-05 at 01:37.
Reply With Quote
  #7 (permalink)  
Old 02-04-05, 13:02
cthornhi cthornhi is offline
Registered User
 
Join Date: Feb 2005
Posts: 8
I was actually able to get everything to work.... Not sure why it wasn't working last night, but guess I needed to take a break.

Anyway, passing a string is as simple as...

stmt = 'SELECT ? FROM table
WHERE col01 = ?';

PREPARE s1 FROM stmt;

EXECUTE s1 USING 'Y', 'SomeValue';

Thanks for all of the help!!!
Reply With Quote
  #8 (permalink)  
Old 02-06-05, 17:21
jacampbell jacampbell is offline
Registered User
 
Join Date: Jan 2005
Posts: 191
Just to expand your expertise: When DB2 is preparing your statement it doesn't know what datatypes you are going to be presenting. Therefore it generates a generic access path - you'll have to do an explain to determine just how it handles easch statement. But as an example if col01 is in an index DB2 might end up doing an index scan rather than an index lookup.

You can give DB2 a boost by using a typed parameter marker:
stmt = 'SELECT ? FROM table WHERE col01 = cast(? as varchar(10))';
for example - where the datatype in the cast is the datatype of col01. "This notation is not a function call, but a "promise" that the type of the parameter at run time will be of the data type specified or some data type that can be converted to the specified data type."

This might give DB2 the information it needs to give you a better access path.

James Campbell
Reply With Quote
  #9 (permalink)  
Old 02-07-05, 22:29
cthornhi cthornhi is offline
Registered User
 
Join Date: Feb 2005
Posts: 8
Thumbs up

Thanks for all of your help, James. BTW, do you know if there is a limitation on the # of input parameters that can be passed in one statement? I have a statement in particular that will require about 13 parameters.

I'm surprised there isn't an easier (excuse my laziness ) to build a dynamic query while doing string comparisons. Now I wish all of my comparisons were based around numbers.
Reply With Quote
  #10 (permalink)  
Old 02-08-05, 01:04
jacampbell jacampbell is offline
Registered User
 
Join Date: Jan 2005
Posts: 191
The only realistic limit I've seen is "Maximum total length of host and indicator variables pointed to in an SQLDA: 32767 bytes"

Unlikely you'll reach that limit.

James Campbell
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