473,396 Members | 1,738 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,396 software developers and data experts.

Db2 select statment

Hello,
I'm looking for a SQL syntax to put a variable name into the from clause.
Specifically I have a colume in a table that is table name, and I want
to use that result in the from clause in a select subquery
ideally I'd like:

-----
select coid,coid_ref from physical.ext ext where coid_ref not in (select
coid from ext.coname)
-----

ext.coname being a column in table physical.ext and its value varies but
is always a table name (like physical.part_list)

Is there a way to that in db2?
I'm not sure I can't really remember, but I believe I've done this
oracle and it worked.

Thanks in advance for any help..

Ken
Nov 12 '05 #1
5 3311
73blazer wrote:
Hello,
I'm looking for a SQL syntax to put a variable name into the from clause.
Specifically I have a colume in a table that is table name, and I want
to use that result in the from clause in a select subquery
ideally I'd like:

-----
select coid,coid_ref from physical.ext ext where coid_ref not in (select
coid from ext.coname)
-----

ext.coname being a column in table physical.ext and its value varies but
is always a table name (like physical.part_list)

Is there a way to that in db2?
I'm not sure I can't really remember, but I believe I've done this
oracle and it worked.

Thanks in advance for any help..

Ken

SQL is not LISP...
You will need to retrieve the table name and then use
PREPARE a dynamic cursor which is composed by glueing the tablename into
the SELECT using concat...

DECLARE tabname VARCHAR(128);
DECLARE stmttxt VARCHAR(1000);
DECLARE stmt STATEMENT;
DECLARE cur CURSOR FOR stmt;
SELECT tname INTO tabname
FROM ....;
SET stmttxt = 'SELECT .....FROM ' || tabname;
PREPARE stmt FROM stmtxt; -- double check syntax (!)
OPEN cur;
.....

Cheers
Serge
--
Serge Rielau
DB2 SQL Compiler Development
IBM Toronto Lab
Nov 12 '05 #2
Serge Rielau wrote:
73blazer wrote:
Hello,
I'm looking for a SQL syntax to put a variable name into the from clause.
Specifically I have a colume in a table that is table name, and I want
to use that result in the from clause in a select subquery
ideally I'd like:

-----
select coid,coid_ref from physical.ext ext where coid_ref not in
(select coid from ext.coname)
-----

ext.coname being a column in table physical.ext and its value varies
but is always a table name (like physical.part_list)

Is there a way to that in db2?
I'm not sure I can't really remember, but I believe I've done this
oracle and it worked.

Thanks in advance for any help..

Ken


SQL is not LISP...
You will need to retrieve the table name and then use
PREPARE a dynamic cursor which is composed by glueing the tablename into
the SELECT using concat...

DECLARE tabname VARCHAR(128);
DECLARE stmttxt VARCHAR(1000);
DECLARE stmt STATEMENT;
DECLARE cur CURSOR FOR stmt;
SELECT tname INTO tabname
FROM ....;
SET stmttxt = 'SELECT .....FROM ' || tabname;
PREPARE stmt FROM stmtxt; -- double check syntax (!)
OPEN cur;
....

Cheers
Serge

Thanks for that. This does work. So at least I have a method and I'm
appreciative.
However it is not so fast. It seems essetially it has to run a select
for every entry in the one table. Is there any other possible way, with
JOIN or UNION perhaps? I have a P5 machine, 4-way 16gb ram, DB2 8.2, AIC
5.3ml01, there's ~153000 entries in that table and it takes about 18
minutes to run. Which is fine I guess, I only need to run this once a
day at night. But it seems it could be faster.
I did some LISP years ago, perhaps that's where I got the idea my query
would work!
Nov 12 '05 #3
73blazer wrote:
Serge Rielau wrote:
73blazer wrote:
Hello,
I'm looking for a SQL syntax to put a variable name into the from
clause.
Specifically I have a colume in a table that is table name, and I
want to use that result in the from clause in a select subquery
ideally I'd like:

-----
select coid,coid_ref from physical.ext ext where coid_ref not in
(select coid from ext.coname)
-----

ext.coname being a column in table physical.ext and its value varies
but is always a table name (like physical.part_list)

Is there a way to that in db2?
I'm not sure I can't really remember, but I believe I've done this
oracle and it worked.

Thanks in advance for any help..

Ken

SQL is not LISP...
You will need to retrieve the table name and then use
PREPARE a dynamic cursor which is composed by glueing the tablename
into the SELECT using concat...

