|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Tuesday, May 21, 2013 12:41 PM
Points: 174,
Visits: 247
|
|
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]
|
|
|
|
|
SSCoach
         
Group: General Forum Members
Last Login: Tuesday, May 21, 2013 1:55 PM
Points: 15,442,
Visits: 9,571
|
|
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
|
|
|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Tuesday, May 21, 2013 12:41 PM
Points: 174,
Visits: 247
|
|
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 )
|
|
|
|
|
SSChampion
        
Group: General Forum Members
Last Login: Today @ 3:22 PM
Points: 11,648,
Visits: 27,757
|
|
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
|
|
|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Tuesday, May 21, 2013 12:41 PM
Points: 174,
Visits: 247
|
|
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
|
|
|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Tuesday, May 21, 2013 12:41 PM
Points: 174,
Visits: 247
|
|
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]
|
|
|
|
|
SSChampion
        
Group: General Forum Members
Last Login: Today @ 3:22 PM
Points: 11,648,
Visits: 27,757
|
|
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
|
|
|
|
|
SSCoach
         
Group: General Forum Members
Last Login: Tuesday, May 21, 2013 1:55 PM
Points: 15,442,
Visits: 9,571
|
|
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
|
|
|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Tuesday, May 21, 2013 12:41 PM
Points: 174,
Visits: 247
|
|
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'
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Today @ 3:17 PM
Points: 5,270,
Visits: 11,212
|
|
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.
---------------------------------------------------------------------
|
|
|
|