Querying table structures, indexes, views, stored procedures, triggers, and custom functions in a database in SQLServer2005

Source: Internet
Author: User

querying table structures, indexes, views, stored procedures, triggers, and custom functions in a database in SQLServer2005

2013-03-11 09:05:06| Category: SQL SERVER| Report | Font size Subscription

(1) Querying the structure of a table in a database in SQLServer2005

SELECT TOP PERCENT--a.id,
case if A.colorder = 1 then d.name ELSE ' END as table name,
case if A.colorder = 1 then IsNull (F.value, ') ELSE ' END as table description,
A.colorder as field ordinal, a.name as field name, case when ColumnProperty (a.ID,
A.name, ' isidentity ') = 1 Then ' √ ' ELSE ' END as identification,
Case when EXISTS
(SELECT 1
From dbo.sysindexes si INNER JOIN
Dbo.sysindexkeys sik on si.id = sik.id and Si.indid = Sik.indid INNER JOIN
Dbo.syscolumns sc on sc.id = sik.id and sc.colid = Sik.colid INNER JOIN
Dbo.sysobjects so on so.name = si.name and So.xtype = ' PK '
WHERE sc.id = a.id and sc.colid = a.colid) Then ' √ ' ELSE ' END as primary key,
B.name as type, a.length as length, ColumnProperty (a.id, A.name, ' PRECISION ')
As precision, ISNULL (ColumnProperty (a.id, a.name, ' scale '), 0) as decimal digits,
Case Time a.isnullable = 1 Then ' √ ' ELSE ' END as allow null, ISNULL (E.text, ')
As default value, ISNULL (G.[value], ') As field description, d.crdate as creation time,
case if A.colorder = 1 then d.refdate ELSE NULL END as Change time
From Dbo.syscolumns a left OUTER JOIN
Dbo.systypes b on a.xtype = B.xusertype INNER JOIN
Dbo.sysobjects D on a.id = d.id and D.xtype = ' U ' and
D.status >= 0 left OUTER JOIN
Dbo.syscomments e on a.cdefault = e.id left OUTER JOIN
Dbo.sysproperties g on a.id = g.id and a.colid = G.smallid and
G.name = ' ms_description ' left OUTER JOIN
Dbo.sysproperties F on d.id = f.id and f.smallid = 0 and
F.name = ' ms_description '
ORDER by D.name, A.colorder
PS: If there are no dbo.sysproperties tables in the database, then only the comments associated with them can be dropped.
The simplest SQL to query the user table is as follows :
SELECT * FROM Sys.tables
(2) Querying the index in a database in SQLServer2005
SELECT TOP PERCENT--a.id,
case if B.keyno = 1 then c.name ELSE ' END as table name,
case if B.keyno = 1 then a.name ELSE ' END as index name, d.name as column name,
B.keyno as index order, case Indexkey_property (C.id, B.indid, B.keyno, ' isdescending ')
When the 1 Then ' descending ' when 0 Then ' ascending ' END as sort, case if P.id is NULL
Then ' ELSE ' √ ' END as primary key, Case Indexproperty (c.id, A.name, ' isclustered ')
When 1 Then ' √ ' if 0 Then ' END as clustered, Case Indexproperty (C.id,
A.name, ' IsUnique ') when 1 Then ' √ ' if 0 Then ' END as unique,
Case when E.id was NULL then ' ELSE ' √ ' END as Unique constraint,
A.origfillfactor as fill factor, c.crdate as creation time, c.refdate as change time
From Dbo.sysindexes a INNER JOIN
Dbo.sysindexkeys b on a.id = b.id and A.indid = B.indid INNER JOIN
Dbo.syscolumns D on b.id = d.id and b.colid = D.colid INNER JOIN
Dbo.sysobjects C on a.id = c.id and C.xtype = ' U ' left OUTER JOIN
Dbo.sysobjects e on e.name = a.name and E.xtype = ' UQ ' left OUTER JOIN
Dbo.sysobjects p on p.name = a.name and P.xtype = ' PK '
WHERE (ObjectProperty (a.id, N ' isusertable ') = 1) and (ObjectProperty (a.ID,
N ' ismsshipped ') = 0) and (Indexproperty (a.id, A.name, ' isautostatistics ') = 0)
ORDER by C.name, A.name, B.keyno
(3) Querying for primary keys, foreign keys, constraints, views, stored procedures, triggers, and custom functions in a database in SQLServer2005
 
ORDER by Case O.xtype if ' X ' then ' extended stored procedure ' when ' TR ' then ' Trigger ' when
' PK ' then ' primary key ' when ' F ' then ' Foreign key ' when ' C ' then ' constraint ' when ' V ' then ' View '
when ' FN ' then ' function-scalar ' when ' IF ' then ' function-inline ' when ' TF ' then ' function-table value '
ELS E ' stored procedure ' END DESC
(4) Querying all diagrams in a database in SQLServer2005
SELECT * FROM Dbo.sysdiagrams
(5) Querying all objects in a database in SQLServer2005
SELECT * FROM Sys.sysobjects
Different objects are identified by Xtype.
C = CHECK Constraint
D = defaults or DEFAULT constraints
F = FOREIGN KEY constraint
L = Log
FN = Scalar function
IF = Inline Table function
P = Stored Procedure
PK = PRIMARY KEY constraint (type is K)
RF = copy Filter stored procedure
S = System table
TF = Table function
TR = Trigger
U = User Table
UQ = UNIQUE constraint (type is K)
V = view
X = Extended Stored Procedure
This information can be viewed by using the index->sysobjects->xtype Books Online.

Querying table structures, indexes, views, stored procedures, triggers, and custom functions in a database in SQLServer2005

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.