Unix Technical Forum

DB2 Dynamic SQL Table Name in Field Value

This is a discussion on DB2 Dynamic SQL Table Name in Field Value within the DB2 forums, part of the Database Server Software category; --> Hello, I am trying to find some information or an example on how to build a dynamic query in ...


Go Back   Unix Technical Forum > Database Server Software > DB2

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-27-2008, 07:54 AM
devx777@hotmail.com
 
Posts: n/a
Default DB2 Dynamic SQL Table Name in Field Value

Hello,

I am trying to find some information or an example on how to build a
dynamic query in DB2 that would allow me to join a table which its name
is stored as a field value on another table.
I have done this in the past in SQL server, but DB2 is not as easy...

Anyone out there that can help me?

Your help will be much appreciated.

Thanks.

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-27-2008, 07:54 AM
Brian Tkatch
 
Posts: n/a
Default Re: DB2 Dynamic SQL Table Name in Field Value


devx777@hotmail.com wrote:
> Hello,
>
> I am trying to find some information or an example on how to build a
> dynamic query in DB2 that would allow me to join a table which its name
> is stored as a field value on another table.
> I have done this in the past in SQL server, but DB2 is not as easy...
>
> Anyone out there that can help me?
>
> Your help will be much appreciated.
>
> Thanks.


One way to do it is by DECLAREing a CURSOR but having the FOR refer to
a PREPAREd stament, gotten from a variable.

For example:

DECLARE List CURSOR WITH RETURN TO CLIENT FOR Query;
PREPARE Query FROM Text;
OPEN List;

B.

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-27-2008, 07:55 AM
jefftyzzer
 
Posts: n/a
Default Re: DB2 Dynamic SQL Table Name in Field Value

Just amplify what Brian has said, let's say your table name is stored
in a variable called V_TAB, you could do something like this:

CREATE PROCEDURE JTYZZER.TTEST()
DYNAMIC RESULT SETS 1
LANGUAGE SQL
SPECIFIC TTEST
INHERIT SPECIAL REGISTERS
BEGIN
DECLARE V_SQL VARCHAR(64);--
DECLARE V_TAB VARCHAR(128);--
DECLARE C_SQL CURSOR WITH RETURN FOR S_SQL;--
SET V_TAB = 'CODE';--
SET V_SQL = 'SELECT COUNT(*) FROM '||V_TAB||' FOR READ ONLY';--
PREPARE S_SQL FROM V_SQL;--
OPEN C_SQL;--
END;

CALL JTYZZER.TTEST();


Result set 1
--------------

1
-----------
13208

1 record(s) selected.

Return Status = 0

HTH,

--Jeff

Brian Tkatch wrote:
> devx777@hotmail.com wrote:
> > Hello,
> >
> > I am trying to find some information or an example on how to build a
> > dynamic query in DB2 that would allow me to join a table which its name
> > is stored as a field value on another table.
> > I have done this in the past in SQL server, but DB2 is not as easy...
> >
> > Anyone out there that can help me?
> >
> > Your help will be much appreciated.
> >
> > Thanks.

>
> One way to do it is by DECLAREing a CURSOR but having the FOR refer to
> a PREPAREd stament, gotten from a variable.
>
> For example:
>
> DECLARE List CURSOR WITH RETURN TO CLIENT FOR Query;
> PREPARE Query FROM Text;
> OPEN List;
>
> B.


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-27-2008, 07:55 AM
jefftyzzer
 
Posts: n/a
Default Re: DB2 Dynamic SQL Table Name in Field Value

Oops, one more thing: you mentioned "a table which its name
is stored as a field value on another table." For that, you could
assign the V_TAB variable from a scalar fullselect, like so:

SET V_TAB = (SELECT x FROM y WHERE z);--

--Jeff

jefftyzzer wrote:
> Just amplify what Brian has said, let's say your table name is stored
> in a variable called V_TAB, you could do something like this:
>
> CREATE PROCEDURE JTYZZER.TTEST()
> DYNAMIC RESULT SETS 1
> LANGUAGE SQL
> SPECIFIC TTEST
> INHERIT SPECIAL REGISTERS
> BEGIN
> DECLARE V_SQL VARCHAR(64);--
> DECLARE V_TAB VARCHAR(128);--
> DECLARE C_SQL CURSOR WITH RETURN FOR S_SQL;--
> SET V_TAB = 'CODE';--
> SET V_SQL = 'SELECT COUNT(*) FROM '||V_TAB||' FOR READ ONLY';--
> PREPARE S_SQL FROM V_SQL;--
> OPEN C_SQL;--
> END;
>
> CALL JTYZZER.TTEST();
>
>
> Result set 1
> --------------
>
> 1
> -----------
> 13208
>
> 1 record(s) selected.
>
> Return Status = 0
>
> HTH,
>
> --Jeff
>
> Brian Tkatch wrote:
> > devx777@hotmail.com wrote:
> > > Hello,
> > >
> > > I am trying to find some information or an example on how to build a
> > > dynamic query in DB2 that would allow me to join a table which its name
> > > is stored as a field value on another table.
> > > I have done this in the past in SQL server, but DB2 is not as easy...
> > >
> > > Anyone out there that can help me?
> > >
> > > Your help will be much appreciated.
> > >
> > > Thanks.

> >
> > One way to do it is by DECLAREing a CURSOR but having the FOR refer to
> > a PREPAREd stament, gotten from a variable.
> >
> > For example:
> >
> > DECLARE List CURSOR WITH RETURN TO CLIENT FOR Query;
> > PREPARE Query FROM Text;
> > OPEN List;
> >
> > B.


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 02-27-2008, 07:55 AM
Brian Tkatch
 
Posts: n/a
Default Re: DB2 Dynamic SQL Table Name in Field Value


jefftyzzer wrote:
> Oops, one more thing: you mentioned "a table which its name
> is stored as a field value on another table." For that, you could
> assign the V_TAB variable from a scalar fullselect, like so:
>
> SET V_TAB = (SELECT x FROM y WHERE z);--
>


I think SELECT x INTO V_TAB FROM y WHERE Z, is the more standard way.

B.

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 02-27-2008, 07:55 AM
jefftyzzer
 
Posts: n/a
Default Re: DB2 Dynamic SQL Table Name in Field Value

I had read in the Janmohamed, et al., book that SET is more efficient,
but that may only be for multiple values. At any rate, both can be
used, and you may well be right that SELECT...INTO is more standard (I
haven't seen enough of others' SQL PL to get a feel for what's best
practice).

Regards,

--Jeff

Brian Tkatch wrote:
> jefftyzzer wrote:
> > Oops, one more thing: you mentioned "a table which its name
> > is stored as a field value on another table." For that, you could
> > assign the V_TAB variable from a scalar fullselect, like so:
> >
> > SET V_TAB = (SELECT x FROM y WHERE z);--
> >

>
> I think SELECT x INTO V_TAB FROM y WHERE Z, is the more standard way.
>
> B.


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 05:40 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com