Sql獲取數據表的結構(包括字段名,字段數據類型,長度等等)


SELECT
表名=d.name,
表說明=isnull(f.value,''),
字段序號=a.colorder,
字段名=a.name,
字段標題=isnull(g.[value],''),
標識=case when COLUMNPROPERTY( a.id,a.name,'IsIdentity')=1 then '√'else '' end,
主鍵=case when exists(SELECT 1 FROM sysobjects where xtype='PK' 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,'')
--into ##tx
FROM syscolumns a
left join systypes b on a.xtype=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 sys.extended_properties g on a.id=g.class and a.colid=g.minor_id  --sql2000此處替換為:left join sysproperties g on a.id=g.id and a.colid=g.smallid
left join sys.extended_properties f on d.id=f.class and f.minor_id=0 --sql2000此處替換為:left join sysproperties f on d.id=f.id and f.smallid=0
--where d.name='bsp_onlinetime' --如果只查詢指定表,加上此條件
order by object_name(a.id),a.colorder


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM