0

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]'
7
  • all of these attr_list tables have the same amount of columns and datatypes? This is SQL Server?
    – SQLChao
    Jul 23, 2014 at 17:20
  • 1
    What RDBMS are you using?
    – JodyT
    Jul 23, 2014 at 17:23
  • SQL Server 2012. To give you an example, our UDW is broken down as follows:
    – MikeDes
    Jul 23, 2014 at 17:57
  • SQL Server 2012. To give you an example, our UDW is broken down as follows: 'dbo.account (account_id PK, account_attr_id, insert_date, Insert_op); dbo.account_attr (account_attr_id PK, account_attr_list_id PK/FK, eff_date PK, eff_status PK, attribute, insert_date, Insert_op; dbo.account_attr_list (account_attr_list_id, attr_name, attr_format, short_desc, long_desc, source_db, source_table, source_column, insert_date, insert_op_)'
    – MikeDes
    Jul 23, 2014 at 18:03
  • You can edit your question with the table structures. No need to put it all into a comment.
    – JodyT
    Jul 23, 2014 at 18:40

3 Answers 3

0

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.

0

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%'
1
  • Hi Rhealtycheck, to clarify, I do not wish to return a list of the column names but rather need to return all the values from all the tables in the database where the table name in that database contains 'x' and the column names in (a,b,c). In the future, we will be adding additional attr_list tables and I want to avoid having to go back to the original query/view created to add another union to update a report that lists all attributes and their description.
    – MikeDes
    Jul 23, 2014 at 18:05
-1

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.

1
  • @JorgeCampos, Unfortunately I don't have enough characters in a response to post the code. What I am trying to achieve is to select all values from a specific list of columns for any table in a specific database that contains "attr_list" in the table name.
    – MikeDes
    Jul 23, 2014 at 18:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.