擷取sqlserver資料庫中所有庫、表、欄位名的方法

來源:互聯網
上載者:User

標籤:style   blog   color   io   資料   ar   div   代碼   

1.擷取所有資料庫名:SELECT Name from Master..SysDatabases ORDER BY Name2.擷取所有表名:SELECT Name from DatabaseName..SysObjects Where XType=‘U‘ ORDER BY NameXType=‘U‘:表示所有使用者表;XType=‘S‘:表示所有系統資料表;3.擷取所有欄位名:SELECT Name from SysColumns WHERE id=Object_Id(‘TableName‘)access :如果表存在,如何得到這個表?SELECT * from MSysObjects where name=‘ExamResultTime ‘ and type=1 and flags=0//得到該資料庫中所有的表名SELECT name from MSysObjects where type=1 and flags=0      1、擷取所有使用者名稱:       Select name FROM Sysusers where status=‘2‘ and islogin=‘1‘      islogin=‘1‘   :表示帳戶       islogin=‘0‘   :表示角色       status=‘2‘   :表示使用者帳戶       status=‘0‘   :表示糸統帳戶             2、擷取所有資料庫名:       Select Name FROM Master..SysDatabases orDER BY Name            3、擷取所有表名:       Select Name FROM DatabaseName..SysObjects Where XType=‘U‘ orDER BY Name      XType=‘U‘    :表示所有使用者表;       XType=‘S‘    :表示所有系統資料表;             4、擷取所有欄位名:       Select Name FROM SysColumns Where id=Object_Id(‘表名‘‘)             5、擷取資料庫所有類型:       select name from systypes             6、擷取主鍵欄位:       Select  name FROM SysColumns Where id=Object_Id(‘表名‘) and colid=(select top 1 keyno from sysindexkeys where id=Object_Id(‘表名‘))             7、擷取表欄位的基本資料:        程式碼       Select        欄位名=rtrim(b.name),         主鍵=CASE WHEN h.id IS NOT NULL  THEN ‘PK‘ ELSE ‘‘ END,         欄位類型=type_name(b.xusertype)+CASE WHEN b.colstat&1=1 THEN ‘[ID(‘ + CONVERT(varchar, ident_seed(a.name))+‘,‘+CONVERT(varchar,ident_incr(a.name))+‘)]‘ ELSE ‘‘ END,         長度=b.length,          允許空=CASE b.isnullable WHEN 0 THEN ‘N‘ ELSE ‘Y‘ END,          預設值=isnull(e.text, ‘‘),         欄位說明=isnull(c.value, ‘‘)       FROM sysobjects a, syscolumns b       LEFT OUTER JOIN sysproperties c ON b.id = c.id AND b.colid = c.smallid       LEFT OUTER JOIN syscomments e ON b.cdefault = e.id       LEFT OUTER JOIN (Select g.id, g.colid FROM sysindexes f, sysindexkeys g Where (f.id=g.id)AND(f.indid=g.indid)AND(f.indid>0)AND(f.indid<255)AND(f.status&2048)<>0) h ON (b.id=h.id)AND(b.colid=h.colid)       Where (a.id=b.id)AND(a.id=object_id(‘要查詢的表‘))  --要查詢的表改成你要查詢表的名稱       orDER BY b.colid              程式碼       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 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,‘‘),         欄位說明=isnull(g.[value],‘‘)       FROM syscolumns a       left join systypes b on a.xusertype=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 sysproperties g on (a.id=g.id)and(a.colid=g.smallid)       left join sysproperties f on (d.id=f.id)and(f.smallid=0)       --where d.name=‘要查詢的表‘         --如果只查詢指定表,加上此條件       order by a.id,a.colorder  

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.