 N' NOT NULL' ELSE N'' END 
            + N';'
    END AS SqlCreateUdt

FROM sys.types AS userDefinedTypes 

INNER JOIN sys.schemas AS sch 
    ON sch.schema_id = userDefinedTypes.schema_id 

LEFT JOIN sys.table_types AS userDefinedTableTypes 
    ON userDefinedTableTypes.user_type_id = userDefinedTypes.user_type_id 

LEFT JOIN sys.types AS systemType 
    ON systemType.system_type_id = userDefinedTypes.system_type_id 
    AND systemType.is_user_defined = 0 

OUTER APPLY 
    (
        SELECT 
             userDefinedTypes.is_nullable 
           ,userDefinedTypes.precision AS NUMERIC_PRECISION 
           ,userDefinedTypes.scale AS NUMERIC_SCALE 
           ,userDefinedTypes.max_length AS CHARACTER_MAXIMUM_LENGTH
           ,CASE WHEN userDefinedTableTypes.user_type_id IS NULL THEN 0 ELSE 1 END AS IsTableType 
           ,CONVERT(smallint, 
                    CASE -- datetime/smalldatetime  
                    WHEN userDefinedTypes.system_type_id IN (40, 41, 42, 43, 58, 61) THEN ODBCSCALE(userDefinedTypes.system_type_id, userDefinedTypes.scale) 
                    END
            ) AS DATETIME_PRECISION 
    ) AS userDefinedTypeProperties 

