代碼如下:--設(shè)置sql 可以獲取實(shí)例名稱sp_configure 'xp_cmdshell' , 1;goreconfigure;go
--獲取實(shí)例名EXEC sys .xp_cmdshell 'sqlcmd -Lc'
--獲取所有數(shù)據(jù)庫(kù)Select * FROM Master..SysDatabases order by Name
--獲取數(shù)據(jù)庫(kù)所有表use yeekang ---數(shù)據(jù)庫(kù)名稱select * from sysobjects where type = 'U' order by name
--獲取指定表字段select * from syscolumns where id= OBJECT_ID('Userinfo' )
SELECT表名 =case when a .colorder= 1 then d.name else '' end ,表說明 =case when a .colorder= 1 then isnull(f .value, '') else '' end,字段序號(hào) =a. colorder,字段名 =a. name,標(biāo)識(shí) =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,占用字節(jié)數(shù) =a. length,長(zhǎng)度 =COLUMNPROPERTY( a.id ,a. name,'PRECISION' ),小數(shù)位數(shù) =isnull( COLUMNPROPERTY(a .id, a.name ,'Scale'), 0),允許空 =case when a .isnullable= 1 then '√' else '' end,默認(rèn)值 =isnull( e.text ,''),字段說明 =isnull( g.[value] ,'')
復(fù)制代碼 代碼如下:FROM syscolumns aleft join systypes b on a. xusertype=b .xusertypeinner join sysobjects d on a. id=d .id and d .xtype= 'U' and d.name <>'dtproperties'left join syscomments e on a. cdefault=e .idleft join sys. extended_properties g on a.id =g. major_id and a.colid =g. minor_idleft join sys. extended_properties f on d.id =f. major_id and f. minor_id=0where a .id= OBJECT_ID('Userinfo' )order by a. id,a .colorder
另一例:
在SQL Server數(shù)據(jù)庫(kù)中每一個(gè)數(shù)據(jù)庫(kù)都有一個(gè)sysobjects系統(tǒng)表,這個(gè)表里面存儲(chǔ)了當(dāng)前數(shù)據(jù)庫(kù)的所有對(duì)象,包括對(duì)象表,用戶表,視圖,觸發(fā)器,約束,默認(rèn)值,日志,和存儲(chǔ)過程的信。
先列一下這張表的一些字段名:
name 對(duì)象名id 對(duì)象標(biāo)識(shí)號(hào)xtype 對(duì)象類型uid 所有者對(duì)象的用戶IDparent_obj 父對(duì)象的對(duì)象標(biāo)識(shí)號(hào)crdate 對(duì)象的創(chuàng)建日期ftcatid 為全文索引注冊(cè)的所有用戶表的全文目錄標(biāo)識(shí)符schema_ver 版本號(hào),category 用于發(fā)布,約束和標(biāo)識(shí)
看上上面的字段你應(yīng)該已經(jīng)清楚了吧...xtype這個(gè)字段就是確定對(duì)象類型的...
如果你想獲取數(shù)據(jù)庫(kù)中所有的表信息,你可以寫如下的查詢語句:
代碼如下:
select * from sysobjects where xtype='u'
如果你想獲取數(shù)據(jù)庫(kù)中所有的存儲(chǔ)過程的信息,你可以寫如下的查詢語句:
代碼如下:
select * from sysobjects where xtype='p'
如果你想獲取數(shù)據(jù)庫(kù)中所有的視圖信息,你可以寫如下的查詢語句:
代碼如下:
select * from sysobjects where xtype='v'
如果你想獲取數(shù)據(jù)庫(kù)中所有的觸發(fā)器信息,你可以寫如下的查詢語句:
代碼如下:
select * from sysobjects where xtype='tr'
獲取SQL所有數(shù)據(jù)庫(kù)名、所有表名、所有字段名
1.獲取所有數(shù)據(jù)庫(kù)名:
Select Name FROM Master..SysDatabases orDER BY Name
2.獲取所有表名:
代碼如下:
Select Name FROM DatabaseName..SysObjects Where XType='U' orDER BY Name
XType='U':表示所有用戶表;
XType='S':表示所有系統(tǒng)表;
3.獲取所有字段名:
代碼如下:
Select Name FROM SysColumns Where id=Object_Id('TableName')
方法二。
代碼如下:
select * from sys.databases select * from information_schema.tables select * from information_schema.columns where table_name='tablename'
更多信息請(qǐng)查看IT技術(shù)專欄