SQL Recipes (Beta II)
A FREE cookbook for SQL queries and examples
Register FAQ Search Today's Posts Mark Forums Read

SQL - Questions and Answers Have a SQL question? Post it here. First do a search to see if someone hasn't already answered it.

Go Back   SQL Recipes a FREE cookbook of SQL queries and examples > SQL queries and examples > SQL - Questions and Answers

Reply
 
LinkBack Thread Tools Search this Thread Rate Thread
  #1 (permalink)  
Old 05-31-2006, 07:26 PM
Christian
 
Posts: n/a
Default Oracle dialect question:

How to pull data from several tables that follows a pattern?


How do I pull data from several tables into one, where the table name follow a certain pattern:

EX_001
EX_002
EX_003
..
..
EX_N

Where new tables can be added continually in the same pattern - and this data also should be included in the SQL?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Edit/Delete Message Reply With Quote

ANSWER(S):

  #2 (permalink)  
Old 06-02-2006, 08:28 AM
Dimitar
 
Posts: n/a
Default Oracle answer. Re: How to pull data from several tables that follows a pattern?

Oracle provides Dynamic SQL facilities that would be the basis for a solution.
The main points:
1. You create a "for ... loop ... end loop" to iterate over the table names that follow the pattern.
2. Inside the loop (for each table) you append a sql "select ..." text to form a union of the data in all tables.
3. At the end you execute the constructed sql text.

There can be variations of this solution. You could use the "execute immediate .." construct or the dbms_sql facility.
Or, instead of concatenating, inside the loop you could actually execute the single-table sql.

Hope this helps.

P.S.
FOR t IN (SELECT object_name FROM user_objects WHERE object_type='TABLE' AND object_name LIKE 'ex_%')
loop
 ...
end loop
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Edit/Delete Message Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may post new threads
You may post replies
You may not post attachments
You may 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

Moderation Tools:



All times are GMT. The time now is 10:47 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0
Copyright (c) 2006-2007 SQL Recipes

1 2 3 4 5 6 7