Citation: http://blog.sina.com.cn/s/blog_7c0e26230100x2zl.html
1. Get all database names:
SELECT Name from Master. sysdatabases ORDER by Name
2. Get all table names:
SELECT Name from DatabaseName. SysObjects Where xtype= ' U ' ORDER by Name
Xtype= ' U ': represents all user tables;
Xtype= ' S ': denotes all system tables;
3. Get all field names:
SELECT Name from syscolumns WHERE id=object_id (' TableName ')
Access:
If a table exists, how do I get this table?
SELECT * from msysobjects where name= ' examresulttime ' and type=1 and flags=0
Get all the table names in the database
SELECT name from Msysobjects where type=1 and flags=0
- 1. Get all user names:
- Select name from Sysusers where status=' 2 ' and islogin=' 1 '
- islogin=' 1 ' : Indicates account
- islogin=' 0 ' : Indicates a role
- status=' 2 ' : Indicates user account
- status=' 0 ' : Indicates a system account
- 2. Get all database names:
- Select Name from Master.. sysdatabases OrDER by Name
- 3. Get all table names:
- Select Name from DatabaseName. SysObjects Where xtype=' U ' orDER by Name
- xtype=' U ' : represents all user tables;
- xtype=' S ' : denotes all system tables;
- 4. Get all field names:
- Select Name from syscolumns Where id=object_id (' table name ')
- 5. Get all types of databases:
- Select name from systypes
- 6. Get the primary key field:
- Select name from syscolumns Where id=object_id (' table name ') and colid= (select top 1 keyno from Sysindexkeys where id=object_id (' table name '))
- 7, get the basic information of table field:
- Program code
- Select
- Field name =rtrim (b.name),
- Primary key = case if h.id is isn't NULL then ' PK ' ELSE " END,
- field type =type_name (b.xusertype) +case when b.colstat&1=1 then + convert ( varchar , ident_seed (A. name )) + +convert ( varchar , IDENT_INCR (A. name )) + ')] ' else " end ,
- Length =b.length,
- Allow NULL = case b.isnullable if 0 then ' N ' ELSE ' Y ' END,
- Default value =isnull(E.text, "),
- Field Description =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 (' table to query ')) --The table you want to query is changed to the name of the table you want to query
- OrDER by B.colid
- Program code
- Select
- Table name = case when A.colorder=1 and then D.name else ' end,
- Table Description = case if a.colorder=1 then isnull(F.value,") Else " end,
- Field Ordinal =a.colorder,
- Field name =a.name,
- Identification = case when ColumnProperty (a.id,a.name,' isidentity ') =1 then ' √ ' Else ' end,
- Primary KEY = Case whenExistsSelect1 fromsysobjectswhereXtype=' PK ' andnameinch(Selectname fromsysindexesWhereIndidinch(SelectIndid fromSysindexkeysWhereId=a.id andColid=a.colid))) Then' √ 'Else"'End,
- Type =b.name,
- Field Length =a.length,
- Takes up bytes =columnproperty (a.id,a.name,' PRECISION '),
- Number of decimal digits =IsNull(ColumnProperty (a.id,a.name,' scale '), 0),
- Allow null = case Time a.isnullable=1 then ' √ 'else ' end,
- Default value =isnull(E.text,"),
- Field Description =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= ' table to query '--if you only query the specified table, add this condition
- Order by A.id,a.colorder
Methods to get all libraries, tables, and field names in SQL Server database