SQL server queries all the table names and fields in the database.

Source: Internet
Author: User
Tags sql server query

SQL server queries all the table names and fields in the database.

SELECT * FROM INFORMATION_SCHEMA.columns WHERE TABLE_NAME = 'account'

SELECT
(Case when a. colorder = 1 then d. name else ''end) as table name. -- if the table name is the same, null is returned.
A. colorder as field serial number,
A. name as field name,
(Case when COLUMNPROPERTY (a. id, a. name, 'isidentity ') = 1 then' √ 'else' end) as identity,
(Case when (SELECT count (*) FROM sysobjects -- query the primary key
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) as primary key, -- Query primary key END
B. name as type,
A. length as occupies the number of bytes,
COLUMNPROPERTY (a. id, a. name, 'precision ') as length,
Isnull (COLUMNPROPERTY (a. id, a. name, 'Scale'), 0) as decimal places,
(Case when a. isnullable = 1 then '√ 'else'' end) as allow null,
Isnull (e. text, '') as default value,
Isnull (g. [value], '') AS field description
FROM syscolumns a 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
Order by a. id, a. colorder


How does SQL server query all tables? Not all tables are named.

Select name from sysobjects where xtype = 'U ';

How can I query all the table names and field types in a specified database in SQL Server?

There is a set of system views below sys. You can find all the data you need through those views ~~
 

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.