如何获取服务器上的数据库+元数据列表及其拥有的表格+ 单一视图中的表格元数据?
加入sys.databases
和sys.tables
的任何方式?
提前谢谢
答案 0 :(得分:0)
将所需的选择集合到变量中并执行SQL
Declare @a varchar(max)
Select @a=''
select @a=@a + 'select '''+name+''' as DB, * from ['+name+'].sys.tables' + CHAR(10) + 'UNION '
from master.dbo.sysdatabases
Set @a=SUBSTRING(@a,1,len(@a)-Len('UNION '))
EXEC( @a)
答案 1 :(得分:0)
我的方法是创建一个可以检索所有内容的视图 表名。这是一个将构建它的存储过程 视图。如果添加或删除任何新内容,则需要重新创建视图 数据库,但只要您的数据库,视图应保持良好 保持不变。
循环:
INSERT INTO t_fo_databases
SELECT name FROM sys.databases WHERE name like '%SITE' ORDER BY name
DECLARE @DBs as varchar(MAX)
WHILE (SELECT TOP 1 names FROM t_fo_databases) LIKE '%SITE'
BEGIN
SET @DBs = (SELECT TOP 1 names FROM t_fo_databases)
PRINT @DBs
USE [master]
DELETE FROM t_fo_Databases WHERE names = @DBs
END
查看哪个将检索表名
CREATE PROC sp_BuildDBTablesView
as
IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.Views WHERE TABLE_NAME =
'vw_DBTables')
DROP TABLE vw_DBTables
DECLARE @CreateView varchar(8000)
SET @CreateView = 'CREATE VIEW vw_DBTables
as
'
SELECT name
INTO #DBs
FROM sysDatabases
DECLARE @DBName varchar(100)
WHILE (SELECT COUNT(*) FROM #DBs) 0
BEGIN
SET @DBName = (SELECT min(name) FROM #DBs)
SET @CreateView = @CreateView + 'SELECT * FROM ' + @DBName +
'.INFORMATION_SCHEMA.TABLES' + CHAR(13) + 'UNION' + CHAR(13)
DELETE #DBs WHERE name = @DBName
END
SET @CreateView = LEFT(@CreateView, LEN(@CreateView) - 6) --- Take
off the last UNION
PRINT @CreateView
EXEC master..sp_SQLExec @CreateView
DROP TABLE #DBs
GO
现在,只要您想查看表格,只需运行:
SELECT * FROM master.dbo.vw_DBTables
您将获得整个列表。
我希望有所帮助。
答案 2 :(得分:0)
大家好,谢谢你的帮助!
我已经指出以下文章提供了非常简洁的解决方案:
------------
-- Microsoft SQL Server database meta data - list of all tables in database
------------
USE AdventureWorks2008;
-- SQL list all tables using system view sys.objects
SELECT SchemaName = SCHEMA_NAME(schema_id),
TableName = name
FROM sys.objects
WHERE TYPE = 'U'
AND is_ms_shipped = 0
ORDER BY SchemaName,
TableName
GO
/* Partial results
SchemaName TableName
dbo AWBuildVersion
dbo Category
dbo DatabaseLog
dbo ErrorLog
dbo SubCategory
dbo sysdiagrams
HumanResources Department
HumanResources Employee
HumanResources EmployeeDepartmentHistory
HumanResources EmployeePayHistory
*/
-- T-SQL alternative: using a different system view - sys.tables
SELECT SchemaName = SCHEMA_NAME(schema_id),
TableName = name
FROM sys.tables
ORDER BY SchemaName,
TableName
GO
------------
-- MSSQL information_schema views - ANSI 92 SQL compatible
SELECT *
FROM INFORMATION_SCHEMA.TABLES
WHERE table_type = 'BASE TABLE'
ORDER BY TABLE_SCHEMA,
TABLE_NAME
/* Partial results
TABLE_CATALOG TABLE_SCHEMA TABLE_NAME TABLE_TYPE
AdventureWorks Sales SalesOrderHeader BASE TABLE
AdventureWorks Sales SalesOrderHeaderSalesReason BASE TABLE
AdventureWorks Sales SalesPerson BASE TABLE
AdventureWorks Sales SalesPersonQuotaHistory BASE TABLE
AdventureWorks Sales SalesReason BASE TABLE
AdventureWorks Sales SalesTaxRate BASE TABLE
AdventureWorks Sales SalesTerritory BASE TABLE
AdventureWorks Sales SalesTerritoryHistory BASE TABLE
*/
------------
--- SQL list all table in all databases on PRODSRV\SQL2008 SQL Server instance
DECLARE @CMD VARCHAR(1024)
CREATE TABLE #TableList (
DatabaseName SYSNAME,
SchemaName SYSNAME,
TableName SYSNAME)
SET @CMD = 'USE [?]; SELECT DB_NAME()DATABASE_NAME, SCHEMA_NAME(schema_id),
NAME FROM SYS.TABLES'
PRINT @CMD -- test & debug
-- T-SQL insert exec undocumented system procedure
INSERT INTO #TableList
EXEC SP_MSFOREACHDB @CMD
DELETE FROM #TableList
WHERE DatabaseName IN ('master','msdb','tempdb','model')
SELECT *
FROM #TableList
ORDER BY DatabaseName, SchemaName, TableName
GO
/* Partial results
DatabaseName SchemaName TableName
AdventureWorks2008 Sales SalesTerritoryHistory
AdventureWorks2008 Sales ShoppingCartItem
AdventureWorks2008 Sales SpecialOffer
*/
DROP TABLE #TableList
GO
------------