Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where do I find Sql Server metadata for column datatypes?

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'
like image 366
avgvstvs Avatar asked Feb 07 '12 16:02

avgvstvs


People also ask

How do I find column data types in SQL Server database?

You can get the MySQL table columns data type with the help of “information_schema. columns”. SELECT DATA_TYPE from INFORMATION_SCHEMA. COLUMNS where table_schema = 'yourDatabaseName' and table_name = 'yourTableName'.

Where is metadata stored in SQL Server?

T-SQL metadata functions in SQL Server come under the umbrella of System functions in SSMS and can be accessed as shown below. Expand Databases, and navigate to Programmability in database AdventureWorksDW2017.

How do I find column properties in SQL?

Right-click a column in the Model Explorer and click Properties. The SQL Server Table Column Editor opens. Select the table from the Table drop-down to define the columns that are available for the table.


1 Answers

You are close. You can look at sys.columns to 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 the user_type_id field. The keys for that field are in sys.types.

In its entirety you can do:

select object_NAME(c.object_id), c.name, t.name, c.max_length
from sys.columns c
INNER JOIN sys.types t
    ON t.user_type_id = c.user_type_id

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.

like image 109
JNK Avatar answered Sep 22 '22 17:09

JNK