Sql Server 查詢表名的備註

2021-08-20 21:35:13 字數 3621 閱讀 4916

查詢表名描述  ms_description

select tbs.name 表名,ds.value 描述       

from sys.extended_properties ds

left join sysobjects tbs on ds.major_id=tbs.id

where ds.minor_id=0 and

tbs.name='schedulerecords';--表名

新增表的描述

execute sp_addextendedproperty n'ms_description', n'選單表', n'user', n'dbo', n'table', n'menus', null, null;
更新表的描述

execute sp_updateextendedproperty n'ms_description', n'選單表', n'user', n'dbo', n'table', n'menus', null, null;
查詢表的外來鍵

select tbs.name 表名,ds.value 描述      

from sys.extended_properties ds

left join sysobjects tbs on ds.major_id=tbs.id

where ds.minor_id=0 and

tbs.name='schedulerecords';--表名

where object_name(sysobjects.parent_obj)='table name'

sql查詢表的所有欄位的備註說明

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'tablename' -- 如果只查詢指定表,加上此條件

order by o.name,c.column_id

查詢SQL Server中的表名

sqlconnection conn new sqlconnection server uid sa pwd 123 database 08 09cgywdb connection timeout 2 conn.open sqlcommand comm new sqlcommand select o...

sql server 查詢表名,儲存過程,列名等

1 獲取當前資料庫中的所有使用者表 select name from sysobjects where xtype u and status 0 2 獲取某乙個表的所有字段 select name from syscolumns where id object id n 表名 select a.fr...

SQL Server 快速換表名

經常在我們的業務系統中,表的增長比較快,表的資料量非常龐大,這時我們需要建立乙個新表來替代它,那怎麼快速的把這新錶方上去呢?如人工操作,有可能是會影響業務系統的,所以我們考慮用命令的方式來處理。主要的方式是就是使用系統自帶的儲存過程 sp rename exec sp rename oldtable...