標籤:style blog http color io ar strong 資料 sp
引自:http://blog.sina.com.cn/s/blog_7c0e26230100x2zl.html
1.擷取所有資料庫名:
SELECT Name from Master..SysDatabases ORDER BY Name
2.擷取所有表名:
SELECT Name from DatabaseName..SysObjects Where XType=‘U‘ ORDER BY Name
XType=‘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
擷取sqlserver資料庫中所有庫、表、欄位名的方法