查詢Sql Server數據庫對象結構
- 查詢數據庫
- 查詢架構
- 查詢表
- 查詢列
- 查詢存儲過程
- 查詢視圖
1.查詢某一服務器下全部數據庫
select t.[name] as 數據庫
from sys.databases as t
order by name
2.查詢某一數據庫的架構
select name as 架構,
schema_id as 架構Id
--,principal_id
from sys.schemas
where principal_id = 1;
3.查詢某一數據庫的表
select s.name+'.'+t.name as 表,
t.object_id as 表Id
from sys.tables as t
inner join sys.schemas as s on t.schema_id = s.schema_id and s.principal_id=1
4.查詢某一數據庫的全部的列
select
s.name as 架構,
d.NAME as 表,
A.NAME as 列,
COLUMNPROPERTY( a.id,a.name,'IsIdentity') as 是否爲自增, --0非自增 1自增
B.NAME AS 數據類型,
COLUMNPROPERTY(A.ID,A.NAME,'PRECISION') AS 精度,
ISNULL(COLUMNPROPERTY(A.ID,A.NAME,'Scale'),0) AS 保留位,
A.ISNULLABLE AS 是否爲空, -- 0空 1非空,
ISNULL(E.TEXT,'') as 默認值,
ISNULL(g.[value],'') AS 列說明
FROM syscolumns a
LEFT JOIN systypes b ON a.xtype=b.xusertype
INNER JOIN sys.objects d ON a.id=d.object_id AND d.type='U'AND d.name<>'dtproperties'
inner join sys.schemas as s on d.schema_id=s.schema_id
LEFT JOIN syscomments e ON a.cdefault=e.id
LEFT JOIN sys.extended_properties g ON a.id=g.major_id AND a.colid=g.minor_id
LEFT JOIN sys.extended_properties f ON d.object_id=f.major_id AND f.minor_id =0
5.查詢存儲過程
select
s.Name as 架構,
p.Name as 存儲過程,
sm.definition as 存儲過程內容
from sys.procedures as p
inner join sys.sql_modules sm ON p.object_id = sm.object_id
inner join sys.schemas as s on p.schema_id=s.schema_id
6.查詢視圖
select
s.name+'.'+t.name as 視圖,
t.object_id as 視圖Id
from sys.views as t
inner join sys.schemas as s on t.schema_id = s.schema_id and s.principal_id=1