|
|
|
SSC Journeyman
      
Group: General Forum Members
Last Login: Friday, March 01, 2013 12:29 PM
Points: 99,
Visits: 327
|
|
This is another one of my fun little tools that I decided to share. There are times that I need to review the structure of one or (usually) more tables. I need to quickly see all the column names, which are Varchar or Int, and which are Nullable, etc. To do this I made a little tool that makes things go fairly quickly.
Select T.Name As TableName ,C.Name As ColumnName ,ST.Name As DataType ,C.Max_Length ,C.Precision ,C.Scale ,Case When C.Is_Nullable = 1 Then '' When C.Is_Nullable = 0 Then 'Not Null' End As Nullable ,Case When C.Is_Identity = 1 Then 'Identity Field' When C.Is_Identity = 0 Then '' End As Is_Identity From Sys.Tables T Inner Join Sys.Columns C On T.Object_ID = C.Object_ID Inner Join Sys.Types ST On C.User_Type_ID = ST.User_Type_ID Where T.Name = '<First Table Name Here>' ---------------------------------------- Union All ---------------------------------------- Select T.Name ,C.Name ,ST.Name ,C.Max_Length ,C.Precision ,C.Scale ,Case When C.Is_Nullable = 1 Then '' When C.Is_Nullable = 0 Then 'Not Null' End ,Case When C.Is_Identity = 1 Then 'Identity Field' When C.Is_Identity = 0 Then '' End From Sys.Tables T Inner Join Sys.Columns C On T.Object_ID = C.Object_ID Inner Join Sys.Types ST On C.User_Type_ID = ST.User_Type_ID Where T.Name = '<Second Table Name Here...Copy the entire second query and paste for all additional tables>'
You can add additional dynamic SQL steps so you can pass a basic list of tables via a temp table, and cycle through them. I find that while it is doable, this way is usually faster. If I'm working with > 50 tables then I'll add the temp table approach, but usually I don't need to review that many at once, so this works just fine.
Hope you find this useful.
“Any fool can know. The point is to understand.” - Albert Einstein
"DOH!" - Homer Simpson
|
|
|
|
|
SSC Rookie
      
Group: General Forum Members
Last Login: Yesterday @ 6:47 AM
Points: 46,
Visits: 176
|
|
hi ,
you use generate script wizard which is provided in ssms right cllck on database then tasks you will get generate scripts option.
Using this option u can generate scripts of table and other objects also.
|
|
|
|
|
Old Hand
      
Group: General Forum Members
Last Login: Yesterday @ 1:42 PM
Points: 384,
Visits: 543
|
|
... or you could simply use the built-in functionality in SSMS. Highlight the table name, and hit ALT+F1. It will give you all kinds of useful table metadata, including:
- Column data types and lengths - Identity field, seed, increment if present - Filegroup - Index names - Foreign Keys from this table - Foreign Keys to this table
Hakim Ali www.sqlzen.com
|
|
|
|
|
SSC Journeyman
      
Group: General Forum Members
Last Login: Friday, March 01, 2013 12:29 PM
Points: 99,
Visits: 327
|
|
True about the other ways to get this kind of data, and I use both of those fairly frequently as well. This is for those times when I have several tables to review, and I just need the basic break down of each table. This is also mainly posted for a quick reference point for me to code I've used before, and use often.
“Any fool can know. The point is to understand.” - Albert Einstein
"DOH!" - Homer Simpson
|
|
|
|
|
SSChampion
        
Group: General Forum Members
Last Login: Today @ 3:50 PM
Points: 11,645,
Visits: 27,735
|
|
jarid you could streamline the use even further if you put that in a stored procedure.
for example, I created a proc sp_GetDDL which returns the table structure of a table, kind of similar to yours.
now, by adding it to SSMS keyboard shortcuts, I can double click on an object name, click CONTROL + 3 and get the results.
so say i'm in the middle of writing a view or a proc, and I need some at-a-glance list of the columns like you suggest...simply highlight the objectname, do the keyboard shortcut, and poof.
something helpful like that increases productivity for me.
the first screenshot is from something similar, sp_find, which just searches tables and columns that partially match some string i highlight.

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
|
|
|
|