January 4, 2012 at 1:50 pm
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
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]
January 4, 2012 at 1:53 pm
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
January 4, 2012 at 2:05 pm
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 )
January 4, 2012 at 2:07 pm
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
January 4, 2012 at 2:18 pm
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
January 4, 2012 at 2:20 pm
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]
January 4, 2012 at 2:24 pm
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
January 4, 2012 at 2:29 pm
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
January 4, 2012 at 2:53 pm
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'
January 4, 2012 at 2:56 pm
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.
---------------------------------------------------------------------
January 5, 2012 at 6:41 am
george sibbald (1/4/2012)
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.
Nothing at all wrong with sp_help. Just depends on what you're looking for in terms of table definition results.
- 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
January 5, 2012 at 7:42 am
sp_help 'Table Name' throws out many layers of information. This would be ideal for just checking one table at a time just to get information.
What we need is a way to get the "CREATE TABLE ABC ( mo VARCHAR(10) )" definition in text form.
This way, via SSIS, I can save all definitions to a text file.
January 9, 2012 at 6:33 am
Is it absolutely necessary to have SSIS output them for you? If not, you can use the object-scripting wizard in SSMS to generate table scripts any time you need to.
Right-click the database in the object browser, Task->Script Database, select the options you want and output to a text file. Usually a pretty fast and easy task.
- 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
January 9, 2012 at 6:56 am
Agreed!
However, this gives us all the tables. Not a problem.
My next question is, can this task be automated via a SSIS ?
What I mean is can I have a job that runs everyday at 6 PM and have the definitions file being written to
some directory ?
January 9, 2012 at 7:15 am
My next question is, can this task be automated via a SSIS ?
What I mean is can I have a job that runs everyday at 6 PM and have the definitions file being written to
some directory ?
It's possible. How? You need to spend some time understanding SSIS data flows, if you are less familiar with it.
Viewing 15 posts - 1 through 15 (of 23 total)
You must be logged in to reply to this topic. Login to reply