Is it possible to use a user-defined table type in SQL Server like a Struct?

Viewed 67

In PostgreSQL, I can do something like this to define and then use a composite type:

create type PERSON as (first varchar, last varchar);
select PERSON '("tom", "jones")'

And in SQL Server I can also create a custom type by doing:

create type person as table (first varchar, last varchar);

Is there any way then to use that composite/table-type within a select statement, such as doing:

select CAST(('tom', 'jones') AS person)

Or something similar?

1 Answers

Table Types are readonly and cannot be modified, you can declare a variable as type of TableType and do insert update which does not actually update the table on which it is created. you can do something like this

DECLARE @personType AS person
 
INSERT INTO @personType 
VALUES ( 'tom', 'jones' )

SELECT * FROM @personType 
Related