我需要以下格式的所有用户定义数据库的结构细节: -
名称类型详情
其中的详细信息包括 - 如果它是一个表而不是列A(类型,大小),ColumnB(类型,大小)....,ConstraintA,ConstarintB ...,IndexeA(索引列),IndexB(IndexedColumns) .....
如果它是该功能或程序的主体的功能或程序。
以下是我创建的查询,但10个数据库大约需要8分钟: -
CREATE TABLE #modelStructure (Name VARCHAR(500),TYPE VARCHAR(256),DETAILS NVARCHAR(MAX))
DECLARE @dbTableName NVARCHAR(500)='#modelStructure'
USE MODEL
SELECT * INTO #AllTablesAndViews FROM sys.objects ob WHERE ob.is_ms_shipped=0 AND ob.type IN ('U','V')
SELECT tab.name AS TableName,tab.object_id,tab.type_desc AS Type,col.name AS ColumnName,typ.name AS ColumnType,typ.max_length AS ColumnSize
,ob.name AS ConstraintName,trig.name AS TriggerName INTO #tempTableDetails
FROM #AllTablesAndViews tab
LEFT JOIN sys.columns col ON tab.object_id=col.object_id
LEFT JOIN sys.objects ob ON ob.parent_object_id=tab.object_id
LEFT JOIN sys.triggers trig ON trig.parent_id=tab.object_id
LEFT JOIN sys.types typ ON col.user_type_id=typ.user_type_id
WHERE tab.name <>'sysdiagrams' AND ob.type_desc LIKE '%CONSTRAINT' AND tab.is_ms_shipped=0
GROUP BY tab.name ,tab.object_id,tab.type_desc ,col.name,typ.name,typ.max_length,ob.name ,trig.name
SELECT * INTO #stuffedTable FROM
(
SELECT ttd.TableName AS Name,ttd.Type,
'COLUMNS :-'+
STUFF((SELECT ',' + t1.ColumnName+'('+'Type:'+CONVERT(VARCHAR(200),t1.ColumnType)+' Size:'+CONVERT(VARCHAR(200),t1.ColumnSize)+')'
FROM #tempTableDetails t1
WHERE t1.object_id=ttd.object_id
GROUP BY t1.TableName,t1.ColumnName,t1.ColumnType,t1.ColumnSize
ORDER BY t1.ColumnName
FOR XML PATH('')),1,1,'')+' || '+
'CONSTRAINTS:-'+ STUFF((SELECT ',' + t1.ConstraintName
FROM #tempTableDetails t1
WHERE t1.object_id=ttd.object_id
GROUP BY t1.TableName,t1.ConstraintName
ORDER BY t1.ConstraintName
FOR XML PATH('')),1,1,'')+' || '+
'TRIGGERS:-'+ STUFF((SELECT ',' + ISNULL(t1.TriggerName,'')
FROM #tempTableDetails t1
WHERE t1.object_id=ttd.object_id
GROUP BY t1.TableName,t1.TriggerName
ORDER BY t1.TriggerName
FOR XML PATH('')),1,1,'')+' || ' AS Details
FROM #tempTableDetails ttd
)t
GROUP BY t.Details,t.Name,t.Type ORDER BY Name
;WITH cteIndexDetails
AS
(
SELECT
ind.name IndexName ,
t.name TableName,
col.name IndexColumn
FROM
sys.indexes ind
INNER JOIN
sys.index_columns ic ON ind.object_id = ic.object_id and ind.index_id = ic.index_id
INNER JOIN
sys.columns col ON ic.object_id = col.object_id and ic.column_id = col.column_id
INNER JOIN
sys.tables t ON ind.object_id = t.object_id
WHERE
t.is_ms_shipped = 0
AND t.name <> 'sysdiagrams'
AND ic.is_included_column=0 GROUP BY ind.name,t.name,col.name
)
SELECT id.TableName,'INDEXES :-'+STUFF((SELECT ',' + t1.IndexName +'( Index Column: '+t1.IndexColumn+' )'
FROM cteIndexDetails t1
WHERE t1.TableName=id.TableName
GROUP BY t1.TableName,t1.IndexName,t1.IndexColumn
ORDER BY t1.IndexName
FOR XML PATH('')),1,1,'')AS [IndexDetails] INTO #indexDetails
FROM cteIndexDetails id GROUP BY TableName
INSERT INTO #modelStructure
SELECT Name,TYPE,DETAILS
FROM
(
SELECT st.Name,st.Type,(st.Details+''+id.IndexDetails) AS Details FROM #stuffedTable st INNER JOIN #indexDetails id ON st.Name=id.TableName
UNION
SELECT
r.ROUTINE_NAME AS 'Name'
,r.ROUTINE_TYPE COLLATE database_default AS 'Type'
,sm.definition AS 'DETAILS'
FROM INFORMATION_SCHEMA.ROUTINES r
INNER JOIN sys.sql_modules sm ON sm.object_id=object_id(r.ROUTINE_NAME)
WHERE (ROUTINE_TYPE = 'function' OR ROUTINE_TYPE = 'procedure')
)t ORDER BY t.TYPE,t.Name
DROP TABLE #AllTablesAndViews
DROP TABLE #tempTableDetails
DROP TABLE #stuffedTable
DROP TABLE #indexDetails
DROP TABLE #modelStructure
答案 0 :(得分:0)
如果您想获得表格的结构,请使用
之类的东西SELECT *
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_name = 'tablename'