Two views of check constraint information in the query library (SQL SERVER)

Source: Internet
Author: User
Tags join sql query table name
server| View
The following two views are based on system table sysobjects, syscomments, and system view sysconstraints, including the table ID, table name, column ID, column name, check constraint ID, check constraint name, The check constraint status value and the contents of the check constraint, Tccview is Table-column-check view, the result is a column-level check constraint, Tcview is Table-check view, and the result is a table-level check constraint.

For the role of the value of the field status, see the article about the property settings for a check constraint without Enterprise Manager.

The following is a two-view code that can be used in any database in a SQL SERVER 2000 database.

Tccview:
SELECT top PERCENT a.id as TableID, A.tablename, A.colid, A.columnname,
A.datatype, A.length, B.constid as Checkid, B.checkname, B.status, b.content
From (SELECT sysobjects.name as TableName, Sysobjects.id,
Syscolumns.name as ColumnName, Syscolumns.colid,
Systypes.name as datatype, syscolumns.length as length
From sysobjects, syscolumns, systypes
WHERE sysobjects.xtype = ' u ' and sysobjects.id = Syscolumns.id and
Syscolumns.xtype = Systypes.xtype and
Systypes.xtype = Systypes.xusertype and sysobjects.status > 0)
A left OUTER JOIN
(SELECT sysobjects.name checkname, Sysobjects.status, Sysconstraints.constid,
Sysconstraints.id, Sysconstraints.colid, syscomments.text as content
From sysobjects, sysconstraints, syscomments
WHERE xtype = ' C ' and sysobjects.id = Sysconstraints.constid and
Sysconstraints.constid = syscomments.id) b on a.id = b.ID and
A.colid = B.colid
Order by A.tablename, A.columnname, B.checkname

Tcview:
SELECT a.id as TableID, A.tablename, B.constid as Checkid, B.checkname, B.status,
B.content
From (SELECT sysobjects.id, sysobjects.name as TableName
From sysobjects
WHERE sysobjects.xtype = ' u ' and sysobjects.status > 0) a left OUTER JOIN
(SELECT sysobjects.name checkname, Sysobjects.status, Sysconstraints.constid,
Sysconstraints.id, Sysconstraints.colid, syscomments.text as content
From sysobjects, sysconstraints, syscomments
WHERE xtype = ' C ' and sysobjects.id = Sysconstraints.constid and
Sysconstraints.constid = syscomments.id and sysconstraints.colid = 0) b on
a.ID = b.ID


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.