472,342 Members | 1,298 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,342 software developers and data experts.

Provide table name dynamically in query

13,262 8TB
Hi Oracle experts,
I have a table called, say, OtherTables which stores IDs of other tables and their respective table names.

OtherTables(ID, tableID, tableName)

ID is the key of the OtherTables table.

I want to select from a table whose tableID is stored in the OtherTables table. I have the ID of the OtherTables row that contains this table that I want to select from.

I hope this is possible and that someone would be able to help get a solution in one query.

Thanks in advance.
Oct 26 '07 #1
10 10180
MMcCarthy
14,534 Expert Mod 8TB
I could do this in VBA and assume it could be similarly done in any application that would allow you to build a SQL string. Are you using any other application with Oracle in this case?

I'm not an expert in Oracle syntax and as such I'm not aware of any way to retrieve a table name from one query to use in the FROM part of another query.

Mary
Oct 26 '07 #2
eWish
971 Expert 512MB
I've not worked with Oracle. If I understand your intentions correctly this resource might be of help.
Oct 27 '07 #3
MMcCarthy
14,534 Expert Mod 8TB
Actually that will probably work. Forgot oracle had an All_Tables facility.
Oct 27 '07 #4
debasisdas
8,127 Expert 4TB
try to use

Expand|Select|Wrap|Line Numbers
  1. select TABLE_NAME from user_tables;
for the name of all the tables in hte current schema.

else this

Expand|Select|Wrap|Line Numbers
  1. select OWNER, TABLE_NAME from all_all_tables
for all the tables in the database.
Oct 27 '07 #5
r035198x
13,262 8TB
try to use

Expand|Select|Wrap|Line Numbers
  1. select TABLE_NAME from user_tables;
for the name of all the tables in hte current schema.

else this

Expand|Select|Wrap|Line Numbers
  1. select OWNER, TABLE_NAME from all_all_tables
for all the tables in the database.
First, thanks to everyone who's responded.
selecting from user_tables won't help me here. The OtherTables table is mine. I created it and it stores information about other tables.
I want to retrieve a table from a row in that OtherTables table and then do a select on that table that I will have retrieved from the OtherTables table all in one query.

Something like
Expand|Select|Wrap|Line Numbers
  1. select * from (Select tableName from OtherTables where ID = 10)
but this obviously simply returns one table name. I want to select the data in that table that is returned by the above query instead.

I hope I've made myself more clear ...
Oct 27 '07 #6
debasisdas
8,127 Expert 4TB
Try to use execute immediate in a PL/SQL block.

Also use CURSORS.
Oct 27 '07 #7
MMcCarthy
14,534 Expert Mod 8TB
Try something like ...

Expand|Select|Wrap|Line Numbers
  1. SELECT * from all_tables tabs                                         
  2. WHERE tabs.table_name IN
  3. (Select tableName from OtherTables where ID = 10)
  4.  
This is based on eWish's link
Oct 27 '07 #8
amitpatel66
2,367 Expert 2GB
Try something like ...

Expand|Select|Wrap|Line Numbers
  1. SELECT * from all_tables tabs                                         
  2. WHERE tabs.table_name IN
  3. (Select tableName from OtherTables where ID = 10)
  4.  
This is based on eWish's link
Mary, the above query would give information about table details ie tablespace used etc and not the data of that table.

Make us of dymamic sql (EXECUTE IMMEDIATE) to achieve the functionality as suggested in previous posts.
Oct 28 '07 #9
r035198x
13,262 8TB
Mary, the above query would give information about table details ie tablespace used etc and not the data of that table.

Make us of dymamic sql (EXECUTE IMMEDIATE) to achieve the functionality as suggested in previous posts.
I suppose there's no way of doing it in one query then? I'm trying to use this in a report in ireports and I don't think I can use PL/SQL blocks in ireports.
Oct 29 '07 #10
debasisdas
8,127 Expert 4TB
I suppose there's no way of doing it in one query then? I'm trying to use this in a report in ireports and I don't think I can use PL/SQL blocks in ireports.
Then try use the code inside a stored procedure and call it. You need to use ref cursor as the output type of the procedure.
Oct 29 '07 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Michael Hobbs | last post by:
Yeah, yeah, another X-Oriented paradigm, but please hear me out. I have recently been studying up on EJB's in order to extend my resume. I have...
1
by: PT | last post by:
I got a problem. And thats..... First of all, I got these three tables. ------------------- ------------------ ----------------------...
7
by: Rick Caborn | last post by:
Does anyone know of a way to execute sql code from a dynamically built text field? Before beginning, let me state that I know this db...
4
by: m_houllier | last post by:
STUDENT TABLE StudentReference Student Name etc ATTENDANCE TABLE AttendanceID CourseID StudentReference
3
by: ryanmhuc | last post by:
Is it possible to have a dynamic table name within a query or a table name that is a variable? This does not work but gives an example: SELECT *...
8
by: rshivaraman | last post by:
Hi : I have a TableA with around 10 columns with varchar and numeric datatypes It has 500 million records and its size is 999999999 KB. i...
1
Merlin1857
by: Merlin1857 | last post by:
How to search multiple fields using ASP A major issue for me when I first started writing in VB Script was constructing the ability to search a...
4
by: MGM | last post by:
Hello everyone, I once again have a problem :p I need to write a query to get the total amount of rows in a table. Problem is, the table name is...
3
dlite922
by: dlite922 | last post by:
I'm building a dynamic reporting system. The report can of course query multiple tables. The query declares which tables it will access, the...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...

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.