Using a subquery instead of tablename

Table Meta:
-------------------------------------
type                  tab_name
new                   tab_news
sports                tab_sps

Table tab_news
------
id

Table tab_sps
-------------------
id
xx

Now i want to use

SELECT id 
  FROM (SELECT tab_name 
          FROM Meta 
         WHERE type = 'news');

But it doesn’t work, any ideas?

+3
source share
4 answers

SQL does not support the / etc variable for the table name - the only way to support what you are asking is using dynamic SQL:

FOR i IN (SELECT tab_name
            FROM META m
           WHERE m.type = ?) LOOP
  EXECUTE IMMEDIATE 'SELECT * FROM '|| i.tab_name ||'';
END LOOP;
+2
source

The syntax structure you are trying to use does not do what you want. What appears in the FROM clause is a data set. It can be a table or view. In your case, the data set is a subset of Meta; in particular, the column "tab_name" for rows with the type "news".

SELECT id 
  FROM (SELECT tab_name 
          FROM Meta 
         WHERE type = 'news');

SQL . , , "tab_name" "" . - . table_, Meta , .

SELECT tab_name.getId()
FROM Meta
Where type = 'news';

"-" , , .

+2

, , , . , , SQL . SQL, , , .

, . . , , , , . . , , . , . , .

0

.

select * from  (select tab_name from Meta where type='news') as my_sub_query;
-1

All Articles