![]() |
|
|
By: Atif Shehzad | Read Comments (2) | Print Atif is a passionate SQL Server DBA, technical reviewer and article author. Related Tips: More |
|
Solution
Although a few different design options are available, one option is to create SQL Server user defined data types and bind them to an existing default and rule. In some respects, they can be considered customized SQL Server data types. The SQL Server user defined data types can be created both with SQL Server Management Studio and T-SQL commands. Let's walk through samples of each option to serve as an example of SQL Server user defined data types can be used with defaults and rules.
Creating SQL Server User Defined Data Types in SQL Server Management Studio (SSMS)

General page of User-Defined Data Type
Below is a example view of the General page for a user defined data type creation.

Extended Properties page of User-Defined Data Type
I have added two extended properties for the User-Defined Data Type which can be considered meta data. In this example I have included the author (Created By) and purpose (Created For) as shown below:

T-SQL Implementation of the User Defined Data Types, Rules and Defaults
Below outlines the sample code for the User Defined Data Types, Rules and Defaults:
|
Syntax for creating SQL Server user defined data type |
sp_addtype [ @typename = ] type, |
Here is a basic explanation of the four parameters from the system stored procedure:
| Parameter | Explanation |
| @typename | Name of new user defined data type that is being created |
| @phystype | Base system data type of SQL Server |
| @nulltype | To specify that null values are allowed for this data type or not |
| @owner | Owner of this being created user defined data type |
Below is the example default and rule code used in SSMS above:
|
Example Default and Rule Code |
| -- Create a default value for phone number to be used in example USE AdventureWorks GO CREATE DEFAULT Default_PhNo AS 'UnknownNumber' GO -- Create a rule for phone number to be used in example -- Number will be in format +92-3335409953 or UnknownNumber by default USE AdventureWorks GO CREATE RULE rule_PhNo AS (@phone='UnknownNumber') OR (LEN(@phone)=14 AND SUBSTRING(@phone,1,1)= '+' AND SUBSTRING(@phone,4,1)= '-') GO |
In the final code snippet, we will bind the rules and defaults to the user defined data types:
|
Commands to bind the defaults and rules to user defined data types |
-- To bind a default 'Default_PhNo' to user defined data type 'PhoneNumb' -- To bind a rule 'rule_PhNo' to user defined data type 'PhoneNumb' EXEC sp_bindrule 'rule_PhNo', 'PhoneNumb' GO |
Next Steps
| Share: | Share | Tweet |
|
![]() |
![]() |
Free SQL Server Learning |
| Monday, January 30, 2012 - 5:32:55 AM - barun kumar |
|
|
Hello sir, i have read your blogs on SQL Server User Defined Data Types, Rules and Defaults . It's very useful for me but i have a doubt that you use a variable @phone while creating the rule without declaring it and it work properly without generating any error why? Please clear my doubt. |
|
|
privacy | disclaimer | copyright | advertise | about authors | contribute | feedback | giveaways | user groups Some names and products listed are the registered trademarks of their respective owners. Edgewood Solutions LLC | MSSharePointTips.com | MSSQLTips.com |