Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7407771
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T05:47:01+00:00 2026-05-29T05:47:01+00:00

I have a need to create a user table type. My preference is to

  • 0

I have a need to create a user table type. My preference is to use a single SQL statement. Ignoring the bells and whistles that can be attributed to such create statement I first derived the logic to generate the create statement for a single column table with the statement:

select 'create type ' + tt.name + ' as TABLE (' + c.name + ' ' + t.name +
case
when t.name in ('varchar','char','nvarchar','nchar','binary','varbinary') then
     '(' +
      case
         when c.max_length = -1 then 'MAX'
         else convert(varchar, c.max_length)
      end + ')'
    when t.name in ('numeric','decimal') then
      '(' + convert(varchar, c.precision) + ',' + convert(varchar, c.scale) + ')'
    else ''
 end + ')'
from sys.table_types tt
join sys.columns c
on tt.type_table_object_id = c.object_id
join sys.types t
on c.system_type_id = t.system_type_id and
c.user_type_id = t.user_type_id

This is limited in scope in terms of the data types, etc but sufficient for now. What I am trying to figure out is out to expand this such that the statement that creates the table column definitions is in some sort of inner loop to handle 1 to n columns. It seems as though it should be possible, but I have not been able to work out the logic.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T05:47:01+00:00Added an answer on May 29, 2026 at 5:47 am

    You can use the for xml path string concatenation trick.

    select 'create type ' + tt.name + ' as TABLE (' + 
        stuff((select ', '+c.name + ' ' + t.name +
                      case when t.name in ('varchar','char','nvarchar','nchar','binary','varbinary') 
                           then '(' + case when c.max_length = -1 
                                           then 'MAX'
                                           else convert(varchar, c.max_length)
                                      end + ')'
                           when t.name in ('numeric','decimal') 
                           then '(' + convert(varchar, c.precision) + ',' + convert(varchar, c.scale) + ')'
                           else ''
                      end 
               from sys.columns c
                 inner join sys.types t
                   on c.system_type_id = t.system_type_id and
                      c.user_type_id = t.user_type_id        
               where c.object_id = tt.type_table_object_id
               for xml path(''), type      
              ).value('.', 'varchar(max)'), 1, 2, '')+ ')'
    from sys.table_types tt
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a User-Defined Table Type called tvpInsertedColumns : CREATE TYPE [Audit].[tvpInsertedColumns] AS TABLE(
I have this SQL Server 2008 UDT: CREATE TYPE [dbo].[IdentityType] AS TABLE( [Id] [int]
I have a need to create a transactional process using an external API that
We have a need to create a private key that is seeded from 2
I have a need to create a library of Object Oriented PHP code that
I need to create a textbox that a user can enter an arbitrary string
I have a SQL query that isn't table-specific and I don't know how to
I have a table videos and I need to create top videos for it.
I have the need to create the functionality to allow my registered users to
I have a need to create a HttpSession (via cookie) whenever a client invokes

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.