User-Defined Table Types
UDT stands for "User-Defined Data Type." It is a feature in database management systems that allows users to create custom data types to suit their specific needs. While most database systems come with predefined data types like integers, strings, dates, etc., UDTs offer the flexibility to define new data types that can represent more complex or specialized information.
User-Defined Data Types (UDTs) can be used in various scenarios in a database to organize, standardize, and manage custom data types.
When creating a UDT, you can combine existing data types into a single logical unit. For example, if you need to store information about a person's address, you can create a UDT called "Person" that encapsulates these attributes into a single data type.
Example of Creating a User-Defined Table Type (UDTT) named "address_udt"
CREATE TYPE address_udt AS TABLE (
street VARCHAR(100),
city VARCHAR(50),
state VARCHAR(50),
postal_code VARCHAR(10)
);
Insert data into the table variable and Select the data from the table variable.
-- Declare a table variable of the UDTT type
DECLARE @addresses address_udt;
-- Insert data into the table variable
INSERT INTO @addresses (street, city, state, postal_code)
VALUES
('123 Main St', 'Noida', 'UP', '201301'),
('456 house', 'Delhi', 'Delhi', '231521');
-- Display the contents of the table variable
SELECT * FROM @addresses;
Drop a User-Defined Table Type (UDTT) named "address_udt".
DROP TYPE address_udt;
Retrieve User-Defined Types from the Database.
SELECT *
FROM sys.table_types
WHERE is_user_defined = 1;
Retrieve User-Defined Types from the Database by name.
SELECT *
FROM sys.table_types
WHERE is_user_defined = 1 AND name = 'address_udt';
Storing Personal Details with Addresses using User-Defined Table Type (UDT) in Stored Procedure and how to execute the stored procedure.
-- First, create a table to store the main data
CREATE TABLE PersonTable (
ID INT IDENTITY(1,1) PRIMARY KEY,
Name VARCHAR(100),
FatherName VARCHAR(100),
Age INT,
Street VARCHAR(100),
City VARCHAR(50),
State VARCHAR(50),
Postal_Code VARCHAR(10)
);
--Create a User-Defined Table Type (UDTT) named "address_udt"
CREATE TYPE address_udt AS TABLE (
street VARCHAR(100),
city VARCHAR(50),
state VARCHAR(50),
postal_code VARCHAR(10)
);
--Create the procedure to insert personal details along with addresses
Create PROCEDURE InsertDataWithAddress
@Name VARCHAR(100),
@FatherName VARCHAR(100),
@Age INT,
@Address Address_UDT READONLY
AS
BEGIN
DECLARE @Street VARCHAR(100);
DECLARE @City VARCHAR(50);
DECLARE @State VARCHAR(50);
DECLARE @Postal_Code VARCHAR(10);
-- Assume that the UDT has only one row. You can modify accordingly if needed.
SELECT TOP 1 @Street = Street, @City = City, @State = State, @Postal_Code = Postal_Code FROM @Address;
-- Insert the main data into MainTable
INSERT INTO PersonTable (Name, FatherName, Age, Street, City, State, Postal_Code)
VALUES (@Name, @FatherName, @Age, @Street, @City, @State, @Postal_Code);
END;
--To use this procedure, you can insert data into the address_udt table type and call the persondetail procedure as follows:
-- Declare and populate the UDT variable for the address
DECLARE @MyAddress Address_UDT;
INSERT INTO @MyAddress (Street, City, State, Postal_Code)
VALUES ('123 Main St', 'Noida', 'UP', '201310');
-- Insert data with address details using the stored procedure
EXEC InsertDataWithAddress
@Name = 'Mukesh',
@FatherName = 'Rahul',
@Age = 30,
@Address = @MyAddress;
select * from PersonTable
User-Defined Table Type (UDTT) for storing product information and then using it in a function to retrieve data based on specific criteria.
-- First, create a table to store the main data and insert data into table
CREATE TABLE ProductTable (
ProductID INT IDENTITY(1,1) PRIMARY KEY,
ProductName VARCHAR(100),
Price DECIMAL(10, 2),
Category VARCHAR(50)
);
INSERT INTO ProductTable (ProductName, Price, Category)
VALUES ('Product A', 100.00, 'Electronics'),
('Product B', 50.00, 'Clothing'),
('Product C', 20.00, 'Electronics'),
('Product D', 10.00, 'Toys'),
('Product E', 80.00, 'Clothing');
--Create a User-Defined Table Type (UDTT) named "product_udt"
CREATE TYPE product_udt AS TABLE (
ProductID INT,
ProductName VARCHAR(100),
Price DECIMAL(10, 2),
Category VARCHAR(50)
);
-- create a function that uses the UDTT to retrieve products based on a specific category:
CREATE FUNCTION GetProductsByCategory
(
@CategoryFilter product_udt READONLY
)
RETURNS TABLE
AS
RETURN
(
SELECT ProductID, ProductName, Price, Category
FROM ProductTable
WHERE Category IN (SELECT Category FROM @CategoryFilter)
);
Execute function to retrieve products by category:
DECLARE @FilterCategory AS product_udt;
INSERT INTO @FilterCategory (Category)
VALUES ('Electronics'), ('Clothing');
SELECT * FROM dbo.GetProductsByCategory(@FilterCategory);

Join the conversation! Your thoughts help the community grow.