DECLARE tabname VARCHAR(128);
DECLARE stmttxt VARCHAR(1000);
DECLARE stmt STATEMENT;
DECLARE cur CURSOR FOR stmt;
SELECT tname INTO tabname
FROM ....;
SET stmttxt = 'SELECT .....FROM ' || tabname;
PREPARE stmt FROM stmtxt; -- double check syntax (!)
OPEN cur;
....

Cheers
Serge

Thanks for that. This does work. So at least I have a method and I'm
appreciative.
However it is not so fast. It seems essetially it has to run a select
for every entry in the one table. Is there any other possible way, with
JOIN or UNION perhaps? I have a P5 machine, 4-way 16gb ram, DB2 8.2, AIC
5.3ml01, there's ~153000 entries in that table and it takes about 18
minutes to run. Which is fine I guess, I only need to run this once a
day at night. But it seems it could be faster.
I did some LISP years ago, perhaps that's where I got the idea my query
would work!

Well, you could batch them uop maybe a few dozen at a time and combine
the individual queries with a UNION ALL you construct. I won't dare say
whether that will actually be faster....
What is the information you are retrieving? Is this metadata to be used
in some profile, monitoring?

Cheers
Serge

--
Serge Rielau
DB2 SQL Compiler Development
IBM Toronto Lab
Nov 12 '05 #4
Serge Rielau wrote:
73blazer wrote:
Serge Rielau wrote:
73blazer wrote:

Hello,
I'm looking for a SQL syntax to put a variable name into the from
clause.
Specifically I have a colume in a table that is table name, and I
want to use that result in the from clause in a select subquery
ideally I'd like:

-----
select coid,coid_ref from physical.ext ext where coid_ref not in
(select coid from ext.coname)
-----

ext.coname being a column in table physical.ext and its value varies
but is always a table name (like physical.part_list)

Is there a way to that in db2?
I'm not sure I can't really remember, but I believe I've done this
oracle and it worked.

Thanks in advance for any help..

Ken

SQL is not LISP...
You will need to retrieve the table name and then use
PREPARE a dynamic cursor which is composed by glueing the tablename
into the SELECT using concat...

DECLARE tabname VARCHAR(128);
DECLARE stmttxt VARCHAR(1000);
DECLARE stmt STATEMENT;
DECLARE cur CURSOR FOR stmt;
SELECT tname INTO tabname
FROM ....;
SET stmttxt = 'SELECT .....FROM ' || tabname;
PREPARE stmt FROM stmtxt; -- double check syntax (!)
OPEN cur;
....

Cheers
Serge

Thanks for that. This does work. So at least I have a method and I'm
appreciative.
However it is not so fast. It seems essetially it has to run a select
for every entry in the one table. Is there any other possible way,
with JOIN or UNION perhaps? I have a P5 machine, 4-way 16gb ram, DB2
8.2, AIC 5.3ml01, there's ~153000 entries in that table and it takes
about 18 minutes to run. Which is fine I guess, I only need to run
this once a day at night. But it seems it could be faster.
I did some LISP years ago, perhaps that's where I got the idea my
query would work!


Well, you could batch them uop maybe a few dozen at a time and combine
the individual queries with a UNION ALL you construct. I won't dare say
whether that will actually be faster....
What is the information you are retrieving? Is this metadata to be used
in some profile, monitoring?

Cheers
Serge

It's actually not a query, but a delete. I want to delete these entries.
The (somewhat poorly coded) application using this database leaves these
(what we call) dead links around when you delete things through the
application, the dead links then cause problems elsewhere in the same
application(great application). We submit bug to the application maker,
but they won't fix unless we can describe to them exactly how the dead
links get created, and we just can't reproduce on demand. All we know is
at the end of the day, there a few in there. (they KNOW it leaves dead
links, they are just hiding behind a technicality)

Regards,
Ken
Nov 12 '05 #5
73blazer wrote:
Serge Rielau wrote:
73blazer wrote:
Serge Rielau wrote:

73blazer wrote:

> Hello,
> I'm looking for a SQL syntax to put a variable name into the from
> clause.
> Specifically I have a colume in a table that is table name, and I
> want to use that result in the from clause in a select subquery
> ideally I'd like:
>
> -----
> select coid,coid_ref from physical.ext ext where coid_ref not in
> (select coid from ext.coname)
> -----
>
>
>
> ext.coname being a column in table physical.ext and its value
> varies but is always a table name (like physical.part_list)
>
> Is there a way to that in db2?
> I'm not sure I can't really remember, but I believe I've done this
> oracle and it worked.
>
> Thanks in advance for any help..
>
> Ken