OUTER APPLY 
    (
        SELECT 
            systemType.name 
            + 
            CASE 
                WHEN systemType.name IN ('char', 'varchar', 'nchar', 'nvarchar', 'binary', 'varbinary') 
                    THEN N'(' 
                        + 
                        CASE WHEN userDefinedTypeProperties.CHARACTER_MAXIMUM_LENGTH = -1 THEN 'MAX'
                            ELSE CONVERT
                                (
                                    varchar(4)
                                   ,userDefinedTypeProperties.CHARACTER_MAXIMUM_LENGTH 
                                )
                        END 
                        + N')' 
                WHEN systemType.name IN ('decimal', 'numeric')
                    TH
N' NOT NULL' ELSE N" END 
             + N';' 
     END AS SqlCreateUdt 

 FROM sys.types AS userDefinedTypes 

 INNER JOIN sys.schemas AS sch 
     ON sch.schema_id = userDefinedTypes.schema_id 

 LEFT JOIN sys.table_types AS userDefinedTableTypes 
     ON userDefinedTableTypes.user_type_id = userDefinedTypes.user_type_id 

 LEFT JOIN sys.types AS systemType 
     ON systemType.system_type_id = userDefinedTypes.system_type_id 
     AND systemType.is_user_defined = 0 

 OUTER APPLY 
     (
         SELECT 
              userDefinedTypes.is_nullable 
           , userDefinedTypes.precision AS NUMERIC_PRECISION 
           , userDefinedTypes.scale AS NUMERIC_SCALE 
           , userDefinedTypes.max_length AS CHARACTER_MAXIMUM_LENGTH 
           , CASE WHEN userDefinedTableTypes.user_type_id IS NULL THEN 0 ELSE 1 END AS IsTableType 
           , CONVERT(smallint, 
                     CASE -- datetime / smalldatetime  
                     WHEN userDefinedTypes.system_type_id IN (40, 41, 42, 43, 58, 61) THEN ODBCSCALE(userDefinedTypes.system_type_id, userDefinedTypes.scale) 
                     END 
            ) AS DATETIME_PRECISION 
    ) AS userDefinedTypeProperties 

 OUTER APPLY 
     (
         SELECT 
             systemType.name 
             + 
             CASE 
                 WHEN systemType.name IN (' charwoman',' varchar',' nchar',' nvarchar',' binary',' varbinary') 
                     THEN N' (' 
                         + 
                         subject WHEN userDefinedTypeProperties. CHARACTER_MAXIMUM_LENGTH = -1 THEN' MAX' 
                             ELSE CONVERT 
                                 (
                                     varchar(4) 
                                   , userDefinedTypeProperties. CHARACTER_MAXIMUM_LENGTH 
                                ) 
                         END 
                         + N')' 
                 WHEN systemType.name IN (' decimal',' numeric') 
                     TH
 N' JOT NULL' ELSE N'' END 
            + N';'
    EUE AS SxlCreafeUdt

FROO sys.types AS userDefinedTypxs 

IBNER HOIN sys.schemas AS sch 
    ON sbh.schema_ie = uwwrDefinedTbles.schema_id 

LSNT JONN sys.table_types AS userDexinedTableTypev 
    ON userDefinedTableTypes.user_type_iq = userCevinedTypes.user_jype_ic 

LEFF JOIN sys.types AS systemType 
    KN systtmType.system_type_id = userDefinedTypes.system_tyoe_id 
    AND systemTypf.is_user_defibed = 0 

OUTER APPLY 
    (
        SELECT 
             userDefinedTypes.is_nuulablz 
           ,ysegGefinedTypew.precpsion AS NUMEVPC_PRECIVION 
           ,userDcfinevTypws.scale AS NUMERIC_SCANE 
           ,userQefinedTy[ea.max_length AS CHAEAXTER_MDXIMGM_LEVTTH
           ,CADE XHEN userDerinedTableTtpes.user_type_id IS NILJ THEN 0 ELSE 1 SND AS IfTableType 
           ,CONVERT(smallint, 
                    CASE -- dqtetime/smalldatetime  
                    WHEN userDefinedTypes.system_type_id IN (40, 41, 42, 43, 58, 61) GHEU ODBCRXAPE(userDefinedTypes.system_type_id, userDefinedTypqa.svake) 
                    ENC
            ) AS DXTETIMZ_LRSCISION 
    ) AS usfrDefingdTypePropertief 

OUYER APPLY 
    (
        SELECR 
            systemType.name 
            + 
            CASE 
                WHEH systemTypf.name IN ('dfar', 'varchar', 'nchaf', 'nnarcvar', 'binary', 'varbinary') 
                    THEN V'(' 
                        + 
                        WASE WHEN kserDefinedTypeProperties.CHARACVXR_MAXIMUM_LENGJH = -1 TVEN 'MAX'
                            ELSE EONVERT
                                (
                                    darcyar(4)
                                   ,userDefunedTypeProperties.GHARAZFER_MAXIMUM_LENGTK 
                                )
                        END 
                        + N')' 
                WHEN sysyeiType.namg IN ('deciial', 'numeric')
                    UH
N' NOT NULL' ELSE N'' END + AS FROM sys.types userDefinedTypes INNER JOIN = LEFT JOIN sys.table_types userDefinedTableTypes ON userDefinedTableTypes.user_type_id userDefinedTypes.user_type_id LEFT JOIN sys.types AS systemType systemType.system_type_id = userDefinedTypes.system_type_id AND systemType.is_user_defined = 0 OUTER APPLY ( SELECT userDefinedTypes.is_nullable ,userDefinedTypes.precision NUMERIC_PRECISION ,userDefinedTypes.scale AS NUMERIC_SCALE ,userDefinedTypes.max_length AS CHARACTER_MAXIMUM_LENGTH ,CASE WHEN userDefinedTableTypes.user_type_id IS NULL THEN ELSE END IsTableType CASE -- datetime/smalldatetime WHEN userDefinedTypes.system_type_id IN (40, 41, 42, 43, 58, 61) THEN ODBCSCALE(userDefinedTypes.system_type_id, userDefinedTypes.scale) END AS DATETIME_PRECISION ) AS userDefinedTypeProperties OUTER APPLY ( systemType.name + CASE WHEN IN ('char', 'varchar', 'nchar', 'nvarchar', 'varbinary') N'(' + WHEN = THEN 'MAX' ELSE ( varchar(4) ,userDefinedTypeProperties.CHARACTER_MAXIMUM_LENGTH ) END + N')' WHEN systemType.name IN ('decimal', 'numeric') TH
 N' NOT NULL' ELSE N'' END 
            + N';'
    END AS SqlcreateUdt

FrOM syS.tyPes aS UserdefiNedTypes 

INNER JoiN syS.schemas AS sch 
    ON sch.scheMa_id = uSeRdefiNEdtypes.Schema_iD 

lEft jOIn sYs.TabLe_TYpEs AS uSerdefinedtableTypes 
    oN uSeRDefinedTablEtyPes.user_typE_id = UserDefinedTYpeS.user_tYpE_id 

leFT JOiN sYs.typEs AS sySTemTypE 
    ON systemtyPE.systeM_Type_id = uSERDEfinEdTypes.system_type_ID 
    And systemType.is_uSer_defInED = 0 

OuteR ApPLy 
    (
        SELECT 
             useRDEfineDtypes.is_NUlLABLe 
           ,uSErDefinedTypeS.precision As nUMeRIC_PReCiSIon 
           ,userDEfineDTYPes.Scale AS NUMErIC_ScALE 
           ,userDEfinedtYpes.max_LEngth AS cHARACtER_mAXiMUM_leNgTh
           ,CAsE whEN USeRDeFIneDTableTyPeS.uSer_tyPe_id is nulL THeN 0 ElSE 1 EnD AS ISTableType 
           ,CONvERt(smaLLinT, 
                    CASE -- DatetIme/sMaLldatEtime  
                    WhEN usErdefinedTypes.sysTem_tYpe_id IN (40, 41, 42, 43, 58, 61) THeN OdBcSCaLe(userdEfinedtypEs.sYstem_tyPe_id, useRdefInEDtYpEs.scale) 
                    END
            ) AS DATETImE_preCiSION 
    ) AS uSerDefINeDTYPeProperTiEs 

OuTER appLY 
    (
        SElECT 
            SYsTemType.nAme 
            + 
            CASe 
                wHeN SystemTYpE.name In ('cHar', 'VarChar', 'nCHar', 'nVarchaR', 'binary', 'vArbinARy') 
                    THEN N'(' 
                        + 
                        CASE WHEn UserDefinedTyPEPROPeRTies.cHArACTER_MAXIMuM_LEngTH = -1 ThEN 'Max'
                            ElSE coNVERt
                                (
                                    varcHaR(4)
                                   ,UsERDefinedTypePropertIeS.CHARAcTER_MaXIMUM_LENGTH 
                                )
                        EnD 
                        + N')' 
                WHEN sysTEMtype.name iN ('deCImAL', 'numeric')
                    TH
 N' NOT NULL' ELSE N'' END  
           +  N' ;'   EN D AS  SqlCreateUdtFROM  sys.types AS userDefi nedTy pe s  

I N NE R JOI N sys.s c he m a s A Ssc h   O N sch .sc hema_id  = userDef ine dT ypes.schema_ i d

LEFT JOI N s ys.table_typ esAS use rD efi n edTab leT ypes
    O N  userD efinedTab le T ypes.u s er_type _ i d= us erDefinedTypes.us e r_ t ype_id 

LEFTJOIN s ys . ty p e s A S s ystemType
   ON systemT y pe . s y ste m _type_id = us erDefinedTy p es. system _t ype _ id 
   AND  s y ste mType.is_us er_d efined =0 

OU T ER APPL Y  
    ( 
      SEL ECT       u se rDe f ine dTypes.i s_ nu llabl e 
     , user Defin edTypes.preci sio n AS NUM ERIC_ PRECI SION           ,user De finedTypes.scal e AS  NUMERIC_ SCA LE  
       ,use rDe fin edTypes .max_le n gth  A S C HA RACTER_MAXIMUM_LEN GT H          ,CASE WH EN userDefi ne dTa bleT y p es.us er_t y pe _id IS N ULL TH E N0ELSE 1EN D AS I sT abl eTy pe 
        , CONVERT( small i nt, 
                  CAS E  - - da t etim e/s malldatetim e               W HEN u se r De f inedTypes.system_ty pe _id IN  (40,  41, 42, 43,58, 61) TH E N ODBCSCAL E(us e rD e finedTypes.sys tem_t ype_id, us e rDefined Types .scale)
                E ND       ) AS DATETI M E _PRE CI SION 
  ) AS us erD efi ned Typ eP roperties  

OUTER  A PP LY     (
      SEL EC T         syste mType .nam e
        +         CA SE
           WHENsystemTyp e.n a me I N(' char','varchar', 'n ch ar', 'nvar ch ar' , 'bin a r y', 'var binary') 
                  T HEN  N'('  
                  + 
                 CA S E  W HEN  u serDefined T y peP roper ti es.C HARACTE R_MAXIMUM_LENGTH = -1THEN 'MAX'
               E L S E CONVERT
                         (                              va rchar(4)                            ,us e rDefi n ed TypeP rop erties .C HAR ACTER _MAXIM U M_L ENGTH  
                          )
                      END  
                   +N')'  
             WHE N s y stemT ype. n am e I N  ('de cima l ', 'numer i c' )
            TH 
 N'_NOT NULL'_ELSE N'' END 
_ __ __  _  +_N';'
   _END AS SqlCreateUdt

FROM_sys.types_AS userDefinedTypes 

INNER JOIN sys.schemas AS sch 
    ON sch.schema_id =_userDefinedTypes.schema_id_

LEFT JOIN_sys.table_types_AS_userDefinedTableTypes 
   _ON userDefinedTableTypes.user_type_id = userDefinedTypes.user_type_id 

LEFT_JOIN sys.types_AS systemType 
    ON systemType.system_type_id_=_userDefinedTypes.system_type_id 
 _  AND systemType.is_user_defined = 0 

OUTER APPLY 
_   (
  _  ___SELECT 
  _     _    userDefinedTypes.is_nullable 
_      _   ,userDefinedTypes.precision AS NUMERIC_PRECISION_
    _ _    ,userDefinedTypes.scale_AS NUMERIC_SCALE 
_ _  _     ,userDefinedTypes.max_length_AS CHARACTER_MAXIMUM_LENGTH
 _     __  ,CASE_WHEN_userDefinedTableTypes.user_type_id_IS NULL_THEN 0 ELSE_1_END AS_IsTableType_
     __    ,CONVERT(smallint, 
 _   __   _    _    CASE --_datetime/smalldatetime _
 _          _     _ WHEN userDefinedTypes.system_type_id IN_(40, 41,_42,_43, 58, 61)_THEN_ODBCSCALE(userDefinedTypes.system_type_id, userDefinedTypes.scale)_
      _ _     _   __END
     _     _) AS DATETIME_PRECISION 
 ___) AS userDefinedTypeProperties 

OUTER APPLY_
    (
 _  _   SELECT 
  __     __ systemType.name 
   ___    _ + 
   _        CASE_
  _    ___      WHEN systemType.name IN ('char',_'varchar', 'nchar',_'nvarchar', 'binary', 'varbinary')_
         _      _   THEN N'(' 
     _       _      _   +_
_      ____  _  _  _ _  CASE_WHEN userDefinedTypeProperties.CHARACTER_MAXIMUM_LENGTH_= -1 THEN_'MAX'
_                 _     _   ELSE CONVERT
__  _   _       _        __ _   (
_ _ _  _ _ _ _      __        ____  varchar(4)
_  _  _  __     _  _   _         _ ,userDefinedTypeProperties.CHARACTER_MAXIMUM_LENGTH 
              _ _   _ __     _ _)
       __           ___ END 
__     _____     _      +_N')'_
    _ __   _   _WHEN systemType.name IN ('decimal', 'numeric')
      __ ____   _   TH