Obtain the names of all tables in a database and the names of all fields in a table in SQL.

Source: Internet
Author: User
This article introduces how to obtain the names of all tables in a database and the names of all fields in a table. For more information, see.

This article introduces how to obtain the names of all tables in a database and the names of all fields in a table. For more information, see.

1. All Database names in the query:

The Code is as follows:
SELECT Name FROM Master... SysDatabases order by Name2.


Query all the tables in a database:

The Code is as follows:
SELECT Name FROM SysObjects Where XType = 'U' order by Name3.

Query table structure information

The Code is as follows:

SELECT (case when a. colorder = 1 then d. name else null end) Table name,
A. colorder FIELD No., a. name field name,
(Case when COLUMNPROPERTY (a. id, a. name, 'isidentity ') = 1 then' √ 'else' end) id,
(Case when (SELECT (*) FROM sysobjects
WHERE (name in (SELECT name FROM sysindexes
WHERE (id = a. id) AND (indid in
(SELECT indid FROM sysindexkeys
WHERE (id = a. id) AND (colid in
(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 the number of bytes,
COLUMNPROPERTY (a. id, a. name, 'precision ') as length,
Isnull (COLUMNPROPERTY (. id,. name, 'Scale'), 0) as decimal places, (case when. isnullable = 1 then' √ 'else' end) Allow null,
Isnull (e. text, '') default value, isnull (g. [value],'') AS [description]
FROM syscolumns
Left join policypes B on a. xtype = 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 sys. extended_properties g on a. id = g. major_id AND a. colid = g. minor_id
Left join sys. extended_properties f on d. id = f. class and f. minor_id = 0
Where B. name is not null
-- WHERE d. name = 'table to be query' -- this condition is added if only the specified table is queried.
Order by a. id, a. colorder

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.