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

ε祈祈猫儿з 提交于 2019-12-13 02:36:11

问题


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?


回答1:


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.




回答2:


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?




回答3:


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.




回答4:


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



回答5:


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)


来源:https://stackoverflow.com/questions/53153094/sql-server-how-to-select-from-a-list-of-tables-using-information-schema-columns

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!