Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


How do I get the table definition ?


How do I get the table definition ?

Author
Message
mw112009
mw112009
Say Hey Kid
Say Hey Kid (677 reputation)Say Hey Kid (677 reputation)Say Hey Kid (677 reputation)Say Hey Kid (677 reputation)Say Hey Kid (677 reputation)Say Hey Kid (677 reputation)Say Hey Kid (677 reputation)Say Hey Kid (677 reputation)

Group: General Forum Members
Points: 677 Visits: 861
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]
GSquared
GSquared
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14385 Visits: 9729
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
mw112009
mw112009
Say Hey Kid
Say Hey Kid (677 reputation)Say Hey Kid (677 reputation)Say Hey Kid (677 reputation)Say Hey Kid (677 reputation)Say Hey Kid (677 reputation)Say Hey Kid (677 reputation)Say Hey Kid (677 reputation)Say Hey Kid (677 reputation)

Group: General Forum Members
Points: 677 Visits: 861
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 )
Lowell
Lowell
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14984 Visits: 39006
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

--
help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

mw112009
mw112009
Say Hey Kid
Say Hey Kid (677 reputation)Say Hey Kid (677 reputation)Say Hey Kid (677 reputation)Say Hey Kid (677 reputation)Say Hey Kid (677 reputation)Say Hey Kid (677 reputation)Say Hey Kid (677 reputation)Say Hey Kid (677 reputation)

Group: General Forum Members
Points: 677 Visits: 861
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
mw112009
mw112009
Say Hey Kid
Say Hey Kid (677 reputation)Say Hey Kid (677 reputation)Say Hey Kid (677 reputation)Say Hey Kid (677 reputation)Say Hey Kid (677 reputation)Say Hey Kid (677 reputation)Say Hey Kid (677 reputation)Say Hey Kid (677 reputation)

Group: General Forum Members
Points: 677 Visits: 861
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]
Lowell
Lowell
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14984 Visits: 39006
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

--
help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

GSquared
GSquared
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14385 Visits: 9729
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
mw112009
mw112009
Say Hey Kid
Say Hey Kid (677 reputation)Say Hey Kid (677 reputation)Say Hey Kid (677 reputation)Say Hey Kid (677 reputation)Say Hey Kid (677 reputation)Say Hey Kid (677 reputation)Say Hey Kid (677 reputation)Say Hey Kid (677 reputation)

Group: General Forum Members
Points: 677 Visits: 861
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'
george sibbald
george sibbald
SSCertifiable
SSCertifiable (6.4K reputation)SSCertifiable (6.4K reputation)SSCertifiable (6.4K reputation)SSCertifiable (6.4K reputation)SSCertifiable (6.4K reputation)SSCertifiable (6.4K reputation)SSCertifiable (6.4K reputation)SSCertifiable (6.4K reputation)

Group: General Forum Members
Points: 6360 Visits: 13687
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.

---------------------------------------------------------------------
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search