User-Defined Types In SQL Server

When we are working with any programming language, we create user defined types as per our requirement. It is a better idea to have this feature in SQL server, right? Yes, we have the same kind of facility in SQL server  -- we call it User-Defined Types.

We have mainly two types of User-defined types in SQL

  1. User-Defined data type
  2. User-Defined Tables Type

User Defined Data Types

When do we have to use it?

When there is a requirement to create a column with a datatype in the database and creating the same data type columns multiple times by multiple people. Here there is no guarantee that all developers will create the same type. In this situation we can make use of User-Defined data types. So, we create at one time and using the type wherever you need.

How to create User defined data types?

There are 2 ways to create it. The first one and the easiest method is from UI and the second one is using script.

Using UI steps to create User-defined Data types

Expand the data base where you want to create data types -> Programmability ->Types.

SQL server

Right click on the User-Defined Data Types and click on New User-Defined Data Type… . A window will pop up to create the type as shown in the below figure.

SQL server

Enter the Name of the type in the Name textbox and select the base type from Data Type dropdown. Length input will be enabled based on the data type selection. And click on OK button of the popup.

Using Script to create User-defined Data types

I am trying to create 3 user defined data types for Name, Age and Location.

  1. CREATE TYPE TypeName FROM VARCHAR(100)  
  2. CREATE TYPE TypeAge FROM INT  
  3. CREATE TYPE Location FROM VARCHAR(500)  

Let’s use the above created Types for creating table or variable declaration.

  1. CREATE TABLE EMP(EMP_Name TypeName, Emp_Age TypeAge,Emp_Location TypeLocation)  
  2. DECLARE @Name TypeName  

User Defined Table Types

When we have to use it?

Generally, we declare tables to store temp data and use it in the next lines of the script

  1. DECLARE @empTempTab Table (name varchar(100, age int, location VARCHAR(100))  

Suppose we need to create the same kind of temp table in multiple SPs. In this case, instead of creating temp tables multiple times we can create required table type once and use it where ever we require.

How to create User defined Table types?

We don’t have UI to create the User-Defined Table as I explained in the Data types. We can create User-Defined Table types using script method.

Using Script to create User-defined Data types

Let’s take the above temp table structure.

  1. CREATE TYPE empTempTabtype AS Table (name varchar(100), age int, location VARCHAR(100))  
With the above statement we can create the table type.

Now let’s use the above table type to create the temp table.

  1. DECLARE @empTempTab empTempTabType  
Where we can find created types in the data base?

To find the created types in SQL server, go to database->Programmability->Types

There we can find following types

  • User-Defined Data Types
  • User-Defined Table Types

On expanding any of them, we can find our created types

SQL server

Limits of User defined types

  • User-defined types are accessible with in the Database only.
  • We cannot use Table types in the table valued functions

For Example

  1. --Creating Table type  
  2.   
  3. CREATE TYPE empTempTabtype AS Table (ID int identity(1,1),name varchar(100) default('ravi'), age int, location VARCHAR(100))    
  4.   
  5.   
  6. --Trying to create the Table valued function with the above type  
  7.   
  8. CREATE FUNCTION dbo.ufnGetEMPInformation(@ID int)    
  9. RETURNS @retContactInformation empTempTabtype    
  10. AS    
  11. BEGIN    
  12. INSERT INTO @retContactInformation VALUES(@ID, 'ravi', 30, 'Hyderabad')    
  13. RETURN    
  14. END    

  15. --We will get an error on executing the above type.
We cannot alter the User-defined types after creation.

FAQs

Can we give default value for the table type columns?

Yes, we can give default values for the columns of the User-defined Table types. Check the following example. 

  1. --Creating Table type  
  2.   
  3. CREATE TYPE empTempTabtype AS Table (ID int identity(1,1),name varchar(100) default('ravi'), age int, location VARCHAR(100))    
  4.   
  5. --Creating temp table of above type  
  6.   
  7. DECLARE @emp empTempTabType    
  8. INSERT INTO @emp(age, location) VALUES(23, 'Hyderabad'), (30, 'Hyderabad')    
  9. SELECT * FROM @emp    

  10. --Result will be as follows

SQL server

Can we add schema for types?

Yes, we can create schema wise types.

Can we create identity column for User-Defined Table types?

Yes, we can create the Identity column on the User-defined Table type. See the following example.

  1. --Creating Table type  
  2.   
  3. CREATE TYPE empTempTabtype AS Table (ID int identity(1,1),name varchar(100), age int, location VARCHAR(100))    
  4.   
  5. --Creating temp table of above type  
  6.   
  7. DECLARE @emp empTempTabType    
  8. INSERT INTO @emp VALUES('Ravi', 23, 'Hyderabad')    
  9. INSERT INTO @emp VALUES('Kira', 30, 'Hyderabad')    
  10. SELECT * FROM @emp   

Result will be as follows,

SQL server
Can we use User-Defined Data types in the System function like CAST, CONVERT?

No , we cannot.

  1. CREATE TYPE TypeName FROM VARCHAR(100)  
  2. select CAST(1 AS TypeName)  

We get the following exception on executing the above script 

  1. Type TypeName is not a defined system type.  


Similar Articles