Get full SQL for SQL Server data dictionary

Source: Internet
Author: User

SELECT
Sysobjects.name as table name,--------------as function: An alias for the field
--sysproperties. [Value] As table description,----------[] square brackets function: In order to avoid conflicts with keywords
Syscolumns.name as field name,
--properties. [Value] As field description,
Systypes.name as field type,
Syscolumns.length as field length,
ISNULL (ColumnProperty (syscolumns.id, syscolumns.name, ' scale '), 0) as decimal digits,
--isnull---(data to be measured, return value). Returns the return value when the data to be measured is null, back to the data to be measured when the big policy data is non-null
Case when Syscolumns.isnullable=0
Then '
ELSE ' √ '
If END as is empty,
Case if Syscomments.text is NULL
Then ' ELSE syscomments.text
END as default value,
Case when ColumnProperty (Syscolumns.id, Syscolumns.name, ' isidentity ') = 1
Then ' √ ' ELSE '
END as Increment field,
Case if Sysindexes.name is NULL
Then '
ELSE Sysindexes.name
END as index name,
Case if Sysindexkeys.keyno is NULL
Then '
ELSE CONVERT (VARCHAR), Sysindexkeys.keyno
END as index position,
Case when sysindexes.indid=1
Then ' clustered index '
When Sysindexes.indid>1 and sysindexes.indid<>255
Then ' nonclustered index '
When Sysindexes.indid is NULL
Then '
ELSE
Other
END as index type,
Case when EXISTS
(SELECT 1
From sysobjects
WHERE xtype = ' PK ' and name in
(SELECT name
From sysindexes
WHERE Indid in
(SELECT indid
From Sysindexkeys
WHERE id = syscolumns.id and colid = syscolumns.colid)))
Then ' √ ' ELSE '
END as primary key,
Case if Sysforeignkeys.constid is NULL
Then '
ELSE ' √ '
END as external health
From syscolumns--Data table field
INNER JOIN sysobjects--data object
On sysobjects.id = Syscolumns.id
INNER JOIN systypes--Data type
On syscolumns.xtype = Systypes.xtype

Left OUTER JOIN syscomments--Comment information
On syscolumns.cdefault = Syscomments.id
Left OUTER JOIN Sysindexkeys--Information about keys or columns in an index
On sysindexkeys.id = Syscolumns.id
and sysindexkeys.colid = Syscolumns.colid
Left OUTER JOIN sysindexes--Database Index Table
On sysindexes.id = Sysindexkeys.id
and Sysindexes.indid = Sysindexkeys.indid
Left OUTER JOIN Sysforeignkeys
On Sysforeignkeys.fkeyid = Syscolumns.id
and Sysforeignkeys.fkey = Syscolumns.colid
WHERE (sysobjects.xtype = ' U ')
ORDER BY Sysobjects.id,syscolumns.colid

Get full SQL for SQL Server data dictionary

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.