简体   繁体   English

从具有特定表名的所有表中选择所有值

[英]Select all values from all tables with specific table name

EDIT original question: 编辑原始问题:

Our UDW is broken out into attribute and attribute list tables. 我们的UDW分为属性表和属性列表。

I would like to write a data dictionary query that dynamically pulls in all column values from all tables that are like %attr_list% without having to write a series of unions and update or add every time a new attribute list is created in our UDW. 我想编写一个数据字典查询,该查询可以动态地从%attr_list%类的所有表中提取所有列值,而不必编写一系列的并集,并且每次在UDW中创建新的属性列表时都进行更新或添加。

All of our existing attribute list tables follow the same format (number of columns, most column names, etc). 我们所有现有的属性列表表都采用相同的格式(列数,大多数列名等)。 Below is the first two unions in our existing view which I want to avoid updating each time a new attribute list table is added to our UDW. 下面是我们现有视图中的前两个联合,我希望避免在每次将新属性列表添加到UDW时进行更新。

CREATE VIEW [dbo].[V_BI_DATA_DICTIONARY]
(      ATTR_TABLE
  ,ATTR_LIST_ID
  ,ATTR_NAME
  ,ATTR_FORMAT
  ,SHORT_DESCR
  ,LONG_DESCR
  ,SOURCE_DATABASE
  ,SOURCE_TABLE
  ,SOURCE_COLUMN
  ,INSERT_DATETIME
  ,INSERT_OPRID
   )
AS

SELECT 'PREAUTH_ATTR_LIST'             ATTR_TABLE
  ,[PREAUTH_ATTR_LIST_ID]          ATTR_LIST_ID
  ,[ATTR_NAME]                     ATTR_NAME
  ,[ATTR_FORMAT]                   ATTR_FORMAT
  ,[SHORT_DESCR]                   SHORT_DESCR
  ,[LONG_DESCR]                    LONG_DESCR
  ,[SOURCE_DATABASE]               SOURCE_DATABASE
  ,[SOURCE_TABLE]                  SOURCE_TABLE
  ,[SOURCE_COLUMN]                 SOURCE_COLUMN
  ,[INSERT_DATETIME]               INSERT_DATETIME
  ,[INSERT_OPRID]                  INSERT_OPRID
FROM [My_Server].[MY_DB].[dbo].[PREAUTH_ATTR_LIST]

UNION
SELECT 'SAVINGS_ACCOUNT_ATTR_LIST'     
  ,[SAVINGS_ACCOUNT_ATTR_LIST_ID]  
  ,[ATTR_NAME]                     
  ,[ATTR_FORMAT]                   
  ,[SHORT_DESCR]                   
  ,[LONG_DESCR]                    
  ,[SOURCE_DATABASE]
  ,[SOURCE_TABLE]
  ,[SOURCE_COLUMN]
  ,[INSERT_DATETIME]               
  ,[INSERT_OPRID]                  
FROM [My_Server].[MY_DB].[dbo].[SAVINGS_ACCOUNT_ATTR_LIST]'

For SQL Server you should be able to use something like this: 对于SQL Server,您应该可以使用以下命令:

SELECT c.name AS ColName, t.name AS TableName
FROM sys.columns c
    JOIN sys.tables t ON c.object_id = t.object_id
WHERE t.name LIKE '%attr_list%'

And this will include views as well as tables 这将包括视图和表格

SELECT COLUMN_NAME, TABLE_NAME 
FROM INFORMATION_SCHEMA.COLUMNS 
WHERE TABLE_NAME LIKE '%attr_list%'

Something like this might work for you if all tables contain the same columns. 如果所有表都包含相同的列,则可能对您有用。 Just change the temp table and the selected columns to match your own columns. 只需更改临时表和所选列以匹配您自己的列即可。

CREATE TABLE #results (
    ATTR_TABLE SYSNAME,
    ATTR_LIST_ID INT,
    ATTR_NAME NVARCHAR(50),
    ATTR_FORMAT NVARCHAR(50),
    SHORT_DESCR NVARCHAR(50),
    LONG_DESCR NVARCHAR(255),
    SOURCE_DATABASE NVARCHAR(50),
    SOURCE_TABLE NVARCHAR(50),
    SOURCE_COLUMN NVARCHAR(50),
    INSERT_DATETIME DATETIME,
    INSERT_OPRID INT
);

INSERT INTO #results
EXEC sp_MSforeachtable @command1 = 
    ' 
        SELECT ''?''
             , *
        FROM ? 
        WHERE ''?'' LIKE ''%ATTR_LIST%''
    '

SELECT *
FROM #results

DROP TABLE #results

EDIT: Updated my example with your columns. 编辑:用您的专栏更新了我的示例。 Because you use different column name for ATTR_LIST_ID in each table I changed the select to SELECT * . 因为您在每个表中为ATTR_LIST_ID使用了不同的列名, ATTR_LIST_ID我将选择更改为SELECT * Obviously I don't know the data types of your columns so you have to change them. 显然,我不知道列的数据类型,因此您必须更改它们。

This won't work in a view but you could create a stored procedure. 这在视图中不起作用,但是您可以创建一个存储过程。

If using MS SQL Server check out the sys catalog views . 如果使用MS SQL Server,请查看sys目录视图 You can use sys.tables and join to sys.columns to get your tables and columns. 您可以使用sys.tables并加入sys.columns来获取表和列。 sys.extended_properties can get you description information, if entered. 如果输入sys.extended_properties,则可以获取描述信息。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从具有特定名称的所有数据库的所有表中选择 - Select from all tables in all databases with specific name select 列具有特定值的所有表中的表名 - select table name from all tables where column has specific value 来自所有表的特定列的SUM值LIKE table_% - SUM values of specific column from all tables LIKE table_% 从两个表中选择所有列,按表1中的所有列和表2中的特定列分组 - Select all columns from two tables grouping by all columns in table1 and specific column in table2 Microsoft Access联接4个表以显示3个表中的特定值,但显示主表中的所有值 - Microsoft Access joining 4 tables to display specific values from 3 tables but all values from the main table SQL VBA:选择具有特定表名和字段名的所有表 - SQL VBA: Selecting all tables with specific table name and field name 在PostgreSQL中重命名具有特定列名的所有表中的所有列? - Rename all columns from all tables with specific column name in PostgreSQL? 连接两个表。 Select 一个表中的所有行并且仅匹配另一表中的值? - Join two tables. Select all rows from one table and only matching values from other table? 如何从所有表中选择列的所有可能值? - How to select all possible values of columns from all tables? 如何遍历具有特定名称的所有表,然后更新每个表中的特定列值 - How can i traverse all the tables with specific name and then update a particular column value from each table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM