简体   繁体   中英

SQL Server how to select from a list of tables using information_schema.columns or sys.tables?

putting into Table_List all tables of the database as rows.

SELECT [table_name]
INTO Table_List
FROM INFORMATION_SCHEMA.TABLES

selecting from multiple tables with a table

SELECT [common column in each table ]
FROM [Table_List]

This query doesn't work.

I also tried with putting the rows into a list of strings (table names) and then using FROM to select them. But didn't work as well.

Does someone know if there is a way to use the SELECT * FROM tables names stored into a table or list?

I need this because the tables names or changing every month and are thus called with the sys.table or INFORMATION_SCHEMA.TABLES keywords?

if you want to fetch List of tables having common column name then you simply use

SELECT TABLE_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE column_name ='your_column_name'

If your requirement is not like that, then could you please give some example of your requirement. Right now i'm not much clear.

Assuming I understand the question, you want to select all the values of a specific column from all the tables where this column exists.
For that, you need to use dynamic SQL.
Here is one way to do it:

DECLARE @Sql nvarchar(4000) -- You might need nvarchar(max) here

SELECT @Sql = STUFF(
        (       
            SELECT ' UNION ALL SELECT Id, '''+ TABLE_NAME +''' As TableName FROM '+ TABLE_NAME
            FROM Information_schema.Columns
            WHERE Column_Name = 'ID'
            FOR XML PATH('')
        ), 1, 11, '')

EXEC(@Sql)

This will return all Id values from all tables where there is an Id column, along with the table name where it exists.

Please note that it requires that all the id columns will have the same data type, or at least data types that can be implicitly converted to each other.

Zohar Beled, I tried your way and I feel I am getting closer to the solution. But I got a new error: "Could not find stored procedure 'SELECT * FROM , AFU$, 'APP 3$', 'ASH $', BICHAT$, 'TE APHP$', VS$, CFS$, CIOD$, CMCP$, CNCF$, COLLOQUE$, EADV$"

DECLARE @tableList nvarchar(4000)
DECLARE @Mycommand nvarchar(4000)

SET @tableList = (SELECT (CAST(', ' + [table_name] AS VARCHAR(MAX)))
                  FROM [Table_List]  
                  FOR XML PATH (''))
SET @Mycommand = 'SELECT * FROM ' + @tableList
EXEC @Mycommand

I also tried with this query to first put the rows into a comma-separated string and then use the new table containing the string in the FROM statement. But it doesn't give anything.

select STRING_AGG(table_name, ', ') as column_string
into table_string
from Table_List

select *
from (select column_string
      from table_string)

Because it is too complicated to solve this problem without real data, I will try to add some:

               |       tables 1       |      table 2        |   ...   |     table n
---------------------------------------------------------------------------------------
columns_name:  |  name | B | C | D    |  name | B | C | D   |   ...   |    name | B | C | D
------------------------------------------------------------------------------------------
column_content:|  John | ...          |  Ben  | ...         |   ...   |    John| ...

The objective is to extract the rows in the N tables where name = 'John'.

Where we already have a table called [table_names] with the n tables names stored in the column [column_table_name].

Now we want to do something like that:

SELECT [name]
FROM (SELECT [table_name]
      FROM INFORMATION_SCHEMA.TABLES)
WHERE [name] = 'Jonh'

Tables names are dynamic and thus unknown until we run the information_schema.tables query.

This final query is giving me an error. Any clue about how to use multiple stored tables names in a subquery?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM