I've been using UDTs for years for those very reasons of consistency & self-documentation you propounded in your video. Updating the UDT has always been its Achilles heel.Currently
You suggest in the video that UDTs should be assigned retrospectively once the database design has settled down - which almost contradicts their reason to exist! Nevertheless, structures DO change and we must synchronise our types to the new design. Here's a handy 2005 script showing all dependencies for your UDTs. At least you can see which objects you need to change or drop before updating your type. This script could be wrapped into a parameterised SP for refined filtering etc.
Select t.name UDT,
SELECT object_id, name, user_type_id, 'SQL_COLUMN' AS type_desc
SELECT object_id, name, user_type_id, 'SQL_PROCEDURE_PARAMETER'
JOIN sys.objects o ON o.object_id = c.object_id
JOIN sys.types t ON t.user_type_id = c.user_type_id
Where t.schema_id = 1 -- dbo (see sys.schemas for all other object owners & change as reqd)
and here's one for SQL Server 2000.
Select t.name UDT,
From syscolumns c
JOIN sysobjects o ON o.id = c.id
JOIN systypes t ON t.usertype = c.xusertype
Where t.uid = 1 -- dbo (see sysusers for other users' IDs)
and o.xtype in ('P','U')
Another thing about UDTs: you can't use them in the Convert()
functions! You get "Type xxx is not a defined system type". When are MS going to get that right? (Apologies if this exists in 2008.)Proposal
But life shouldn't be that hard, and I would propose
the following solution for a near-future SQL Server upgrade.
In almost all database scripting scenarios, every variable is derived from, or has a relationship with, a table-based field. Why not use that field's type when declaring any variable or SP parameter etc? So, for example, when creating a variable relating to tblClient.EMailAddress
, it would be defined as: declare @EMailAddress tblClient.EMailAddress%Type
The upshot of this extension to the syntax is that all variables' types would be dynamic and would depend on the CURRENT type of the underlying source field at execution time. No need to update anything except the table structure! In the purest form, no type except for those defining table fields would use a system type
. Oracle have been doing it for years and it works a treat. From a user/programmer perspective, the change would be painless, rock solid and even more self-documenting than UDTs now. Changing the underlying SQL Server engine to use this proposed syntax may not be be so trivial...