Home » SQL & PL/SQL » SQL & PL/SQL » single query by which data can fetched from table which name generated from a query (oracle 10 g)
single query by which data can fetched from table which name generated from a query [message #615013] Thu, 29 May 2014 08:40 Go to next message
evivekj
Messages: 13
Registered: May 2014
Location: india
Junior Member
Hi,

Need Help in writing a single query by which data can fetched from a table name which we get from a another query
ex :
SELECT object_name
FROM all_objects
WHERE object_type='TABLE'
AND owner='ABC' ;

let ABC is the user who have only one table and in my case each user have only one table and each tables for diffrent user
will have same primary key column empid

ex:
SELECT empid FROM tab_abc

So how do can i can find diffrent empolyee id data for differnt owner input


thank you
Re: single query by which data can fetched from table which name generated from a query [message #615015 is a reply to message #615013] Thu, 29 May 2014 08:43 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
welcome to this forum

Please read and follow the forum guidelines, to enable us to help you:

http://www.orafaq.com/forum/t/88153/0/ and please read http://www.orafaq.com/forum/t/174502/102589/

>So how do can i can find diffrent empolyee id data for differnt owner input
how do you know the schema name for any of the other users?
Re: single query by which data can fetched from table which name generated from a query [message #615020 is a reply to message #615013] Thu, 29 May 2014 09:10 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
Different query for different tables with UNION ALL.
Re: single query by which data can fetched from table which name generated from a query [message #615056 is a reply to message #615020] Thu, 29 May 2014 15:32 Go to previous messageGo to next message
evivekj
Messages: 13
Registered: May 2014
Location: india
Junior Member
LET there is a OWNER 'ABC' and have table with column empid do this query will give me empid values for owner 'ABC' ?

SELECT empid FROM (SELECT object_name FROM all_objects WHERE object_type='TABLE' AND owner='ABC') ;
Re: single query by which data can fetched from table which name generated from a query [message #615059 is a reply to message #615056] Thu, 29 May 2014 15:59 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
just run the SQL yourself to see the results.
Don't ask us what happens with your tables, data, & code.
Re: single query by which data can fetched from table which name generated from a query [message #615098 is a reply to message #615059] Fri, 30 May 2014 03:53 Go to previous messageGo to next message
evivekj
Messages: 13
Registered: May 2014
Location: india
Junior Member
on running this query this is give error since it oracle interprates it as subquery and try to filter out data from sub query but my requirement is
this subqury return table name and from that table i want to select empid which is not happening over here so is there any other way to implement this
in a single query
Re: single query by which data can fetched from table which name generated from a query [message #615100 is a reply to message #615098] Fri, 30 May 2014 04:10 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
No you can't do it in a single query, you would have to use dynamic SQL.
Re: single query by which data can fetched from table which name generated from a query [message #615104 is a reply to message #615100] Fri, 30 May 2014 04:58 Go to previous messageGo to next message
evivekj
Messages: 13
Registered: May 2014
Location: india
Junior Member
it not seems to be possible with dynamic sql also in a single query to exceute
Re: single query by which data can fetched from table which name generated from a query [message #615105 is a reply to message #615104] Fri, 30 May 2014 05:02 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
Dynamic sql is in plsql. You need to loop through all the table names and use execute immediate. But I find all this silly. All youneed to do is get the table names and use different queries for each. Use union all to combine them together.
Re: single query by which data can fetched from table which name generated from a query [message #615121 is a reply to message #615100] Fri, 30 May 2014 07:37 Go to previous messageGo to next message
Solomon Yakobson
Messages: 3273
Registered: January 2010
Location: Connecticut, USA
Senior Member
cookiemonster wrote on Fri, 30 May 2014 05:10
No you can't do it in a single query, you would have to use dynamic SQL.


Yes, you can (assuming table has no LONG columns - there is a bug in XML with LONGs):

SELECT  owner,
        table_name,
        empno
  from  all_tab_columns,
        xmltable(
                 '/ROW/EMPNO'
                 passing xmlquery(('ora:view("' || table_name || '")') returning content)
                 columns
                   empno number path '.'
                )
  where column_name = 'EMPNO'
    and owner = 'SCOTT'
  order by owner,
           table_name
/

OWNER                          TABLE_NAME                          EMPNO
------------------------------ ------------------------------ ----------
SCOTT                          EMP                                  7369
SCOTT                          EMP                                  7499
SCOTT                          EMP                                  7521
SCOTT                          EMP                                  7566
SCOTT                          EMP                                  7654
SCOTT                          EMP                                  7698
SCOTT                          EMP                                  7782
SCOTT                          EMP                                  7788
SCOTT                          EMP                                  7839
SCOTT                          EMP                                  7844
SCOTT                          EMP                                  7876

OWNER                          TABLE_NAME                          EMPNO
------------------------------ ------------------------------ ----------
SCOTT                          EMP                                  7900
SCOTT                          EMP                                  7902
SCOTT                          EMP                                  7934
SCOTT                          EMP_INFO                              101

15 rows selected.

SQL> 


SY.

[Updated on: Fri, 30 May 2014 07:41]

Report message to a moderator

Re: single query by which data can fetched from table which name generated from a query [message #615141 is a reply to message #615121] Fri, 30 May 2014 10:41 Go to previous messageGo to next message
Michel Cadot
Messages: 68647
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

This does not work in 10g (OP's version).
But you can use:
SQL> col empno format 99999
SQL> select owner, table_name, to_number(extractvalue(value(x),'/EMPNO')) empno
  2  from all_tab_columns,
  3       table(xmlsequence(extract(dbms_xmlgen.getxmltype('select empno from '||owner||'.'||table_name),
  4                         '/ROWSET/ROW/EMPNO'))) x
  5  where column_name = 'EMPNO'
  6    and owner = 'SCOTT'
  7  order by owner, table_name
  8  /
OWNER                          TABLE_NAME                      EMPNO
------------------------------ ------------------------------ ------
SCOTT                          EMP                              7369
SCOTT                          EMP                              7499
SCOTT                          EMP                              7521
SCOTT                          EMP                              7566
SCOTT                          EMP                              7654
SCOTT                          EMP                              7698
SCOTT                          EMP                              7934
SCOTT                          EMP                              7788
SCOTT                          EMP                              7839
SCOTT                          EMP                              7844
SCOTT                          EMP                              7876
SCOTT                          EMP                              7900
SCOTT                          EMP                              7902
SCOTT                          EMP                              7782


Re: single query by which data can fetched from table which name generated from a query [message #615144 is a reply to message #615141] Fri, 30 May 2014 10:59 Go to previous messageGo to next message
Solomon Yakobson
Messages: 3273
Registered: January 2010
Location: Connecticut, USA
Senior Member
In 10G we could still use xmltable just substitute ora:view with dbms_xmlgen:

SQL> select  *
  2    from  v$version
  3  /

BANNER
----------------------------------------------------------------
Oracle Database 10g Enterprise Edition Release 10.2.0.5.0 - 64bi
PL/SQL Release 10.2.0.5.0 - Production
CORE    10.2.0.5.0      Production
TNS for 64-bit Windows: Version 10.2.0.5.0 - Production
NLSRTL Version 10.2.0.5.0 - Production

SQL> SELECT  owner,
  2          table_name,
  3          empno
  4    from  all_tab_columns,
  5          xmltable(
  6                   '/ROWSET/ROW/EMPNO'
  7                   passing dbms_xmlgen.getxmltype('select empno from '||owner||'.'||table_name)
  8                   columns
  9                     empno number path '.'
 10                  )
 11    where column_name = 'EMPNO'
 12      and owner = 'SCOTT'
 13    order by owner,
 14             table_name
 15  /

OWNER                          TABLE_NAME                          EMPNO
------------------------------ ------------------------------ ----------
SCOTT                          EMP                                  7369
SCOTT                          EMP                                  7499
SCOTT                          EMP                                  7521
SCOTT                          EMP                                  7566
SCOTT                          EMP                                  7654
SCOTT                          EMP                                  7698
SCOTT                          EMP                                  7934
SCOTT                          EMP                                  7788
SCOTT                          EMP                                  7839
SCOTT                          EMP                                  7844
SCOTT                          EMP                                  7876

OWNER                          TABLE_NAME                          EMPNO
------------------------------ ------------------------------ ----------
SCOTT                          EMP                                  7900
SCOTT                          EMP                                  7902
SCOTT                          EMP                                  7782

14 rows selected.

SQL> 


SY.
Re: single query by which data can fetched from table which name generated from a query [message #615637 is a reply to message #615144] Fri, 06 June 2014 06:00 Go to previous messageGo to next message
evivekj
Messages: 13
Registered: May 2014
Location: india
Junior Member
thanks guys for your reply but on exceuting this getting error
----------------------------------------------------------
empno number path '.'
*
ERROR at line 9:
ORA-01780: string literal required
----------------------------------------------
Re: single query by which data can fetched from table which name generated from a query [message #615658 is a reply to message #615637] Fri, 06 June 2014 09:47 Go to previous message
Michel Cadot
Messages: 68647
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Please post your version number and complete query and execution as we did (especially Solomon).

Previous Topic: Cannot access table in PL/SQL even with direct grant
Next Topic: Converting SQL syntax
Goto Forum:
  


Current Time: Fri Apr 26 14:18:51 CDT 2024