查詢SQL Server2008資料庫表資訊

2021-06-18 01:52:15 字數 2127 閱讀 7254

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 dbo.sysindexes si

inner join dbo.sysindexkeys sik on si.id = sik.id and si.indid = sik.indid

inner join dbo.syscolumns sc on sc.id = sik.id and sc.colid = sik.colid

inner join dbo.sysobjects so on so.name = so.name and so.xtype = 'pk'

where sc.id = a.id and sc.colid = a.colid) then '√'

else ''

end ,

外來鍵=case when tony.fkey is not null and tony.fkey=a.colid then '√' else '' end ,

外來鍵表=case when tony.fkey is not null and tony.fkey=a.colid

then object_name(tony.fkeyid) else ''

end ,

外來鍵字段=case when tony.fkey is not null and tony.fkey=a.colid

then (select name from syscolumns

where colid=tony.fkey and id=tony.fkeyid)

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], ''),

建立時間=d.crdate,

更改時間=case when a.colorder = 1 then d.refdate else null end

from dbo.syscolumns a

left join dbo.systypes b on a.xtype = b.xusertype

inner join dbo.sysobjects d on a.id = d.id and d.xtype = 'u' and d.status >= 0

left join dbo.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.id = f.major_id and f.minor_id = 0

left join sysobjects htl on htl.parent_obj=d.id and htl.xtype='f'

left join sysforeignkeys tony on htl.id=tony.constid

--where d.name='tb' --這裡輸入包含表名稱的條件

order by d.id, a.colorder

SQLServer2008語句查詢

1 判斷資料庫是否存在 if exists select from sys.databases where name 資料庫名 drop database 資料庫名 2 判斷表是否存在 if exists select from sysobjects where id object id 表名 an...

SQL Server 2008 實施查詢優化建議

可以手動實施資料庫引擎優化顧問的建議,也可以在優化會話中自動實施。如果需要在實施建議之前檢查優化結果,請使用資料庫引擎優化顧問圖形使用者介面 gui 然後可以使用 microsoftsql servermanagement studio 手動執行資料庫引擎優化顧問在分析完工作負荷之後生成的 tran...

SQL SERVER 2008查詢其他資料庫

1 訪問本地的其他資料庫 啟用ad hoc distributed queries exec sp configure show advanced options 1reconfigure exec sp configure ad hoc distributed queries 1reconfigu...