Same query Different tables [message #100592] |
Fri, 28 February 2003 07:06 |
Praveen
Messages: 57 Registered: November 2001
|
Member |
|
|
I have a SQL select query with certain complicated join conditions which i have to execute on different tables. Plz inform if i can do it in one generalized query ??
To be more clear...
select * from tabname where tabname = 'some variable' did not work...and gave error "table tabname does not exist"!
How do i do it ? Is there no other way other than writing same query again and again, each time changing just the table-name ??
Plz help...clearing this roadblock is very important for the project
|
|
|
Re: Same query Different tables [message #100593 is a reply to message #100592] |
Fri, 28 February 2003 09:07 |
|
Mahesh Rajendran
Messages: 10708 Registered: March 2002 Location: oracleDocoVille
|
Senior Member Account Moderator |
|
|
----------------------------------------------------
-- with just sql.
-- you can save the file as a .sql and execute it..
----------------------------------------------------
mag@itloaner1_local > select count(*) from &table_name;
Enter value for table_name: emp
old 1: select count(*) from &table_name
new 1: select count(*) from emp
COUNT(*)
----------
12
mag@itloaner1_local > /
Enter value for table_name: dept
old 1: select count(*) from &table_name
new 1: select count(*) from dept
COUNT(*)
----------
4
----------------------------------------------------------------------
----------
-- using pl/sql
----------
mag@itloaner1_local > get sp
1 create or replace procedure count_tab (tname in all_tables.table_name%type)
2 is
3 type rc is ref cursor;
4 my_rc rc;
5 cnt number;
6 begin open my_rc for 'select count(*) from ' || tname;
7 loop
8 fetch my_rc into cnt;
9 exit when my_rc%notfound;
10 end loop;
11 close my_rc;
12 dbms_output.put_line('count of records in table '||tname||' : '||cnt);
13* end;
mag@itloaner1_local > /
Procedure created.
mag@itloaner1_local > exec count_tab('EMP');
count of records in table EMP : 12
PL/SQL procedure successfully completed.
mag@itloaner1_local > EXEC count_tab('DEPT');
count of records in table DEPT : 4
PL/SQL procedure successfully completed.
mag@itloaner1_local >
|
|
|