Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 123»»»

How do I get the table definition ? Expand / Collapse
Author
Message
Posted Wednesday, January 4, 2012 1:50 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Monday, January 27, 2014 9:53 AM
Points: 177, Visits: 250
Team:
Is there a way for me to get the definition of a table via a sql statement.
Now I was able to get the def of a stored proc using the following SQL

[color=#CC73CC]
select
sc.name + '.' + o.[name] as name,
[definition] as def
from
sys.sql_modules AS sm JOIN sys.objects AS o
on o.object_id = sm.object_id
AND o.[type] in ( 'P' )
INNER JOIN sys.schemas sc
on ( sc.schema_id = o.schema_id )
ORDER BY sc.name + '.' + o.[name]
[/color]
Post #1230310
Posted Wednesday, January 4, 2012 1:53 PM


SSCoach

SSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoach

Group: General Forum Members
Last Login: Friday, June 27, 2014 12:43 PM
Points: 15,444, Visits: 9,596
You can get most of a table definition from sys.tables, sys.columns, sys.indexes, sys.constraints, sys.ImForgettingSomethingButDontRememberWhatItIs.

Play around with those a bit, and you can get enough to generate a Create Table script, if that's what you're looking for.


- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
Property of The Thread

"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
Post #1230313
Posted Wednesday, January 4, 2012 2:05 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Monday, January 27, 2014 9:53 AM
Points: 177, Visits: 250
I know of I play around I will get it.
But then what I wanted is for a "GURU" to just throw the SQL statement for me ( Ha Ha )
Post #1230325
Posted Wednesday, January 4, 2012 2:07 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 8:24 AM
Points: 12,887, Visits: 31,835
via TSQL, it's one of those really difficult things to do on your own.
Like Gus said, all the data is out there in the sys.tables/sys.columns metadata views, but it's up to you to pull it all together.

look at this script contribution i wrote, and go to it's forum link for the most updated version
Get DDL for Any SQL 2005 Table


I've got versions for 2008 also, which additionally scripts 2008+ only items, like FilteredColumns and SPARSE datacolumns as well.


Lowell

--There is no spoon, and there's no default ORDER BY in sql server either.
Actually, Common Sense is so rare, it should be considered a Superpower. --my son
Post #1230327
Posted Wednesday, January 4, 2012 2:18 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Monday, January 27, 2014 9:53 AM
Points: 177, Visits: 250
One way to do it is to install the following stored proc written by a GURU

http://www.techrepublic.com/blog/datacenter/script-table-definitions-using-tsql/431

Post #1230336
Posted Wednesday, January 4, 2012 2:20 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Monday, January 27, 2014 9:53 AM
Points: 177, Visits: 250
or can some one help me here with this antiquated query.
This works fine. Just that it does not use sys.objects or sys.columns.

what i need is to be able to join with sys.schemas so that i can add the
schema name to the begining of the table name.

SELECT a.[name] as 'Table',
b.[name] as 'Column',
c.[name] as 'Datatype',
b.[length] as 'Length',
CASE
WHEN b.[cdefault] > 0 THEN d.[text]
ELSE NULL
END as 'Default',
CASE
WHEN b.[isnullable] = 0 THEN 'No'
ELSE 'Yes'
END as 'Nullable'
FROM sysobjects a
INNER JOIN syscolumns b
ON a.[id] = b.[id]
INNER JOIN systypes c
ON b.[xtype] = c.[xtype]
LEFT JOIN syscomments d
ON b.[cdefault] = d.[id]
WHERE a.[xtype] = 'u'
-- 'u' for user tables, 'v' for views.
--and a.[name]='table name'
AND a.[name] <> 'dtproperties'
ORDER BY a.[name],b.[colorder]
Post #1230341
Posted Wednesday, January 4, 2012 2:24 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 8:24 AM
Points: 12,887, Visits: 31,835
sysobjects :
you can either use SCHEMA_NAME(sys.objects.
object_id) from sys.objects

OR
JOIN sys.schemas ON sys.objects.schema_id = sys.schemas.schema_id)


Lowell

--There is no spoon, and there's no default ORDER BY in sql server either.
Actually, Common Sense is so rare, it should be considered a Superpower. --my son
Post #1230347
Posted Wednesday, January 4, 2012 2:29 PM


SSCoach

SSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoach

Group: General Forum Members
Last Login: Friday, June 27, 2014 12:43 PM
Points: 15,444, Visits: 9,596
mw112009 (1/4/2012)
or can some one help me here with this antiquated query.
This works fine. Just that it does not use sys.objects or sys.columns.

what i need is to be able to join with sys.schemas so that i can add the
schema name to the begining of the table name.

SELECT a.[name] as 'Table',
b.[name] as 'Column',
c.[name] as 'Datatype',
b.[length] as 'Length',
CASE
WHEN b.[cdefault] > 0 THEN d.[text]
ELSE NULL
END as 'Default',
CASE
WHEN b.[isnullable] = 0 THEN 'No'
ELSE 'Yes'
END as 'Nullable'
FROM sysobjects a
INNER JOIN syscolumns b
ON a.[id] = b.[id]
INNER JOIN systypes c
ON b.[xtype] = c.[xtype]
LEFT JOIN syscomments d
ON b.[cdefault] = d.[id]
WHERE a.[xtype] = 'u'
-- 'u' for user tables, 'v' for views.
--and a.[name]='table name'
AND a.[name] <> 'dtproperties'
ORDER BY a.[name],b.[colorder]


That's an SQL 2000 version, and it's a human-readable description of a table and its columns, not a script for the table. It will need to be updated for SQL 2005 and beyond, if a description is even what's needed.

Honestly, if you want human-readable table documentation, which is what that script tries to do, I'd use RedGate's SQL Doc product instead. A lot more features and uses.


- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
Property of The Thread

"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
Post #1230354
Posted Wednesday, January 4, 2012 2:53 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Monday, January 27, 2014 9:53 AM
Points: 177, Visits: 250
So far managed to come up with this.
I think I this may be sufficient for now. It may not give the script. But has the info I need.

SELECT d.name + '.' + a.[name] as 'Table',c.name,
CASE WHEN
c.name in ( 'varchar', 'nvarchar','ntext','char' ) THEN b.max_length
ELSE
NULL
END
as [LENGTH],
CASE WHEN b.is_nullable= 1 THEN 'NULL' ELSE 'NOT NULL' END as NULLABLE
FROM sys.objects a
INNER JOIN sys.columns b
ON a.[object_id] = b.[object_id]
INNER JOIN sys.types c
ON c.[system_type_id] = b.[system_type_id]
INNER JOIN sys.schemas d on ( d.schema_id = a.schema_id )
WHERE
a.name like 'fin_dashboard_visit_counts'
Post #1230367
Posted Wednesday, January 4, 2012 2:56 PM
SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: 2 days ago @ 1:21 PM
Points: 5,975, Visits: 12,883
am I missing something here? whats wrong with sp_help?

you can look at the code in sp_help to see how it's done if you only want a subset of what sp_help gives you.


---------------------------------------------------------------------

Post #1230369
« Prev Topic | Next Topic »

Add to briefcase 123»»»

Permissions Expand / Collapse