Question

How can I get back the column information for a User-Defined Table Type?

EXEC sp_columns TABLENAME

gets me back all the column information for a table.

I want to do the same thing for a User-Defined Table Type called SearchList and roughly the same column information back.

I want to get this so I can code generate the Data Tables that I need in c#.

UPDATE TO SHOW WHAT I USED

select c.name as COLUMN_NAME, t.name as [TYPE_NAME], c.precision as [PRECISION], c.is_nullable as [NULLABLE], c.system_type_id, c.precision as [LENGTH]
from sys.columns c, sys.types t
where c.object_id = (select type_table_object_id from sys.table_types where name = 'SearchList')
and t.user_type_id = c.user_type_id
order by c.column_id
Was it helpful?

Solution

This will list all table types and their columns:

select tt.name, c.name from sys.table_types tt
inner join sys.columns c on c.object_id = tt.type_table_object_id
order by c.column_id

You can add a where clause and select other columns, as appropriate, to get what you need.

OTHER TIPS

I have used below query in my project to get the table type with column name and data type.

select tt.name AS table_Type, c.name AS table_Type_col_name,st.name AS     
table_Type_col_datatype
from sys.table_types tt
inner join sys.columns c on c.object_id = tt.type_table_object_id
INNER JOIN sys.systypes AS ST  ON ST.xtype = c.system_type_id
order by tt.name
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top