How SQL Server gets the names of all the tables in a database, the names of all the fields in a table

Source: Internet
Author: User

1. Query all database names in the database:

SELECT Name from Master. sysdatabases ORDER by Name

2. Query all the table names in a database:

SELECT name from SysObjects Where xtype= ' U ' ORDER by Name

3. Query table structure information:

 1 SELECT (case if A.colorder=1 then d.name else null end) Table name, 2 a.colorder field ordinal, a.name field name, 3  TY (a.id,a.name, ' isidentity ') =1 then ' √ ' else ' end) identifier, 4 (SELECT count (*) from sysobjects 5 WHERE (name in (select name from sysindexes 6 where (id = a.id) and (Indid in 7 (select Indid from Sysindexkeys 8 where (id = a.id  ) and (Colid in 9 (SELECT colid from syscolumns WHERE (id = a.id) and (name = A.name)))))) and (xtype = ' PK ') ">0 Then ' √ ' else ' end) primary key, B.name type, A.length occupies bytes, one columnproperty (a.id,a.name, ' PRECISION ') as length, IsNull (COLUMNPR Operty (a.id,a.name, ' scale '), 0) as decimal place, (case time a.isnullable=1 then ' √ ' else ' end) allow null, IsNull (E.text, ') default value, Isnul L (G.[value], ') as [description]14 from syscolumns A and left join Systypes B on a.xtype=b.xusertype inner join sysobjects D On a.id=d.id and d.xtype= ' U ' and d.name<> ' dtproperties ' of left joins syscomments E on a.cdefault=e.id-left join Sys.extended_propertiesG on a.id=g.major_id and A.colid=g.minor_id19 left join Sys.extended_properties F on D.id=f.class and f.minor_id=020 where B.name is not null21--where d.name= ' table to query '--if only the specified table is queried, add this condition to the order by A.id,a.colorder

How SQL Server gets the names of all the tables in a database, the names of all the fields in a table

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.