Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SQK Server table definition 1

Status
Not open for further replies.

kel1981b

Programmer
Jan 3, 2003
82
US
Is there a way to export the layout definitions of each table of particular database to some type file that I could then import into an Excel sheet?
 
Here's one to show the tables & colums:

[tt]
SELECT dbo.sysobjects.name AS TableName, dbo.syscolumns.name AS ColName,
dbo.syscolumns.colid AS ColOrder, dbo.systypes.name,
dbo.syscolumns.length, dbo.syscolumns.prec, dbo.syscolumns.isnullable
FROM dbo.sysobjects INNER JOIN
dbo.syscolumns ON dbo.sysobjects.id = dbo.syscolumns.id LEFT OUTER JOIN
dbo.systypes ON dbo.syscolumns.xtype = dbo.systypes.xtype
WHERE OBJECTPROPERTY(OBJECT_ID(dbo.sysobjects.name), 'ISTABLE') = 1
ORDER BY dbo.sysobjects.name, dbo.syscolumns.colid
[/tt]

and here's one for tables & indexes:

[tt]
SELECT dbo.sysobjects.name AS TableName, dbo.sysindexes.name AS IndexName,
dbo.sysindexkeys.keyno, dbo.syscolumns.name,
CASE WHEN dbo.sysindexes.status & 2 = 2 THEN 'Yes'
ELSE 'No'
END AS IsUnique,
CASE WHEN dbo.sysindexes.status & 16 = 16 THEN 'Yes'
ELSE 'No'
END AS IsClustered
FROM dbo.sysindexes INNER JOIN
dbo.sysobjects ON dbo.sysindexes.id = dbo.sysobjects.id INNER JOIN
dbo.sysindexkeys ON dbo.sysindexes.id = dbo.sysindexkeys.id
AND dbo.sysindexes.indid = dbo.sysindexkeys.indid INNER JOIN
dbo.syscolumns ON dbo.sysindexkeys.colid = dbo.syscolumns.colid
AND dbo.sysindexkeys.id = dbo.syscolumns.id
WHERE (LEFT(dbo.sysindexes.name, 3) <> '_WA'
AND LEFT(dbo.sysindexes.name, 3) <> 'hin')
AND (dbo.sysobjects.xtype <> 'S')
ORDER BY dbo.sysobjects.name, dbo.sysindexes.indid, dbo.sysindexkeys.keyno
[/tt]
 
Here's one to show the tables & colums:

[tt]
SELECT dbo.sysobjects.name AS TableName, dbo.syscolumns.name AS ColName,
dbo.syscolumns.colid AS ColOrder, dbo.systypes.name,
dbo.syscolumns.length, dbo.syscolumns.prec, dbo.syscolumns.isnullable
FROM dbo.sysobjects INNER JOIN
dbo.syscolumns ON dbo.sysobjects.id = dbo.syscolumns.id LEFT OUTER JOIN
dbo.systypes ON dbo.syscolumns.xtype = dbo.systypes.xtypea
WHERE OBJECTPROPERTY(OBJECT_ID(dbo.sysobjects.name), 'ISTABLE') = 1
ORDER BY dbo.sysobjects.name, dbo.syscolumns.colid
[/tt]

and here's one for tables & indexes:

[tt]
SELECT dbo.sysobjects.name AS TableName, dbo.sysindexes.name AS IndexName,
dbo.sysindexkeys.keyno, dbo.syscolumns.name,
CASE WHEN dbo.sysindexes.status & 2 = 2 THEN 'Yes'
ELSE 'No'
END AS IsUnique,
CASE WHEN dbo.sysindexes.status & 16 = 16 THEN 'Yes'
ELSE 'No'
END AS IsClustered
FROM dbo.sysindexes INNER JOIN
dbo.sysobjects ON dbo.sysindexes.id = dbo.sysobjects.id INNER JOIN
dbo.sysindexkeys ON dbo.sysindexes.id = dbo.sysindexkeys.id
AND dbo.sysindexes.indid = dbo.sysindexkeys.indid INNER JOIN
dbo.syscolumns ON dbo.sysindexkeys.colid = dbo.syscolumns.colid
AND dbo.sysindexkeys.id = dbo.syscolumns.id
WHERE (LEFT(dbo.sysindexes.name, 3) <> '_WA'
AND LEFT(dbo.sysindexes.name, 3) <> 'hin')
AND (dbo.sysobjects.xtype <> 'S')
ORDER BY dbo.sysobjects.name, dbo.sysindexes.indid, dbo.sysindexkeys.keyno
[/tt]
 
Thanks a lot! It dies work! You help me a lot! [thumbsup]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top