Methods to get all libraries, tables, and field names in SQL Server database

Source: Internet
Author: User

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. 1. Get all user names:
    2. Select name  from Sysusers where status=' 2 ' and islogin=' 1 '
    3. islogin=' 1 ' : Indicates account
    4. islogin=' 0 ' : Indicates a role
    5. status=' 2 ' : Indicates user account
    6. status=' 0 ' : Indicates a system account
    7. 2. Get all database names:
    8. Select Name  from Master.. sysdatabases OrDER by Name
    9. 3. Get all table names:
    10. Select Name  from DatabaseName. SysObjects Where xtype=' U ' orDER by Name
    11. xtype=' U ' : represents all user tables;
    12. xtype=' S ' : denotes all system tables;
    13. 4. Get all field names:
    14. Select Name  from syscolumns Where id=object_id (' table name ')
    15. 5. Get all types of databases:
    16. Select name from systypes
    17. 6. Get the primary key field:
    18. Select name  from syscolumns Where id=object_id (' table name ') and colid= (select top 1 keyno from Sysindexkeys where id=object_id (' table name '))
    19. 7, get the basic information of table field:
    20. Program code
    21. Select
    22. Field name =rtrim (b.name),
    23. Primary key = case if h.id is isn't NULL then ' PK ' ELSE " END,
    24.    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 ,    
    25. Length =b.length,
    26. Allow NULL = case b.isnullable if 0 then ' N ' ELSE ' Y ' END,
    27. Default value =isnull(E.text, "),
    28. Field Description =IsNull(C.value, ")
    29. from sysobjects A, syscolumns b
    30. Left OUTER JOIN sysproperties c on b.id = C.id and b.colid = C.smallid
    31. Left OUTER JOIN syscomments e on b.cdefault = e.id
    32. 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)   
    33. 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
    34. OrDER by B.colid
    35. Program code
    36. Select
    37. Table name = case when A.colorder=1 and then D.name else ' end,
    38. Table Description = case if a.colorder=1 then isnull(F.value,") Else " end,
    39. Field Ordinal =a.colorder,
    40. Field name =a.name,
    41. Identification = case when ColumnProperty (a.id,a.name,' isidentity ') =1 then ' √ ' Else ' end,
    42. Primary KEY = Case whenExistsSelect1 fromsysobjectswhereXtype=' PK ' andnameinch(Selectname fromsysindexesWhereIndidinch(SelectIndid fromSysindexkeysWhereId=a.id andColid=a.colid))) Then' √ 'Else"'End,
    43. Type =b.name,
    44. Field Length =a.length,
    45. Takes up bytes =columnproperty (a.id,a.name,' PRECISION '),
    46. Number of decimal digits =IsNull(ColumnProperty (a.id,a.name,' scale '), 0),
    47. Allow null = case Time a.isnullable=1 then ' √ 'else ' end,
    48. Default value =isnull(E.text,"),
    49. Field Description =isnull(G.[value],")
    50. from Syscolumns a
    51. Left Join systypes b on A.xusertype=b.xusertype
    52. Inner join sysobjects D on (a.id=d.id)and (d.xtype=' U ')and (d.name <>' dtproperties ')
    53. Left Join syscomments e on a.cdefault=e.id
    54. Left join sysproperties G on (a.id=g.id)and (A.colid=g.smallid)
    55. Left join sysproperties F on (d.id=f.id)and (f.smallid=0)
    56. --where d.name= ' table to query '--if you only query the specified table, add this condition
    57. Order by A.id,a.colorder

Methods to get all libraries, tables, and field names in SQL Server database

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.