SQL Server如何使用information_schema.columns或sys.tables从表列表中进行选择?

时间:2018-11-05 11:01:53

标签: sql sql-server tsql subquery

将Table的所有表作为行放入Table_List。

2018-11-05 14:38:46.842 DEBUG 32256 --- [           main] com.zaxxer.hikari.HikariConfig           : HikariPool-1 - configuration:
2018-11-05 14:38:46.917 DEBUG 32256 --- [           main] com.zaxxer.hikari.HikariConfig           : allowPoolSuspension.............false
2018-11-05 14:38:46.923 DEBUG 32256 --- [           main] com.zaxxer.hikari.HikariConfig           : autoCommit......................true
2018-11-05 14:38:46.928 DEBUG 32256 --- [           main] com.zaxxer.hikari.HikariConfig           : catalog.........................none
2018-11-05 14:38:46.932 DEBUG 32256 --- [           main] com.zaxxer.hikari.HikariConfig           : connectionInitSql...............none
2018-11-05 14:38:46.937 DEBUG 32256 --- [           main] com.zaxxer.hikari.HikariConfig           : connectionTestQuery.............none
2018-11-05 14:38:46.942 DEBUG 32256 --- [           main] com.zaxxer.hikari.HikariConfig           : connectionTimeout...............30000
2018-11-05 14:38:46.946 DEBUG 32256 --- [           main] com.zaxxer.hikari.HikariConfig           : dataSource......................none
2018-11-05 14:38:46.951 DEBUG 32256 --- [           main] com.zaxxer.hikari.HikariConfig           : dataSourceClassName.............none
2018-11-05 14:38:46.955 DEBUG 32256 --- [           main] com.zaxxer.hikari.HikariConfig           : dataSourceJNDI..................none
2018-11-05 14:38:46.967 DEBUG 32256 --- [           main] com.zaxxer.hikari.HikariConfig           : dataSourceProperties............{password=<masked>}
2018-11-05 14:38:46.973 DEBUG 32256 --- [           main] com.zaxxer.hikari.HikariConfig           : driverClassName................."org.postgresql.Driver"
2018-11-05 14:38:46.978 DEBUG 32256 --- [           main] com.zaxxer.hikari.HikariConfig           : healthCheckProperties...........{}
2018-11-05 14:38:46.983 DEBUG 32256 --- [           main] com.zaxxer.hikari.HikariConfig           : healthCheckRegistry.............none
2018-11-05 14:38:46.987 DEBUG 32256 --- [           main] com.zaxxer.hikari.HikariConfig           : idleTimeout.....................600000
2018-11-05 14:38:46.995 DEBUG 32256 --- [           main] com.zaxxer.hikari.HikariConfig           : initializationFailFast..........true
2018-11-05 14:38:47.002 DEBUG 32256 --- [           main] com.zaxxer.hikari.HikariConfig           : initializationFailTimeout.......1
2018-11-05 14:38:47.010 DEBUG 32256 --- [           main] com.zaxxer.hikari.HikariConfig           : isolateInternalQueries..........false
2018-11-05 14:38:47.017 DEBUG 32256 --- [           main] com.zaxxer.hikari.HikariConfig           : jdbc4ConnectionTest.............false
2018-11-05 14:38:47.022 DEBUG 32256 --- [           main] com.zaxxer.hikari.HikariConfig           : jdbcUrl.........................jdbc:postgresql://0.0.0.0:5432/docker
2018-11-05 14:38:47.027 DEBUG 32256 --- [           main] com.zaxxer.hikari.HikariConfig           : leakDetectionThreshold..........0
2018-11-05 14:38:47.032 DEBUG 32256 --- [           main] com.zaxxer.hikari.HikariConfig           : maxLifetime.....................1800000
2018-11-05 14:38:47.036 DEBUG 32256 --- [           main] com.zaxxer.hikari.HikariConfig           : maximumPoolSize.................10
2018-11-05 14:38:47.041 DEBUG 32256 --- [           main] com.zaxxer.hikari.HikariConfig           : metricRegistry..................none
2018-11-05 14:38:47.046 DEBUG 32256 --- [           main] com.zaxxer.hikari.HikariConfig           : metricsTrackerFactory...........com.zaxxer.hikari.metrics.micrometer.MicrometerMetricsTrackerFactory@1dd74143
2018-11-05 14:38:47.050 DEBUG 32256 --- [           main] com.zaxxer.hikari.HikariConfig           : minimumIdle.....................10
2018-11-05 14:38:47.054 DEBUG 32256 --- [           main] com.zaxxer.hikari.HikariConfig           : password........................<masked>
2018-11-05 14:38:47.059 DEBUG 32256 --- [           main] com.zaxxer.hikari.HikariConfig           : poolName........................"HikariPool-1"
2018-11-05 14:38:47.065 DEBUG 32256 --- [           main] com.zaxxer.hikari.HikariConfig           : readOnly........................false
2018-11-05 14:38:47.071 DEBUG 32256 --- [           main] com.zaxxer.hikari.HikariConfig           : registerMbeans..................false
2018-11-05 14:38:47.076 DEBUG 32256 --- [           main] com.zaxxer.hikari.HikariConfig           : scheduledExecutor...............none
2018-11-05 14:38:47.081 DEBUG 32256 --- [           main] com.zaxxer.hikari.HikariConfig           : scheduledExecutorService........internal
2018-11-05 14:38:47.085 DEBUG 32256 --- [           main] com.zaxxer.hikari.HikariConfig           : schema..........................none
2018-11-05 14:38:47.089 DEBUG 32256 --- [           main] com.zaxxer.hikari.HikariConfig           : threadFactory...................internal
2018-11-05 14:38:47.093 DEBUG 32256 --- [           main] com.zaxxer.hikari.HikariConfig           : transactionIsolation............default
2018-11-05 14:38:47.098 DEBUG 32256 --- [           main] com.zaxxer.hikari.HikariConfig           : username........................"docker"
2018-11-05 14:38:47.102 DEBUG 32256 --- [           main] com.zaxxer.hikari.HikariConfig           : validationTimeout...............5000
2018-11-05 14:38:47.105  INFO 32256 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...

