Creates a user-defined data type.
sp_addtype [@typename =] type,
[@phystype =] system_data_type
[, [@nulltype =] 'null_type']
‘binary(n)’ | image | smalldatetime |
bit | int | smallint |
‘char(n)’ | ‘nchar(n)’ | text |
datetime | ntext | tinyint |
decimal | numeric | uniqueidentifier |
‘decimal[(p[, s])]’ | ‘numeric[(p[, s])]’ | ‘varbinary(n)’ |
float | ‘nvarchar(n)’ | ‘varchar(n)’ |
‘float(n)’ | real |
Quotation marks are required around all parameters that have embedded blank spaces or punctuation marks. For more information about available data types, see Data Types.
Note The null_type parameter only defines the default nullability for this data type. If nullability is explicitly defined when the user-defined data type is used during table creation, it takes precedence over the defined nullability. For more information, see ALTER TABLE and CREATE TABLE.
0 (success) or 1 (failure)
None
A user-defined data type name must be unique in the database, but user-defined data types with different names can have the same definition.
Executing sp_addtype creates a user-defined data type and adds it to the systypes system table for a specific database, unless sp_addtype is executed with master as the current database. If the user-defined data type must be available in all new user-defined databases, add it to model. After a user data type is created, you can use it in CREATE TABLE or ALTER TABLE, as well as bind defaults and rules to the user-defined data type.
User-defined data types cannot be defined using the SQL Server timestamp data type.
Execute permissions default to the public role.
This example creates a user-defined data type named ssn (social security number) that is based on the SQL Server-supplied varchar data type. The ssn data type is used for columns holding 11-digit social security numbers (999-99-9999). The column cannot be NULL.
Notice that varchar(11) is enclosed in single quotation marks because it contains punctuation (parentheses).
USE master
EXEC sp_addtype ssn, 'VARCHAR(11)', 'NOT NULL'
This example creates a user-defined data type (based on datetime) named birthday that allows null values.
USE master
EXEC sp_addtype birthday, datetime, 'NULL'
This example creates two additional user-defined data types, telephone and fax, for both domestic and international telephone and fax numbers.
USE master
EXEC sp_addtype telephone, 'varchar(24)', 'NOT NULL'
EXEC sp_addtype fax, 'varchar(24)', 'NULL'
CREATE DEFAULT | sp_rename |
CREATE RULE | sp_unbindefault |
sp_bindefault | sp_unbindrule |
sp_bindrule | System Stored Procedures |
sp_droptype |