0

If I have a user table type (like CREATE TYPE ...), how do I get to it's definition in the system tables?

I know I can see it through SSMS through GUI, but I would like to have a query to retrieve it from system tables.

I don't see it through any of the modules:

SELECT 
 * 
FROM 
 sys.all_sql_modules m 
WHERE 
 m.definition like 'CREATE TYPE %' --doesn't output any types definitions
asked Apr 11, 2019 at 11:01

1 Answer 1

2

You need to query sys.table_types:

SELECT
 SchemaName = SCHEMA_NAME(TYPE.schema_id),
 TypeName = TYPE.name,
 ColumnID = COL.column_id,
 [Column] = COL.name,
 DataType = ST.name,
 IsNullable = COL.Is_Nullable,
 Length = COL.max_length,
 Precision = COL.[precision],
 Scale = COL.scale,
 Collation = ST.collation
FROM 
 sys.table_types TYPE
 INNER JOIN sys.columns COL ON TYPE.type_table_object_id = COL.object_id
 INNER JOIN sys.systypes AS ST ON ST.xtype = COL.system_type_id
WHERE
 TYPE.is_user_defined = 1
ORDER BY 
 SchemaName,
 TypeName,
 ColumnID

Make sure you are connected to the proper database.

If you want a CREATE TYPE statement like the one from SSMS, you will have to build it from the results of the previous query. That or trace the SSMS's query to see how it builds it when you ask it from the GUI, but there's a chance it's very convoluted (although bulletproof).

answered Apr 11, 2019 at 11:09

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.