SQL is not LISP...
You will need to retrieve the table name and then use
PREPARE a dynamic cursor which is composed by glueing the tablename
into the SELECT using concat...

DECLARE tabname VARCHAR(128);
DECLARE stmttxt VARCHAR(1000);
DECLARE stmt STATEMENT;
DECLARE cur CURSOR FOR stmt;
SELECT tname INTO tabname
FROM ....;
SET stmttxt = 'SELECT .....FROM ' || tabname;
PREPARE stmt FROM stmtxt; -- double check syntax (!)
OPEN cur;
....

Cheers
Serge
Thanks for that. This does work. So at least I have a method and I'm
appreciative.
However it is not so fast. It seems essetially it has to run a select
for every entry in the one table. Is there any other possible way,
with JOIN or UNION perhaps? I have a P5 machine, 4-way 16gb ram, DB2
8.2, AIC 5.3ml01, there's ~153000 entries in that table and it takes
about 18 minutes to run. Which is fine I guess, I only need to run
this once a day at night. But it seems it could be faster.
I did some LISP years ago, perhaps that's where I got the idea my
query would work!

Well, you could batch them uop maybe a few dozen at a time and combine
the individual queries with a UNION ALL you construct. I won't dare
say whether that will actually be faster....
What is the information you are retrieving? Is this metadata to be
used in some profile, monitoring?

Cheers
Serge

It's actually not a query, but a delete. I want to delete these entries.
The (somewhat poorly coded) application using this database leaves these
(what we call) dead links around when you delete things through the
application, the dead links then cause problems elsewhere in the same
application(great application). We submit bug to the application maker,
but they won't fix unless we can describe to them exactly how the dead
links get created, and we just can't reproduce on demand. All we know is
at the end of the day, there a few in there. (they KNOW it leaves dead
links, they are just hiding behind a technicality)

Regards,
Ken

In this case I would stick with what you have.
Throw in COMMIT between the individual DELETEs and let it plod along for
however long it takes (remember to use a cursor WITH HOLD to find the
table names).

Cheers
Serge
--
Serge Rielau
DB2 SQL Compiler Development
IBM Toronto Lab
Nov 12 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: e_AK_05 | last post by:
I have a question and I can't figure out how to do it. I have a select statment : SELECT table1.* AS table1.*, table2.* AS table2.* FROM table1, table2 WHERE 1 this does not work...for me at...
6
by: RCS | last post by:
I've been running into more and more complexity with an application, because as time goes on - we need more and more high-level, rolled-up information. And so I've created views, and views that use...
1
by: Joe Saliba | last post by:
Hi, would like to know how to write a crosstable select statment in sql server2000 where having: - ItemNumber, ItemDescription, ItemColor, ItemSize as rows - Stores as columns - Qty * Netttc...
3
by: Tcs | last post by:
My backend is DB2 on our AS/400. While I do HAVE DB2 PE for my PC, I haven't loaded it yet. I'm still using MS Access. And no, I don't believe this is an Access question. (But who knows? I...
5
by: orencs | last post by:
Hello, I am using Microsoft.Practices.EnterpriseLibrary.Data. I am running the following sqlCommand = "SELECT var1 FROM table1 WHERE var2 IN (4,5,6) ; SELECT var3 FROM table2 WHERE var2 IN...
1
by: Anton Nikiforov | last post by:
Dear All, could you please help me with writing select statment for the following: I have two tables dictionary_text label| Text -----+------------------------ 23 | General 24 | Internet...
6
by: mabond | last post by:
Hi Is it possible to compare a vaule for a select case statement using a wildcard. e.g. somthing like Select case myValue case like "*ing" end select
1
by: zeebiggie | last post by:
Good morning I have a form with the controls in the insert statment below. table1 has an Auto increment primary key hence is omitted in the insert statment and form. Am getting the error It didnt...
2
by: gimme_this_gimme_that | last post by:
I use the following SQL statment to bring z_emp_id values to a employee table: update employee set z_emp_id = (select z.emp_id from z.employee z where z.login=employee.login) Upon executing...
8
by: Trevor2007 | last post by:
I am trying to hard code the following select query into a select case statement ie (case1 <statment>, case 2 <statment>) but I getteing Compiler error: expected line number or label, or...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.