December 4, 2008 at 2:09 pm
Is there some kind of simple syntax that I can run in order to see what fields, their names, Nullable or not, Data type and Max lenth?
I'd find it very useful sometimes instead of looking at the table properties.
thx,
John
December 4, 2008 at 2:23 pm
sp_help 'tbl'
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
December 4, 2008 at 3:20 pm
This will list all tables and all columns in each table (And the gods of T-SQL preserve me - it uses a cursor - not the best practice, but I wrote this one a long long time ago)
CREATE PROCEDURE dbo.UDP_Table_Definitions
AS
Declare @default VARCHAR(128)
Declare @tname VARCHAR(128)
Declare @cname VARCHAR(128)
Declare @dtype VARCHAR(30)
Declare @dlength INT
Declare @sstatus CHAR(3)
Declare @xcomp CHAR(3)
Declare @xdef INT
SET NOCOUNT ON
Create Table #UDT_TDefs
(
TName VARCHAR(128),
CName VARCHAR(128),
DType VARCHAR(30),
Dlength INT,
Sstatus CHAR(3),
xComp CHAR(3),
xDef VarChar(30)
)
DECLARE table_cursor CURSOR fast_forward FOR
SELECT so.name, sc.name, st.name, sc.length, sc.Status, sc.iscomputed, sc.cdefault
From Sysobjects so, syscolumns sc, systypes st
where so.xtype = 'U' and sc.id = so.id and sc.xtype = st.xusertype AND NOT so.name = 'dtproperties'
Open Table_Cursor
Fetch Next from Table_Cursor into @tname, @cname, @dtype, @dlength, @sstatus, @xcomp, @xdef
While @@Fetch_Status = 0
Begin
Set @Default = ' '
If @xdef > 0
Set @Default = (Select text from syscomments where @xdef = id)
Insert Into #UDT_TDefs
(TName, CName, DType, Dlength, Sstatus, xComp, xDef)
Values (@tname, @cname, @dtype, @dlength, @sstatus, @xcomp, @Default )
Fetch Next from Table_Cursor into @tname, @cname, @dtype, @dlength, @sstatus, @xcomp, @xdef
End
Close Table_Cursor
Deallocate Table_Cursor
Select
tname As 'Table Name', cname as 'Column Name', DType as 'Data Type', dlength as 'Len', sStatus as 'NP1', xComp as 'NP2', xdef as 'Default',
Case tname When 'int' then Cast(dlength as Char(6)) Else ' ' end As 'Precision',
Case sStatus When 8 then 'Yes' When 24 then 'Yes' When 56 then 'Yes' Else ' ' end As 'Allow Nulls',
Case sStatus When 128 then 'Yes' Else ' ' end As 'Identity',
Case xcomp When 1 then 'Yes' Else ' ' end As 'Computed'
From #UDT_TDefs
Order by tname
Drop table #UDT_TDefs
It also uses: Sysobjects, syscolumns, systypes which are being depreciated so for use beyond 2005 switch to the new system views per BOL ms-help://MS.SQLCC.v9/MS.SQLSVR.v9.en/tsqlref9/html/44fdc387-67b0-4139-8bf5-ed26cf640cd1.htm
December 4, 2008 at 3:23 pm
I'll try both, thx.
December 4, 2008 at 7:23 pm
bitbucket (12/4/2008)
And the gods of T-SQL preserve me
Pickle juice or formaldehyde? With or without pork chops? 😛
--Jeff Moden
Change is inevitable... Change for the better is not.
December 4, 2008 at 10:57 pm
Select * From INFORMATION_SCHEMA.COLUMNS
[font="Times New Roman"]-- RBarryYoung[/font], [font="Times New Roman"] (302)375-0451[/font] blog: MovingSQL.com, Twitter: @RBarryYoung[font="Arial Black"]
Proactive Performance Solutions, Inc. [/font][font="Verdana"] "Performance is our middle name."[/font]
December 5, 2008 at 7:14 am
Jeff
Pickle juice or formaldehyde? With or without pork chops?
Worked on that SP long before I hand the pleasure and enlightenment of reading your articles, and learning from them. Just did not have the time to redo it since it was written for SQL 2000. I stand chastised.
December 6, 2008 at 8:09 am
bitbucket (12/5/2008)
JeffPickle juice or formaldehyde? With or without pork chops?
Worked on that SP long before I hand the pleasure and enlightenment of reading your articles, and learning from them. Just did not have the time to redo it since it was written for SQL 2000. I stand chastised.
Heh... oh, no problem, ol' friend... Your post just struck me as a bit ironic and I was just having a bit of fun. 😀 You were rough enough on yourself than for me to pick on you. Besides, we all started somewhere... I remember how I used to code SQL when I first started... one huge query for each batch project... it was actually worse than any cursor anyone will ever write. :hehe:
--Jeff Moden
Change is inevitable... Change for the better is not.
December 6, 2008 at 8:28 am
Jeff Moden (12/6/2008)
I remember how I used to code SQL when I first started... one huge query for each batch project... it was actually worse than any cursor anyone will ever write. :hehe:
Can you clarify this a little bit? You're making me worry about my 600-1000+ line queries :hehe:.
Viewing 9 posts - 1 through 9 (of 9 total)
You must be logged in to reply to this topic. Login to reply