[英]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.