I'm trying to add faking synonyms pointing to remote objects support(PR) for tSQLt.
To make it possible to mock/fake tables that have user defined system types in the table definitions, I need to generate the User Defined Data Type (UDDT) DDL statement.
I don't have too much experience with UDDTs, and what I can think of about very simple scenario if the UDDT just is as simple if it would be something like CREATE TYPE dbo.SomeType FROM INT
, but I know that it can be much more complex.
So, the question is if anyone has some working solution already, ideally implemented using T-SQL via system objects. In the worst case, SQLCLR would be the only other option.
There are different kinds of User-Defined Types: User-Defined Table Types (UDTT), User-Defined Types (UDT; complex types implemented via SQLCLR), and User-Defined Data Types (UDDT; mainly synonyms of existing system types, but with size/precision and
NULL
/NOT NULL
included).You don't need to worry about UDTTs as they cannot be columns in tables.
You probably can't deal with UDTs, at least not right now, as that adds a lot of complexity since you would need to copy the assembly as well.
There is one main system catalog view:
sys.types
. The following should get you most, if not all, of what you need.collation_name
does not appear to be usable, either by testing a simpleCREATE TYPE
as a test, or the documentation.Rules
!! These have been deprecated for a long time and should not be used !!
Documentation for
CREATE RULE
Rules can be found in:
sys.sql_modules
(which includes theCREATE
statement in the[definition]
field)You will need to separately cycle through
sys.types
and, for any user-created types whererule_object_id <> 0
, executeEXEC sp_bindrule N'@rulename', N'@objectname';
.Defaults
!! These have been deprecated for a long time and should not be used !!
Documentation for
CREATE DEFAULT
Defaults can be found in:
sys.sql_modules
(which includes theCREATE
statement in the[definition]
field)You will need to separately cycle through
sys.types
and, for any user-created types wheredefault_object_id <> 0
, executeEXEC sp_bindefault N'@defaultname', N'@objectname';
.