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 > Data Access, Manipulation & Batch Languages > ANSI SQL > Select-ing from variable number of tables

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-17-05, 12:33
teterin teterin is offline
Registered User
 
Join Date: Apr 2003
Posts: 62
Select-ing from variable number of tables

Hello!

I'd like to select the maximum value of a certain column (let's call it `X_IDNTY'), from every table, that has such a column. Is such a thing possible with SQL (perhaps, Sybase and/or Oracle flavors?), or do I need to write external program to get the list of tables first and construct the query outside?

Something like:

TABLE_NAME max(X_IDNTY)
------------ --------------
................. ...........

Thanks!
Reply With Quote
  #2 (permalink)  
Old 10-17-05, 14:28
Littlefoot Littlefoot is offline
Lost Boy
 
Join Date: Jan 2004
Location: Croatia, Europe
Posts: 3,629
In Oracle, you might use dynamic SQL, such as in this PL/SQL procedure:
Code:
SET SERVEROUTPUT ON SIZE 1000000;

DECLARE
   CURSOR c1
   IS
      SELECT table_name
        FROM user_tab_columns
       WHERE column_name = 'VT';

   l_var    VARCHAR2 (1024);
   max_vt   ocitanje.vt%TYPE;
BEGIN
   FOR c1r IN c1
   LOOP
      l_var := 'SELECT MAX(vt) FROM ' || c1r.table_name;

      EXECUTE IMMEDIATE l_var INTO max_vt;

      DBMS_OUTPUT.put_line (c1r.table_name || ': ' || max_vt);
   END LOOP;
END;
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