I know that I can get access to column properties via:
select *
from sysobjects
What I can’t find however is information about where to get the type and type length for a column, ie: in
FOO VARCHAR(80)
Where do I look to find the “VARCHAR(80)” part of the type declaration in the metadata tables?
I tried looking at the systypes table, but its values for xtype do not match up to the values of xtype in the sysobjects table.
*I do not have access to the original SQL used to build these tables nor do I have any admin rights.
If you’re familiar with DB2 I’m looking for the equivalent to
select name,
coltype,
length,
from sysibm.syscolumns
where tbname = 'FOO'
You are close. You can look at
sys.columnsto get the columns.You can filter on a table with
OBJECT_ID=OBJECT_ID('dbo.Foo').You can get the length from
sys.columns. The data type is in theuser_type_idfield. The keys for that field are insys.types.In its entirety you can do:
As a side note, in SQL Server the system tables are deprecated (i.e.
syscolumns,sysobjects) and it’s recommended as a best practice to use the views instead,sys.columns,sys.objects, etc.This will give you Table, column, data type, and maxlength for each one.