-
SELECT table field names
I need a statement or sp that will display, for a given user database, an individual table's fieldnames, datatype, and length. Any help is appreciated.
Randy
-
SELECT table field names (reply)
Select from information schema view columns.
------------
Randy at 11/14/01 1:27:17 PM
I need a statement or sp that will display, for a given user database, an individual table's fieldnames, datatype, and length. Any help is appreciated.
Randy
-
SELECT table field names (reply)
Thanks Ray. I tried that but I must be getting the syntax wrong. Can you post a sample for a table called MATRIX?
Randy
------------
Ray Miao at 11/14/2001 1:32:32 PM
Select from information schema view columns.
------------
Randy at 11/14/01 1:27:17 PM
I need a statement or sp that will display, for a given user database, an individual table's fieldnames, datatype, and length. Any help is appreciated.
Randy
-
SELECT table field names (reply)
I believe he meant:
select * from information_schema.columns
------------
Randy at 11/14/2001 5:00:01 PM
Thanks Ray. I tried that but I must be getting the syntax wrong. Can you post a sample for a table called MATRIX?
Randy
------------
Ray Miao at 11/14/2001 1:32:32 PM
Select from information schema view columns.
------------
Randy at 11/14/01 1:27:17 PM
I need a statement or sp that will display, for a given user database, an individual table's fieldnames, datatype, and length. Any help is appreciated.
Randy
-
SELECT table field names (reply)
Try:
SELECT
sysobjects.id as TableID,
sysobjects.name as Table_Name,
syscolumns.name as Field_Name,
systypes.name as Field_Type,
systypes.length as Field_Length,
syscolumns.isnullable as Allow_Null,
syscomments.text as Default_Value
FROM syscolumns
INNER JOIN systypes on systypes.xusertype = syscolumns.xusertype
INNER JOIN sysobjects on sysobjects.id = syscolumns.id
LEFT OUTER JOIN sysobjects as Defaults on Defaults.id = syscolumns.cdefault
LEFT OUTER JOIN syscomments on syscomments.id = Defaults.id
WHERE sysobjects.xtype = 'U'
order by sysobjects.name, syscolumns.colorder
This'll give more info than you're looking for, but you can pare it down by reducing the joins and/or adding to the WHERE clause for the table name.
Jay
------------
at 11/15/2001 2:10:10 PM
I believe he meant:
select * from information_schema.columns
------------
Randy at 11/14/2001 5:00:01 PM
Thanks Ray. I tried that but I must be getting the syntax wrong. Can you post a sample for a table called MATRIX?
Randy
------------
Ray Miao at 11/14/2001 1:32:32 PM
Select from information schema view columns.
------------
Randy at 11/14/01 1:27:17 PM
I need a statement or sp that will display, for a given user database, an individual table's fieldnames, datatype, and length. Any help is appreciated.
Randy
-
SELECT table field names (reply)
Thanks Jay, I'll give it a try.
Randy
------------
Jay Haines at 11/15/2001 5:50:26 PM
Try:
SELECT
sysobjects.id as TableID,
sysobjects.name as Table_Name,
syscolumns.name as Field_Name,
systypes.name as Field_Type,
systypes.length as Field_Length,
syscolumns.isnullable as Allow_Null,
syscomments.text as Default_Value
FROM syscolumns
INNER JOIN systypes on systypes.xusertype = syscolumns.xusertype
INNER JOIN sysobjects on sysobjects.id = syscolumns.id
LEFT OUTER JOIN sysobjects as Defaults on Defaults.id = syscolumns.cdefault
LEFT OUTER JOIN syscomments on syscomments.id = Defaults.id
WHERE sysobjects.xtype = 'U'
order by sysobjects.name, syscolumns.colorder
This'll give more info than you're looking for, but you can pare it down by reducing the joins and/or adding to the WHERE clause for the table name.
Jay
------------
at 11/15/2001 2:10:10 PM
I believe he meant:
select * from information_schema.columns
------------
Randy at 11/14/2001 5:00:01 PM
Thanks Ray. I tried that but I must be getting the syntax wrong. Can you post a sample for a table called MATRIX?
Randy
------------
Ray Miao at 11/14/2001 1:32:32 PM
Select from information schema view columns.
------------
Randy at 11/14/01 1:27:17 PM
I need a statement or sp that will display, for a given user database, an individual table's fieldnames, datatype, and length. Any help is appreciated.
Randy
-
SELECT table field names (reply)
Hi Randy,
Here is, some intresting Stored Procedure that can help you more.
Sp_Help <Tablename> displays all the information regarding a given table. When information is required only on one particular field then this stored procedure can be used with SQL Server 7.0. Syntax: sp_HelpTableField 'TableName' , 'Substring of FieldName'. All the field names matching this 'Substring' will be displayed.
create proc sp_helpTableField
@objname nvarchar(776) = NULL, -- object name we're after
@Field nvarchar(50) = NULL -- field name we're after
as
-- PRELIMINARY
set nocount on
declare @dbname sysname
-- OBTAIN DISPLAY STRINGS FROM spt_values UP FRONT --
declare @no varchar(35), @yes varchar(35), @none varchar(35)
select @no = name from master.dbo.spt_values where type = 'B' and number = 0
select @yes = name from master.dbo.spt_values where type = 'B' and number = 1
select @none = name from master.dbo.spt_values where type = 'B' and number = 2
-- If no @objname given, give a little info about all objects.
if @objname is null
begin
-- DISPLAY ALL SYSOBJECTS --
select
'Name' = o.name,
'Owner' = user_name(uid),
'Object_type' = substring(v.name,5,31)
from sysobjects o, master.dbo.spt_values v
where o.xtype = substring(v.name,1,2) and v.type = 'O9T'
order by Object_type desc, Name asc
print ' '
-- DISPLAY ALL USER TYPES
select
'User_type' = name,
'Storage_type' = type_name(xtype),
'Length' = length,
'Prec' = TypeProperty(name, 'precision',
'Scale' = TypeProperty(name, 'scale',
'Nullable' = case when TypeProperty(name, 'AllowsNull' = 1
then @yes else @no end,
'Default_name' = isnull(object_name(tdefault), @none),
'Rule_name' = isnull(object_name(domain), @none)
from systypes
where xusertype > 256
order by name
return(0)
end
-- Make sure the @objname is local to the current database.
select @dbname = parsename(@objname,3)
if @dbname is not null and @dbname <> db_name()
begin
raiserror(15250,-1,-1)
return(1)
end
-- @objname must be either sysobjects or systypes: first look in sysobjects
declare @objid int
declare @sysobj_type char(2)
select @objid = id, @sysobj_type = xtype from sysobjects where id = object_id(@objname)
-- IF NOT IN SYSOBJECTS, TRY SYSTYPES --
if @objid is null
begin
-- UNDONE: SHOULD CHECK FOR AND DISALLOW MULTI-PART NAME
select @objid = xusertype from systypes where name = @objname
-- IF NOT IN SYSTYPES, GIVE UP
if @objid is null
begin
select @dbname=db_name()
raiserror(15009,-1,-1,@objname,@dbname)
return(1)
end
-- DATA TYPE HELP (prec/scale only valid for numerics)
select
'Type_name' = name,
'Storage_type' = type_name(xtype),
'Length' = length,
'Prec' = TypeProperty(name, 'precision',
'Scale' = TypeProperty(name, 'scale',
'Nullable' = case when allownulls=1 then @yes else @no end,
'Default_name' = isnull(object_name(tdefault), @none),
'Rule_name' = isnull(object_name(domain), @none)
from systypes
where xusertype = @objid
return(0)
end
-- FOUND IT IN SYSOBJECT, SO GIVE OBJECT INFO
select
'Name' = o.name,
'Owner' = user_name(uid),
'Type' = substring(v.name,5,31),
'Created_datetime' = o.crdate
from sysobjects o, master.dbo.spt_values v
where o.id = @objid and o.xtype = substring(v.name,1,2) and v.type = 'O9T'
print ' '
-- DISPLAY COLUMN IF TABLE / VIEW
if @sysobj_type in ('S ','U ','V '
begin
-- SET UP NUMERIC TYPES: THESE WILL HAVE NON-BLANK PREC/SCALE
declare @numtypes nvarchar(80)
select @numtypes = N'tinyint,smallint,decimal,int,real,money,floa t,numeric,smallmoney'
-- INFO FOR EACH COLUMN
print ' '
select
'Column_name' = name,
'Type' = type_name(xusertype),
'Computed' = case when iscomputed = 0 then @no else @yes end,
'Length' = convert(int, length),
'Prec' = case when charindex(type_name(xtype), @numtypes) > 0
then convert(char(5),ColumnProperty(id, name, 'precision')
else ' ' end,
'Scale' = case when charindex(type_name(xtype), @numtypes) > 0
then convert(char(5),OdbcScale(xtype,xscale))
else ' ' end,
'Nullable' = case when isnullable = 0 then @no else @yes end,
'TrimTrailingBlanks' = case ColumnProperty(@objid, name, 'UsesAnsiTrim'
when 1 then @no
when 0 then @yes
else '(n/a)' end,
'FixedLenNullInSource' = case
when type_name(xtype) not in ('varbinary','varchar','binary ','char'
Then '(n/a)'
When status & 0x20 = 0 Then @no
Else @yes END
from syscolumns where id = @objid
and name like '%'+@Field+'%' -- Neeraj Hora
order by colid
-- IDENTITY COLUMN?
print ' '
declare @colname sysname
select @colname = name from syscolumns where id = @objid
and colstat & 1 = 1
select
'Identity' = isnull(@colname,'No identity column defined.',
'Seed' = ident_seed(@objname),
'Increment' = ident_incr(@objname),
'Not For Replication' = ColumnProperty(@objid, @colname, 'IsIDNotForRepl'
-- ROWGUIDCOL?
print ' '
select @colname = null
select @colname = name from syscolumns where id = @objid
and ColumnProperty(@objid, name, 'IsRowGuidCol' = 1
select 'RowGuidCol' = isnull(@colname,'No rowguidcol column defined.'
end
-- DISPLAY PROC PARAMS
if @sysobj_type in ('P ' --RF too?
begin
-- ANY PARAMS FOR THIS PROC?
if exists (select id from syscolumns where id = @objid)
begin
-- INFO ON PROC PARAMS
print ' '
select
'Parameter_name' = name,
'Type' = type_name(xusertype),
'Length' = length,
'Prec' = case when type_name(xtype) = 'uniqueidentifier' then xprec
else OdbcPrec(xtype, length, xprec) end,
'Scale' = OdbcScale(xtype,xscale),
'Param_order' = colid
from syscolumns where id = @objid
end
end
-- DISPLAY TABLE INDEXES & CONSTRAINTS
if @sysobj_type in ('S ','U '
begin
print ' '
execute sp_objectfilegroup @objid
print ' '
execute sp_helpindex @objname
print ' '
execute sp_helpconstraint @objname,'nomsg'
end
else if @sysobj_type in ('V '
begin
-- VIEWS DONT HAVE CONSTRAINTS, BUT PRINT THESE MESSAGES BECAUSE 6.5 DID
print ' '
raiserror(15469,-1,-1) -- No constraints defined
print ' '
raiserror(15470,-1,-1) --'No foreign keys reference this table.'
end
return (0) -- sp_help
--------------------------------------------------------> THE END.
Thanks,
clar.
------------
Randy at 11/14/2001 1:27:17 PM
I need a statement or sp that will display, for a given user database, an individual table's fieldnames, datatype, and length. Any help is appreciated.
Randy
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
|