SQL Server 2000/2005資料字典生成方法
--sql server 2000
SELECT
表名 = case when a.colorder=1 then d.name else '' end,
表說明 = case when a.colorder=1 then isnull(f.value,'') else '' end,
欄位序號 = a.colorder,
欄位名 = a.name,
標識 = case when COLUMNPROPERTY( a.id,a.name,'IsIdentity')=1 then '√'else '' end,
主鍵 = case when exists(SELECT 1 FROM sysobjects where xtype='PK' and parent_obj=a.id and name in (
SELECT name FROM sysindexes WHERE indid in(
SELECT indid FROM sysindexkeys WHERE id = a.id AND colid=a.colid))) then '√' else '' end,
型別 = b.name,
佔用位元組數 = a.length,
長度 = COLUMNPROPERTY(a.id,a.name,'PRECISION'),
小數位數 = isnull(COLUMNPROPERTY(a.id,a.name,'Scale'),0),
允許空 = case when a.isnullable=1 then '√'else '' end,
預設值 = isnull(e.text,''),
欄位說明 = isnull(g.[value],'')
FROM
syscolumns a
left join
systypes b
on
a.xusertype=b.xusertype
inner join
sysobjects d
on
a.id=d.id and d.xtype='U' and d.name<>'dtproperties'
left join
syscomments e
on
a.cdefault=e.id
left join
sysproperties g
on
a.id=g.id and a.colid=g.smallid
left join
sysproperties f
on
d.id=f.id and f.smallid=0
where
d.name='要查詢的表' --如果只查詢指定表,加上此條件
order by
a.id,a.colorder
/*
表名 表說明 欄位序號 欄位名 標識 主鍵 型別 佔用位元組數 長度 小數位數 允許空 預設值 欄位說明
------- ----- ------- -------- ---- ------- ------ ------- --------------- ------ ---------- ----------
authors 1 au_id √ id 11 11 0
2 au_lname varchar 40 40 0
3 au_fname varchar 20 20 0
4 phone char 12 12 0 ('UNKNOWN')
5 address varchar 40 40 0 √
6 city varchar 20 20 0 √
7 state char 2 2 0 √
8 zip char 5 5 0 √
9 contract bit 1 1 0
(所影響的行數為 9 行)
*/
--sql server 2005
-- 1. 表結構資訊查詢
-- ========================================================================
-- 表結構資訊查詢
-- 鄒建 2005.08(引用請保留此資訊)
-- ========================================================================
SELECT
TableName=CASE WHEN C.column_id=1 THEN O.name ELSE N'' END,
TableDesc=ISNULL(CASE WHEN C.column_id=1 THEN PTB.[value] END,N''),
Column_id=C.column_id,
ColumnName=C.name,
PrimaryKey=ISNULL(IDX.PrimaryKey,N''),
[IDENTITY]=CASE WHEN C.is_identity=1 THEN N'√'ELSE N'' END,
Computed=CASE WHEN C.is_computed=1 THEN N'√'ELSE N'' END,
Type=T.name,
Length=C.max_length,
Precision=C.precision,
Scale=C.scale,
NullAble=CASE WHEN C.is_nullable=1 THEN N'√'ELSE N'' END,
[Default]=ISNULL(D.definition,N''),
ColumnDesc=ISNULL(PFD.[value],N''),
IndexName=ISNULL(IDX.IndexName,N''),
IndexSort=ISNULL(IDX.Sort,N''),
Create_Date=O.Create_Date,
Modify_Date=O.Modify_date
FROM sys.columns C
INNER JOIN sys.objects O
ON C.[object_id]=O.[object_id]
AND O.type='U'
AND O.is_ms_shipped=0
INNER JOIN sys.types T
ON C.user_type_id=T.user_type_id
LEFT JOIN sys.default_constraints D
ON C.[object_id]=D.parent_object_id
AND C.column_id=D.parent_column_id
AND C.default_object_id=D.[object_id]
LEFT JOIN sys.extended_properties PFD
ON PFD.class=1
AND C.[object_id]=PFD.major_id
AND C.column_id=PFD.minor_id
-- AND PFD.name='Caption' -- 欄位說明對應的描述名稱(一個欄位可以新增多個不同name的描述)
LEFT JOIN sys.extended_properties PTB
ON PTB.class=1
AND PTB.minor_id=0
AND C.[object_id]=PTB.major_id
-- AND PFD.name='Caption' -- 表說明對應的描述名稱(一個表可以新增多個不同name的描述)
LEFT JOIN -- 索引及主鍵資訊
(
SELECT
IDXC.[object_id],
IDXC.column_id,
Sort=CASE INDEXKEY_PROPERTY(IDXC.[object_id],IDXC.index_id,IDXC.index_column_id,'IsDescending')
WHEN 1 THEN 'DESC' WHEN 0 THEN 'ASC' ELSE '' END,
PrimaryKey=CASE WHEN IDX.is_primary_key=1 THEN N'√'ELSE N'' END,
IndexName=IDX.Name
FROM sys.indexes IDX
INNER JOIN sys.index_columns IDXC
ON IDX.[object_id]=IDXC.[object_id]
AND IDX.index_id=IDXC.index_id
LEFT JOIN sys.key_constraints KC
ON IDX.[object_id]=KC.[parent_object_id]
AND IDX.index_id=KC.unique_index_id
INNER JOIN -- 對於一個列包含多個索引的情況,只顯示第1個索引資訊
(
SELECT [object_id], Column_id, index_id=MIN(index_id)
FROM sys.index_columns
GROUP BY [object_id], Column_id
) IDXCUQ
ON IDXC.[object_id]=IDXCUQ.[object_id]
AND IDXC.Column_id=IDXCUQ.Column_id
AND IDXC.index_id=IDXCUQ.index_id
) IDX
ON C.[object_id]=IDX.[object_id]
AND C.column_id=IDX.column_id
-- WHERE O.name=N'要查詢的表' -- 如果只查詢指定表,加上此條件
ORDER BY O.name,C.column_id
-- 2. 索引及主鍵資訊
-- ========================================================================
-- 索引及主鍵資訊
-- 鄒建 2005.08(引用請保留此資訊)
-- ========================================================================
SELECT
TableId=O.[object_id],
TableName=O.Name,
IndexId=ISNULL(KC.[object_id],IDX.index_id),
IndexName=IDX.Name,
IndexType=ISNULL(KC.type_desc,'Index'),
Index_Column_id=IDXC.index_column_id,
ColumnID=C.Column_id,
ColumnName=C.Name,
Sort=CASE INDEXKEY_PROPERTY(IDXC.[object_id],IDXC.index_id,IDXC.index_column_id,'IsDescending')
WHEN 1 THEN 'DESC' WHEN 0 THEN 'ASC' ELSE '' END,
PrimaryKey=CASE WHEN IDX.is_primary_key=1 THEN N'√'ELSE N'' END,
[UQIQUE]=CASE WHEN IDX.is_unique=1 THEN N'√'ELSE N'' END,
Ignore_dup_key=CASE WHEN IDX.ignore_dup_key=1 THEN N'√'ELSE N'' END,
Disabled=CASE WHEN IDX.is_disabled=1 THEN N'√'ELSE N'' END,
Fill_factor=IDX.fill_factor,
Padded=CASE WHEN IDX.is_padded=1 THEN N'√'ELSE N'' END
FROM sys.indexes IDX
INNER JOIN sys.index_columns IDXC
ON IDX.[object_id]=IDXC.[object_id]
AND IDX.index_id=IDXC.index_id
LEFT JOIN sys.key_constraints KC
ON IDX.[object_id]=KC.[parent_object_id]
AND IDX.index_id=KC.unique_index_id
INNER JOIN sys.objects O
ON O.[object_id]=IDX.[object_id]
INNER JOIN sys.columns C
ON O.[object_id]=C.[object_id]
AND O.type='U'
AND O.is_ms_shipped=0
AND IDXC.Column_id=C.Column_id
-- INNER JOIN -- 對於一個列包含多個索引的情況,只顯示第1個索引資訊
-- (
-- SELECT [object_id], Column_id, index_id=MIN(index_id)
-- FROM sys.index_columns
-- GROUP BY [object_id], Column_id
SELECT
表名 = case when a.colorder=1 then d.name else '' end,
表說明 = case when a.colorder=1 then isnull(f.value,'') else '' end,
欄位序號 = a.colorder,
欄位名 = a.name,
標識 = case when COLUMNPROPERTY( a.id,a.name,'IsIdentity')=1 then '√'else '' end,
主鍵 = case when exists(SELECT 1 FROM sysobjects where xtype='PK' and parent_obj=a.id and name in (
SELECT name FROM sysindexes WHERE indid in(
SELECT indid FROM sysindexkeys WHERE id = a.id AND colid=a.colid))) then '√' else '' end,
型別 = b.name,
佔用位元組數 = a.length,
長度 = COLUMNPROPERTY(a.id,a.name,'PRECISION'),
小數位數 = isnull(COLUMNPROPERTY(a.id,a.name,'Scale'),0),
允許空 = case when a.isnullable=1 then '√'else '' end,
預設值 = isnull(e.text,''),
欄位說明 = isnull(g.[value],'')
FROM
syscolumns a
left join
systypes b
on
a.xusertype=b.xusertype
inner join
sysobjects d
on
a.id=d.id and d.xtype='U' and d.name<>'dtproperties'
left join
syscomments e
on
a.cdefault=e.id
left join
sysproperties g
on
a.id=g.id and a.colid=g.smallid
left join
sysproperties f
on
d.id=f.id and f.smallid=0
where
d.name='要查詢的表' --如果只查詢指定表,加上此條件
order by
a.id,a.colorder
/*
表名 表說明 欄位序號 欄位名 標識 主鍵 型別 佔用位元組數 長度 小數位數 允許空 預設值 欄位說明
------- ----- ------- -------- ---- ------- ------ ------- --------------- ------ ---------- ----------
authors 1 au_id √ id 11 11 0
2 au_lname varchar 40 40 0
3 au_fname varchar 20 20 0
4 phone char 12 12 0 ('UNKNOWN')
5 address varchar 40 40 0 √
6 city varchar 20 20 0 √
7 state char 2 2 0 √
8 zip char 5 5 0 √
9 contract bit 1 1 0
(所影響的行數為 9 行)
*/
--sql server 2005
-- 1. 表結構資訊查詢
-- ========================================================================
-- 表結構資訊查詢
-- 鄒建 2005.08(引用請保留此資訊)
-- ========================================================================
SELECT
TableName=CASE WHEN C.column_id=1 THEN O.name ELSE N'' END,
TableDesc=ISNULL(CASE WHEN C.column_id=1 THEN PTB.[value] END,N''),
Column_id=C.column_id,
ColumnName=C.name,
PrimaryKey=ISNULL(IDX.PrimaryKey,N''),
[IDENTITY]=CASE WHEN C.is_identity=1 THEN N'√'ELSE N'' END,
Computed=CASE WHEN C.is_computed=1 THEN N'√'ELSE N'' END,
Type=T.name,
Length=C.max_length,
Precision=C.precision,
Scale=C.scale,
NullAble=CASE WHEN C.is_nullable=1 THEN N'√'ELSE N'' END,
[Default]=ISNULL(D.definition,N''),
ColumnDesc=ISNULL(PFD.[value],N''),
IndexName=ISNULL(IDX.IndexName,N''),
IndexSort=ISNULL(IDX.Sort,N''),
Create_Date=O.Create_Date,
Modify_Date=O.Modify_date
FROM sys.columns C
INNER JOIN sys.objects O
ON C.[object_id]=O.[object_id]
AND O.type='U'
AND O.is_ms_shipped=0
INNER JOIN sys.types T
ON C.user_type_id=T.user_type_id
LEFT JOIN sys.default_constraints D
ON C.[object_id]=D.parent_object_id
AND C.column_id=D.parent_column_id
AND C.default_object_id=D.[object_id]
LEFT JOIN sys.extended_properties PFD
ON PFD.class=1
AND C.[object_id]=PFD.major_id
AND C.column_id=PFD.minor_id
-- AND PFD.name='Caption' -- 欄位說明對應的描述名稱(一個欄位可以新增多個不同name的描述)
LEFT JOIN sys.extended_properties PTB
ON PTB.class=1
AND PTB.minor_id=0
AND C.[object_id]=PTB.major_id
-- AND PFD.name='Caption' -- 表說明對應的描述名稱(一個表可以新增多個不同name的描述)
LEFT JOIN -- 索引及主鍵資訊
(
SELECT
IDXC.[object_id],
IDXC.column_id,
Sort=CASE INDEXKEY_PROPERTY(IDXC.[object_id],IDXC.index_id,IDXC.index_column_id,'IsDescending')
WHEN 1 THEN 'DESC' WHEN 0 THEN 'ASC' ELSE '' END,
PrimaryKey=CASE WHEN IDX.is_primary_key=1 THEN N'√'ELSE N'' END,
IndexName=IDX.Name
FROM sys.indexes IDX
INNER JOIN sys.index_columns IDXC
ON IDX.[object_id]=IDXC.[object_id]
AND IDX.index_id=IDXC.index_id
LEFT JOIN sys.key_constraints KC
ON IDX.[object_id]=KC.[parent_object_id]
AND IDX.index_id=KC.unique_index_id
INNER JOIN -- 對於一個列包含多個索引的情況,只顯示第1個索引資訊
(
SELECT [object_id], Column_id, index_id=MIN(index_id)
FROM sys.index_columns
GROUP BY [object_id], Column_id
) IDXCUQ
ON IDXC.[object_id]=IDXCUQ.[object_id]
AND IDXC.Column_id=IDXCUQ.Column_id
AND IDXC.index_id=IDXCUQ.index_id
) IDX
ON C.[object_id]=IDX.[object_id]
AND C.column_id=IDX.column_id
-- WHERE O.name=N'要查詢的表' -- 如果只查詢指定表,加上此條件
ORDER BY O.name,C.column_id
-- 2. 索引及主鍵資訊
-- ========================================================================
-- 索引及主鍵資訊
-- 鄒建 2005.08(引用請保留此資訊)
-- ========================================================================
SELECT
TableId=O.[object_id],
TableName=O.Name,
IndexId=ISNULL(KC.[object_id],IDX.index_id),
IndexName=IDX.Name,
IndexType=ISNULL(KC.type_desc,'Index'),
Index_Column_id=IDXC.index_column_id,
ColumnID=C.Column_id,
ColumnName=C.Name,
Sort=CASE INDEXKEY_PROPERTY(IDXC.[object_id],IDXC.index_id,IDXC.index_column_id,'IsDescending')
WHEN 1 THEN 'DESC' WHEN 0 THEN 'ASC' ELSE '' END,
PrimaryKey=CASE WHEN IDX.is_primary_key=1 THEN N'√'ELSE N'' END,
[UQIQUE]=CASE WHEN IDX.is_unique=1 THEN N'√'ELSE N'' END,
Ignore_dup_key=CASE WHEN IDX.ignore_dup_key=1 THEN N'√'ELSE N'' END,
Disabled=CASE WHEN IDX.is_disabled=1 THEN N'√'ELSE N'' END,
Fill_factor=IDX.fill_factor,
Padded=CASE WHEN IDX.is_padded=1 THEN N'√'ELSE N'' END
FROM sys.indexes IDX
INNER JOIN sys.index_columns IDXC
ON IDX.[object_id]=IDXC.[object_id]
AND IDX.index_id=IDXC.index_id
LEFT JOIN sys.key_constraints KC
ON IDX.[object_id]=KC.[parent_object_id]
AND IDX.index_id=KC.unique_index_id
INNER JOIN sys.objects O
ON O.[object_id]=IDX.[object_id]
INNER JOIN sys.columns C
ON O.[object_id]=C.[object_id]
AND O.type='U'
AND O.is_ms_shipped=0
AND IDXC.Column_id=C.Column_id
-- INNER JOIN -- 對於一個列包含多個索引的情況,只顯示第1個索引資訊
-- (
-- SELECT [object_id], Column_id, index_id=MIN(index_id)
-- FROM sys.index_columns
-- GROUP BY [object_id], Column_id
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/16436858/viewspace-663370/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- SQL Server 2000/2005/2008刪除或壓縮資料庫日誌的方法SQLServer資料庫
- sql server 2005資料庫快照SQLServer資料庫
- 如何將SQL Server 2000備份的資料庫檔案還原(升級)為SQL Server 2005或更高版本的資料庫?SQLServer資料庫
- sql server 2005 資料修改的內部原理SQLServer
- SQL Server 2014 匯出資料字典SQLServer
- 如何建立和還原SQL Server 2005資料庫?SQLServer資料庫
- 如何建立和還原SQL Server 2000資料庫?SQLServer資料庫
- 已安裝 SQL Server 2005 Express 工具。若要繼續,請刪除 SQL Server 2005 Express 工具SQLServerExpress
- 還原sql server 2000資料庫的坑,不同版本資料庫SQLServer資料庫
- SQL2005、2008、2000 清空刪除日誌SQL
- SQL Server中Table字典資料的查詢SQL示例程式碼SQLServer
- SQL Server 2005效能調整二(zt)SQLServer
- SQL Server 2005效能調整一(zt)SQLServer
- SQL Server映象自動生成指令碼方法SQLServer指令碼
- navicat生成mysql資料字典MySql
- lavavel 自動生成資料字典
- SQL Server2005使用CTE實現遞迴QCSQLServer遞迴
- SQL Server 2005詳細安裝過程及配置SQLServer
- SQL Server資料庫恢復,SQL Server資料恢復,SQL Server資料誤刪除恢復工具SQLRescueSQLServer資料庫資料恢復
- SQL Server 2005的複製儲存過程選項BYSQLServer儲存過程
- 資料庫映象 (SQL Server)資料庫SQLServer
- SQL server 修改表資料SQLServer
- SQL Server資料庫安全SQLServer資料庫
- SQL SERVER 2012資料庫自動備份的方法SQLServer資料庫
- SQL Server 批量生成資料庫內多個表的表結構SQLServer資料庫
- mssql生成資料庫字典指令碼-MarkDownSQL資料庫指令碼
- 如何利用showdoc自動生成資料字典
- SQL Server 2000詳細安裝過程及配置SQLServer
- Sql Server資料庫檔案丟失的恢復方法SQLServer資料庫
- SQL server資料庫高可用日誌傳送的方法SQLServer資料庫
- sql server 資料庫收縮SQLServer資料庫
- Blazor使用sql server 資料庫BlazorSQLServer資料庫
- SQL Server資料庫巡檢SQLServer資料庫
- SQL Server收縮資料庫SQLServer資料庫
- SQL Server-資料型別SQLServer資料型別
- 管理SQL Server資料庫安全SQLServer資料庫
- SQL Server資料庫遷移SQLServer資料庫
- Microsoft SQL Server 2005中查詢優化器使用的統計資訊一(zt)ROSSQLServer優化
- Microsoft SQL Server 2005中查詢優化器使用的統計資訊三(zt)ROSSQLServer優化