从多个表中选择一个表

SELECT [table_name]
INTO Table_List
FROM INFORMATION_SCHEMA.TABLES

此查询无效。

我还尝试将行放入字符串(表名)列表中,然后使用FROM选择它们。但是效果不佳。

有人知道是否可以使用存储在表或列表中的SELECT [common column in each table ] FROM [Table_List] 吗?

我需要这样做是因为表的名称或每个月都在变化,因此用sys.table或INFORMATION_SCHEMA.TABLES关键字进行调用?

5 个答案:

答案 0 :(得分:0)

如果要获取具有公共列名的表的列表,则只需使用

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

如果您的要求不是这样,那么请举例说明您的要求。现在我还不太清楚。

答案 1 :(得分:0)

假设我理解这个问题,您想从该列所在的所有表中选择特定列的所有值。
为此,您需要使用动态SQL。
这是一种实现方法:

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)

这将返回具有Id列的所有表中的所有Id值,以及存在该表的表名。

请注意,它要求所有id列都具有相同的数据类型,或者至少具有可以隐式转换为彼此的数据类型。

答案 2 :(得分:0)

Zohar Beled,我尝试了您的方式,我觉得我越来越接近解决方案。 但是我遇到了一个新的错误: “找不到存储过程'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

答案 3 :(得分:0)

我也尝试使用此查询,首先将行放入逗号分隔的字符串中,然后在FROM语句中使用包含该字符串的新表。 但是它什么也没有。

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

select *
from (select column_string
      from table_string)

答案 4 :(得分:0)

由于在没有实际数据的情况下解决此问题太复杂了,我将尝试添加一些内容:

               |       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| ...

目标是提取N表中name ='John'的行。

我们已经有了一个名为[table_names]的表,其中n个表名存储在[column_table_name]列中。

现在我们想做这样的事情:

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

表名是动态的,因此在我们运行information_schema.tables查询之前是未知的。

最后的查询给我一个错误。 关于如何在子查询中使用多个存储表名称的任何线索?