2929* Added fix from Erland for doubling length of nchar columns
3030* Added quoting of column names in create index statements (for reserved words)
3131*
32+ * Modified 2022-09-21 by Matthew Rees
33+ *
34+ * Fix bug when user defined types exist by adding additional join clause on sys.types for user_type_id
35+ *
3236* Modified 2023-05-31 by Roelof de Villiers
3337*
3438* Include original constraint name when putting back DEFAULT constraints.
@@ -104,6 +108,7 @@ INNER JOIN sys.columns AS c
104108 AND c .collation_name <> @DatabaseCollation -- Table needs to have columns with "wrong" collation
105109INNER JOIN sys .types AS ty
106110 ON ty .system_type_id = c .system_type_id
111+ AND ty .user_type_id = c .user_type_id
107112 AND ty .name <> N' sysname' -- Exclusion retained from Philip C's original script
108113LEFT JOIN sys .index_columns AS ic -- Find indexes on any of the affected columns
109114 ON ic .object_id = c .object_id
@@ -165,6 +170,7 @@ BEGIN;
165170 FROM sys .columns AS c
166171 INNER JOIN sys .types AS ty
167172 ON ty .system_type_id = c .system_type_id
173+ AND ty .user_type_id = c .user_type_id
168174 AND ty .name <> N' sysname'
169175 WHERE c .object_id = @object_id
170176 AND c .collation_name <> @DatabaseCollation;
@@ -216,6 +222,7 @@ BEGIN;
216222 AND c .collation_name <> @DatabaseCollation
217223 INNER JOIN sys .types AS ty
218224 ON ty .system_type_id = c .system_type_id
225+ AND ty .user_type_id = c .user_type_id
219226 AND ty .name <> N' sysname'
220227 WHERE ic .index_id = ix .index_id
221228 AND ic .object_id = ix .object_id );
@@ -403,6 +410,7 @@ BEGIN;
403410 AND c .collation_name <> @DatabaseCollation
404411 INNER JOIN sys .types AS ty
405412 ON ty .system_type_id = c .system_type_id
413+ AND ty .user_type_id = c .user_type_id
406414 AND ty .name <> N' sysname'
407415 WHERE fkc .parent_object_id = fk .parent_object_id
408416 AND fkc .constraint_object_id = fk .object_id );
@@ -608,6 +616,7 @@ BEGIN;
608616 AND c .collation_name <> @DatabaseCollation
609617 INNER JOIN sys .types AS ty
610618 ON ty .system_type_id = c .system_type_id
619+ AND ty .user_type_id = c .user_type_id
611620 AND ty .name <> N' sysname'
612621 WHERE ic .object_id = kc .parent_object_id
613622 AND ic .index_id = kc .unique_index_id );
@@ -695,6 +704,7 @@ BEGIN;
695704 AND dc .parent_column_id = c .column_id
696705 INNER JOIN sys .types AS ty
697706 ON ty .system_type_id = c .system_type_id
707+ AND ty .user_type_id = c .user_type_id
698708 AND ty .name <> N' sysname'
699709 WHERE c .object_id = @object_id
700710 AND c .collation_name <> @DatabaseCollation;
@@ -710,6 +720,7 @@ BEGIN;
710720 INNER JOIN sys .default_constraints dc on dc .object_id = c .default_object_id
711721 INNER JOIN sys .types AS ty
712722 ON ty .system_type_id = c .system_type_id
723+ AND ty .user_type_id = c .user_type_id
713724 AND ty .name <> N' sysname'
714725 WHERE c .object_id = @object_id
715726 AND c .collation_name <> @DatabaseCollation;
0 commit comments