SQL Server 根據表名獲取表的所有列及屬性


實例一:             

select a.name columnname,c.name as typename,case when a.is_nullable =then 'Not Nullelse 'Nullend as nullable,a.*
from sys.columns a , sys.objects b, sys.types c 
where a.object_id= b.object_id and b.name='表名and a.system_type_id=c.system_type_id order by a.column_id

實例二:

select
     c.name  as  [ 字段名 ] ,t.name  as  [ 字段類型 ]
     , convert ( bit ,c.IsNullable)   as  [ 可否為空 ]
     , convert ( bit , case  when  exists ( select  1  from  sysobjects  where  xtype = ' PK '  and  parent_obj = c.id  and  name  in  (
          select  name  from  sysindexes  where  indid  in (
              select  indid  from  sysindexkeys  where  id  =  c.id  and  colid = c.colid)))  then  1  else  0  end
                  as  [ 是否主鍵 ]
     , convert ( bit , COLUMNPROPERTY (c.id,c.name, ' IsIdentity ' ))  as  [ 自動增長 ]
     ,c.Length  as  [ 占用字節 ] 
     , COLUMNPROPERTY (c.id,c.name, ' PRECISION ' as  [ 長度 ]
     , isnull ( COLUMNPROPERTY (c.id,c.name, ' Scale ' ), 0 as  [ 小數位數 ]
     , ISNULL (CM. text , '' as  [ 默認值 ]
     , isnull (ETP.value, '' AS  [ 字段描述 ]
      -- ,ROW_NUMBER() OVER (ORDER BY C.name) AS [Row]
from  syscolumns c
inner  join  systypes t  on  c.xusertype  =  t.xusertype 
left  join  sys.extended_properties ETP  on  ETP.major_id  =  c.id  and  ETP.minor_id  =  c.colid  and  ETP.name  = ' MS_Description ' 
left  join  syscomments CM  on  c.cdefault = CM.id
where  c.id  =  object_id ( ' 表名 ' )

 

 


免責聲明!

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



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