简体   繁体   中英

Select all values from all tables with specific table name

EDIT original question:

Our UDW is broken out into attribute and attribute list tables.

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.

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.

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:

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 * . 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 . You can use sys.tables and join to sys.columns to get your tables and columns. sys.extended_properties can get you description information, if entered.

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